1 | <?php |
||
38 | class Client implements LoggerAwareInterface |
||
39 | { |
||
40 | private $apiroot = 'https://connect.liblynx.com'; |
||
41 | |||
42 | /** @var string client ID obtain from LibLynx Connect admin portal */ |
||
43 | private $clientId; |
||
44 | |||
45 | /** @var string client secret obtain from LibLynx Connect admin portal */ |
||
46 | private $clientSecret; |
||
47 | |||
48 | /** @var ClientInterface HTTP client for API requests */ |
||
49 | private $guzzle; |
||
50 | |||
51 | /** @var \stdClass entry point resource */ |
||
52 | private $entrypoint; |
||
53 | |||
54 | /** @var CacheInterface */ |
||
55 | protected $cache; |
||
56 | |||
57 | /** @var LoggerInterface */ |
||
58 | protected $log; |
||
59 | |||
60 | /** @var HTTPClientFactory */ |
||
61 | protected $httpClientFactory; |
||
62 | |||
63 | /** |
||
64 | * Create new LibLynx API client |
||
65 | */ |
||
66 | 16 | public function __construct(HTTPClientFactory $clientFactory = null) |
|
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | 2 | public function setLogger(LoggerInterface $logger) |
|
86 | |||
87 | /** |
||
88 | * Set API root |
||
89 | * An alternative root may be provided to you by LibLynx Technical Support |
||
90 | * @param string $url |
||
91 | */ |
||
92 | 10 | public function setAPIRoot($url) |
|
96 | |||
97 | /** |
||
98 | * Set OAuth2.0 client credentials |
||
99 | * These will be provided to you by LibLynx Technical Support |
||
100 | * |
||
101 | * @param string $clientId |
||
102 | * @param string $clientSecret |
||
103 | */ |
||
104 | 12 | public function setCredentials($clientId, $clientSecret) |
|
109 | |||
110 | /** |
||
111 | * @return array containing client id and secret |
||
112 | */ |
||
113 | 2 | public function getCredentials() |
|
117 | |||
118 | /** |
||
119 | * Attempt an identification using the LibLynx API |
||
120 | * |
||
121 | * @param IdentificationRequest $request |
||
122 | * @return Identification|null |
||
123 | */ |
||
124 | 8 | public function authorize(IdentificationRequest $request) |
|
125 | { |
||
126 | 8 | $payload = $request->getRequestJSON(); |
|
127 | 8 | $response = $this->post('@new_identification', $payload); |
|
128 | 2 | if (!isset($response->id)) { |
|
129 | //failed |
||
130 | $this->log->critical('Identification request failed {payload}', ['payload' => $payload]); |
||
131 | return null; |
||
132 | } |
||
133 | |||
134 | 2 | $identification = new Identification($response); |
|
135 | 2 | $this->log->info( |
|
136 | 2 | 'Identification request for ip {ip} on URL {url} succeeded status={status} id={id}', |
|
137 | [ |
||
138 | 2 | 'status' => $identification->status, |
|
139 | 2 | 'id' => $identification->id, |
|
140 | 2 | 'ip' => $identification->ip, |
|
141 | 2 | 'url' => $identification->url |
|
142 | ] |
||
143 | ); |
||
144 | |||
145 | 2 | return $identification; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * General purpose 'GET' request against API |
||
150 | * @param $entrypoint string contains either an @entrypoint or full URL obtained from a resource |
||
151 | * @return mixed |
||
152 | */ |
||
153 | 12 | public function get($entrypoint) |
|
154 | { |
||
155 | 12 | return $this->makeAPIRequest('GET', $entrypoint); |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * General purpose 'POST' request against API |
||
160 | * @param $entrypoint string contains either an @entrypoint or full URL obtained from a resource |
||
161 | * @param $json string contains JSON formatted data to post |
||
162 | * @return mixed |
||
163 | */ |
||
164 | 8 | public function post($entrypoint, $json) |
|
168 | |||
169 | /** |
||
170 | * General purpose 'PUT' request against API |
||
171 | * @param $entrypoint string contains either an @entrypoint or full URL obtained from a resource |
||
172 | * @return mixed string contains JSON formatted data to put |
||
173 | */ |
||
174 | public function put($entrypoint, $json) |
||
178 | |||
179 | /** |
||
180 | * @param $method |
||
181 | * @param $entrypoint |
||
182 | * @param null $json |
||
183 | * @return \stdClass object containing JSON decoded response - note this can be an error response for normally |
||
184 | * handled errors |
||
185 | * @throws LogicException for integration errors, e.g. not setting a cache |
||
186 | * @throws APIException for unexpected API failures |
||
187 | */ |
||
188 | 14 | protected function makeAPIRequest($method, $entrypoint, $json = null) |
|
233 | |||
234 | 14 | public function resolveEntryPoint($nameOrUrl) |
|
247 | |||
248 | 14 | public function getEntryPoint($name) |
|
262 | |||
263 | 14 | protected function getEntrypointResource() |
|
282 | |||
283 | 14 | public function getCache() |
|
290 | |||
291 | 12 | public function setCache(CacheInterface $cache) |
|
295 | |||
296 | /** |
||
297 | * Internal helper to provide an OAuth2 capable HTTP client |
||
298 | */ |
||
299 | 12 | protected function getClient() |
|
315 | } |
||
316 |