-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathClient.php
More file actions
executable file
·153 lines (121 loc) · 4.03 KB
/
Client.php
File metadata and controls
executable file
·153 lines (121 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Mvdnbrk\DhlParcel;
use Composer\CaBundle\CaBundle;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use Mvdnbrk\DhlParcel\Endpoints\Authentication;
use Mvdnbrk\DhlParcel\Endpoints\Capabilities;
use Mvdnbrk\DhlParcel\Endpoints\Labels;
use Mvdnbrk\DhlParcel\Endpoints\ServicePoints;
use Mvdnbrk\DhlParcel\Endpoints\Shipments;
use Mvdnbrk\DhlParcel\Endpoints\TrackTrace;
use Mvdnbrk\DhlParcel\Exceptions\DhlParcelException;
class Client
{
const API_ENDPOINT = 'https://api-gw.dhlparcel.nl';
/** @var string */
protected $apiEndpoint = self::API_ENDPOINT;
/** @var string */
protected $accountId;
/** @var string */
protected $apiKey;
/** @var string */
protected $userId;
/** @var \GuzzleHttp\Client */
protected $httpClient;
/** @var \Mvdnbrk\DhlParcel\Endpoints\Authentication */
public $authentication;
/** @var \Mvdnbrk\DhlParcel\Endpoints\Labels */
public $labels;
/** @var \Mvdnbrk\DhlParcel\Endpoints\ServicePoints */
public $servicePoints;
/** @var \Mvdnbrk\DhlParcel\Endpoints\Shipments */
public $shipments;
/** @var \Mvdnbrk\DhlParcel\Endpoints\TrackTrace */
public $tracktrace;
/** @var Capabilities */
public $capabilities;
public function __construct()
{
$this->httpClient = new HttpClient([
RequestOptions::VERIFY => CaBundle::getBundledCaBundlePath(),
]);
$this->initializeEndpoints();
}
public function initializeEndpoints(): void
{
$this->authentication = new Authentication($this);
$this->labels = new Labels($this);
$this->servicePoints = new ServicePoints($this);
$this->shipments = new Shipments($this);
$this->tracktrace = new TrackTrace($this);
$this->capabilities = new Capabilities($this);
}
/**
* Performs a HTTP call to the API endpoint.
*
* @param string $httpMethod
* @param string $apiMethod
* @param string|null $httpBody
* @param array $requestHeaders
* @return \Psr\Http\Message\ResponseInterface
*
* @throws \Mvdnbrk\DhlParcel\Exceptions\DhlParcelException
*/
public function performHttpCall(string $httpMethod, string $apiMethod, ?string $httpBody = null, array $requestHeaders = [])
{
$url = $this->apiEndpoint.'/'.$apiMethod;
$headers = collect([
'Accept' => 'application/json',
])
->merge($requestHeaders)
->when($httpBody !== null, function ($headers) {
return $headers->put('Content-Type', 'application/json');
})
->all();
$request = new Request($httpMethod, $url, $headers, $httpBody);
try {
$response = $this->httpClient->send($request, ['http_errors' => false]);
} catch (RequestException $e) {
throw DhlParcelException::createFromGuzzleRequestException($e);
}
return $response;
}
public function getAccountId(): ?string
{
return $this->accountId;
}
public function setAccountId(string $value): self
{
$this->accountId = trim($value);
return $this;
}
public function setApiKey(string $value): self
{
$this->apiKey = trim($value);
return $this;
}
public function setUserId(string $value): self
{
$this->userId = trim($value);
return $this;
}
/**
* @throws \Mvdnbrk\DhlParcel\Exceptions\DhlParcelException
*/
public function credentials(): array
{
if (empty($this->userId)) {
throw new DhlParcelException('You have not set a user id. Please use setUserId() to set the user id.');
}
if (empty($this->apiKey)) {
throw new DhlParcelException('You have not set an API key. Please use setApiKey() to set the API key.');
}
return [
'userId' => $this->userId,
'key' => $this->apiKey,
];
}
}