1 | <?php |
||
30 | class DefaultApiClient extends AbstractApiClient |
||
31 | { |
||
32 | /** |
||
33 | * Default request headers. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $headers = array( |
||
38 | 'Accept' => 'application/json' |
||
39 | ); |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function makeApiCall(RequestInterface $request) |
||
45 | { |
||
46 | if ($this->authenticator->getAccessToken() === null) { |
||
47 | $this->getNewToken($request); |
||
48 | } |
||
49 | |||
50 | $response = $this->sendRequest($this->authenticateRequest($request)); |
||
51 | |||
52 | switch ($response['status']) { |
||
53 | case 200: |
||
54 | $this->resetAuthenticationRetryAttempt(); |
||
55 | |||
56 | return $this->createResponseObject($response); |
||
57 | case 401: |
||
58 | $this->incrementAuthenticationRetryAttempt(); |
||
59 | |||
60 | if ($this->isAuthenticationRetryLimitReached()) { |
||
61 | throw new AccessDeniedException('Authentication retry limit reached.'); |
||
62 | } |
||
63 | |||
64 | // Once SD-3820 is fixed, implement SWP-92 branch, it will use |
||
65 | // the refresh token functionality, instead of request a new token |
||
66 | // each time this method is called. |
||
67 | $this->getNewToken($request); |
||
68 | |||
69 | return $this->makeApiCall($request); |
||
70 | } |
||
71 | |||
72 | throw new ClientException(sprintf('The server returned an error with status %s.', $response['status']), $response['status']); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Creates response object from response array. |
||
77 | * |
||
78 | * @param array $response |
||
79 | * |
||
80 | * @return Response API Response object |
||
81 | * |
||
82 | * @throws ClientException Thrown when response object could not be created |
||
83 | */ |
||
84 | protected function createResponseObject(array $response) |
||
92 | |||
93 | /** |
||
94 | * Adds authentication details to request with OAuth decorator. |
||
95 | * |
||
96 | * @param RequestInterface $request |
||
97 | * |
||
98 | * @return OAuthDecorator OAuth ready decorated Request |
||
99 | */ |
||
100 | protected function authenticateRequest(RequestInterface $request) |
||
108 | |||
109 | /** |
||
110 | * Sends the actual request. |
||
111 | * |
||
112 | * @param RequestInterface $request |
||
113 | * |
||
114 | * @return Response Response object created from raw response |
||
115 | * |
||
116 | * @throws ClientException Thrown when response could not be created. |
||
117 | */ |
||
118 | protected function sendRequest(RequestInterface $request) |
||
126 | |||
127 | /** |
||
128 | * Refreshes the token via then authenticator. |
||
129 | * |
||
130 | * @param RequestInterface $request |
||
131 | * |
||
132 | * @return void |
||
133 | */ |
||
134 | protected function getNewToken(RequestInterface $request) |
||
145 | |||
146 | /** |
||
147 | * Adds default headers to the headers per request, only if the key |
||
148 | * cannot not be found in the headers per request. |
||
149 | * |
||
150 | * @param array $headers |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | protected function addDefaultHeaders($headers) |
||
164 | } |
||
165 |