1
|
|
|
<?php |
2
|
|
|
namespace Loevgaard\Linkmobility; |
3
|
|
|
|
4
|
|
|
use Assert\Assert; |
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use Loevgaard\Linkmobility\Request\RequestInterface; |
8
|
|
|
use Loevgaard\Linkmobility\Response\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface as HttpResponseInterface; |
10
|
|
|
|
11
|
|
|
class Client |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The API key used for making requests |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $apiKey; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The base url used for making requests |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $baseUrl; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The HTTP client used for making requests |
29
|
|
|
* |
30
|
|
|
* @var ClientInterface |
31
|
|
|
*/ |
32
|
|
|
protected $httpClient; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Contains the last response object |
36
|
|
|
* |
37
|
|
|
* @var HttpResponseInterface|null |
38
|
|
|
*/ |
39
|
|
|
protected $lastHttpResponse; |
40
|
|
|
|
41
|
1 |
|
public function __construct(string $apiKey, string $baseUrl = 'https://api.linkmobility.dk/v2') |
42
|
|
|
{ |
43
|
1 |
|
Assert::that($baseUrl)->url(); |
44
|
|
|
|
45
|
1 |
|
$this->apiKey = $apiKey; |
46
|
1 |
|
$this->baseUrl = $baseUrl; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param RequestInterface $request |
51
|
|
|
* @return ResponseInterface |
52
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
53
|
|
|
*/ |
54
|
|
|
public function request(RequestInterface $request) : ResponseInterface |
55
|
|
|
{ |
56
|
|
|
$this->rawRequest($request->getMethod(), $request->getUri(), $request->getOptions()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $method |
61
|
|
|
* @param string $uri |
62
|
|
|
* @param array $options |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
65
|
|
|
*/ |
66
|
|
|
public function rawRequest(string $method, string $uri, array $options = []) |
67
|
|
|
{ |
68
|
|
|
$client = $this->getHttpClient(); |
69
|
|
|
|
70
|
|
|
// @todo move default options somewhere else |
71
|
|
|
$options = array_merge($options, [ |
72
|
|
|
'headers' => [ |
73
|
|
|
'Accept' => 'application/json', |
74
|
|
|
], |
75
|
|
|
'verify' => false, |
76
|
|
|
'http_errors' => false, |
77
|
|
|
]); |
78
|
|
|
$this->lastHttpResponse = $client->request($method, $this->baseUrl . $uri.'?apikey='.$this->apiKey, $options); |
79
|
|
|
|
80
|
|
|
return \GuzzleHttp\json_decode((string)$this->lastHttpResponse->getBody()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return ClientInterface |
85
|
|
|
*/ |
86
|
|
|
public function getHttpClient() : ClientInterface |
87
|
|
|
{ |
88
|
|
|
if (!$this->httpClient) { |
89
|
|
|
$this->httpClient = new HttpClient(); |
90
|
|
|
} |
91
|
|
|
return $this->httpClient; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param ClientInterface $httpClient |
96
|
|
|
* @return Client |
97
|
|
|
*/ |
98
|
1 |
|
public function setHttpClient(ClientInterface $httpClient) : Client |
99
|
|
|
{ |
100
|
1 |
|
$this->httpClient = $httpClient; |
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the latest response or null if no request was made yet |
106
|
|
|
* |
107
|
|
|
* @return HttpResponseInterface|null |
108
|
|
|
*/ |
109
|
|
|
public function getLastHttpResponse() : ?HttpResponseInterface |
110
|
|
|
{ |
111
|
|
|
return $this->lastHttpResponse; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|