1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP SDK library for the Superdesk Content API. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Superdesk\ContentApiSdk\Client; |
16
|
|
|
|
17
|
|
|
use Superdesk\ContentApiSdk\API\Request\RequestInterface; |
18
|
|
|
use Superdesk\ContentApiSdk\API\Request\OAuthDecorator; |
19
|
|
|
use Superdesk\ContentApiSdk\API\Response; |
20
|
|
|
use Superdesk\ContentApiSdk\ContentApiSdk; |
21
|
|
|
use Superdesk\ContentApiSdk\Exception\AuthenticationException; |
22
|
|
|
use Superdesk\ContentApiSdk\Exception\AccessDeniedException; |
23
|
|
|
use Superdesk\ContentApiSdk\Exception\ClientException; |
24
|
|
|
use Superdesk\ContentApiSdk\Exception\ResponseException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Request service that implements all method regarding basic request/response |
28
|
|
|
* handling. |
29
|
|
|
*/ |
30
|
|
|
class CurlApiClient 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
|
|
|
$response = null; |
47
|
|
|
|
48
|
|
|
if ($this->authenticator->getAccessToken() !== null) { |
49
|
|
|
$authenticatedRequest = new OAuthDecorator($request); |
50
|
|
|
$authenticatedRequest->setAccessToken($this->authenticator->getAccessToken()); |
51
|
|
|
$authenticatedRequest->addAuthentication(); |
52
|
|
|
|
53
|
|
|
$response = $this->client->makeCall( |
54
|
|
|
$authenticatedRequest->getFullUrl(), |
55
|
|
|
$this->add_default_headers($authenticatedRequest->getHeaders()), |
56
|
|
|
$authenticatedRequest->getOptions() |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
if ($response['status'] == 200) { |
60
|
|
|
$this->authenticationRetryLimit = 0; |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
return new Response($response['body'], $response['headers']); |
64
|
|
|
} catch (ResponseException $e) { |
65
|
|
|
throw new ClientException($e->getMessage(), $e->getCode(), $e); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($response === null || $response['status'] == 401) { |
71
|
|
|
|
72
|
|
|
$this->authenticationRetryLimit++; |
73
|
|
|
|
74
|
|
|
if ($this->authenticationRetryLimit > self::MAX_RETRY_LIMIT) { |
75
|
|
|
throw new AccessDeniedException('Authentication retry limit reached.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
$this->authenticator->setBaseUrl($request->getBaseUrl()); |
80
|
|
|
$this->authenticator->getAuthenticationTokens(); |
81
|
|
|
|
82
|
|
|
// Reexecute event |
83
|
|
|
return $this->makeApiCall($request); |
84
|
|
|
} catch (AccessDeniedException $e) { |
85
|
|
|
throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e); |
86
|
|
|
} catch (AuthenticationException $e) { |
87
|
|
|
throw new AccessDeniedException('Could not authenticate against API.', $e->getCode(), $e); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
throw new ClientException(sprintf('The server returned an error with status %s.', $response['status'])); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds default headers to the headers per request, only if the key |
96
|
|
|
* cannot not be found in the headers per request. |
97
|
|
|
* |
98
|
|
|
* @param array $headers |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private function add_default_headers($headers) |
103
|
|
|
{ |
104
|
|
|
foreach ($this->headers as $key => $value) { |
105
|
|
|
if (!isset($headers[$key])) { |
106
|
|
|
$headers[$key] = $value; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $headers; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|