1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audiens\AppnexusClient; |
4
|
|
|
|
5
|
|
|
use Audiens\AppnexusClient\authentication\AuthStrategyInterface; |
6
|
|
|
use Doctrine\Common\Cache\Cache; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\UriInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* see https://wiki.apnexus.com/display/adnexusdocumentation/Segment+Service |
14
|
|
|
*/ |
15
|
|
|
class Auth implements ClientInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** @var Cache */ |
19
|
|
|
protected $cache; |
20
|
|
|
|
21
|
|
|
/** @var ClientInterface */ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $token; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $username; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $password; |
32
|
|
|
|
33
|
|
|
protected $authStrategy; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
$username, |
37
|
|
|
$password, |
38
|
|
|
ClientInterface $clientInterface, |
39
|
|
|
AuthStrategyInterface $authStrategy |
40
|
|
|
) { |
41
|
|
|
$this->username = $username; |
42
|
|
|
$this->password = $password; |
43
|
|
|
|
44
|
|
|
$this->client = $clientInterface; |
45
|
|
|
$this->authStrategy = $authStrategy; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $method |
50
|
|
|
* @param string|UriInterface $uri |
51
|
|
|
* @param array $options |
52
|
|
|
* |
53
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
|
public function request($method, $uri, array $options = []) |
57
|
|
|
{ |
58
|
|
|
$optionForToken = [ |
59
|
|
|
'headers' => [ |
60
|
|
|
'Authorization' => $this->authStrategy->authenticate($this->username, $this->password), |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$options = array_merge($options, $optionForToken); |
65
|
|
|
|
66
|
|
|
$response = $this->client->request($method, $uri, $options); |
67
|
|
|
|
68
|
|
|
if (!$this->needToRevalidate($response)) { |
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$optionForToken = [ |
73
|
|
|
'headers' => [ |
74
|
|
|
'Authorization' => $this->authStrategy->authenticate($this->username, $this->password), |
75
|
|
|
], |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$options = array_merge($options, $optionForToken); |
79
|
|
|
|
80
|
|
|
return $this->client->request($method, $uri, $options); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritDoc |
85
|
|
|
*/ |
86
|
|
|
public function send(RequestInterface $request, array $options = []) |
87
|
|
|
{ |
88
|
|
|
return $this->client->send($request, $options); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
|
|
public function sendAsync(RequestInterface $request, array $options = []) |
95
|
|
|
{ |
96
|
|
|
return $this->client->sendAsync($request, $options); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritDoc |
101
|
|
|
*/ |
102
|
|
|
public function requestAsync($method, $uri, array $options = []) |
103
|
|
|
{ |
104
|
|
|
return $this->client->requestAsync($method, $uri, $options); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
|
|
public function getConfig($option = null) |
111
|
|
|
{ |
112
|
|
|
return $this->client->getConfig($option); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function needToRevalidate(ResponseInterface $response): bool |
116
|
|
|
{ |
117
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
118
|
|
|
|
119
|
|
|
$response->getBody()->rewind(); |
120
|
|
|
|
121
|
|
|
return isset($content['response']['error_id']) && $content['response']['error_id'] == 'NOAUTH'; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|