Skip to content

Commit 3538fe2

Browse files
API Clients can now specify their request headers (#245)
1 parent c200fc1 commit 3538fe2

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

src/Account/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ protected function makePricingRequest($country, $pricingType) : array
154154
*/
155155
public function getBalance() : Balance
156156
{
157-
$data = $this->getAccountAPI()->get('get-balance');
157+
$data = $this->getAccountAPI()->get('get-balance', [], ['accept' => 'application/json']);
158158

159159
if (is_null($data)) {
160160
throw new Exception\Server('No results found');

src/Client/APIResource.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ class APIResource implements ClientAwareInterface
6363
*/
6464
protected $lastResponse;
6565

66-
public function create(array $body, string $uri = '') : ?array
66+
public function create(array $body, string $uri = '', $headers = []) : ?array
6767
{
68+
if (empty($headers)) {
69+
$headers = ['content-type' => 'application/json'];
70+
}
71+
6872
$request = new Request(
6973
$this->getBaseUrl() . $this->getBaseUri() . $uri,
7074
'POST',
7175
'php://temp',
72-
['content-type' => 'application/json']
76+
$headers
7377
);
7478

7579
$request->getBody()->write(json_encode($body));
@@ -90,17 +94,22 @@ public function create(array $body, string $uri = '') : ?array
9094
return json_decode($response->getBody()->getContents(), true);
9195
}
9296

93-
public function delete(string $id) : ?array
97+
public function delete(string $id, $headers = []) : ?array
9498
{
9599
$uri = $this->getBaseUrl() . $this->baseUri . '/' . $id;
100+
101+
if (empty($headers)) {
102+
$headers = [
103+
'accept' => 'application/json',
104+
'content-type' => 'application/json'
105+
];
106+
}
107+
96108
$request = new Request(
97109
$uri,
98110
'DELETE',
99111
'php://temp',
100-
[
101-
'accept' => 'application/json',
102-
'content-type' => 'application/json'
103-
]
112+
$headers
104113
);
105114

106115
$response = $this->getClient()->send($request);
@@ -117,20 +126,25 @@ public function delete(string $id) : ?array
117126
return json_decode($response->getBody()->getContents(), true);
118127
}
119128

120-
public function get($id, array $query = [])
129+
public function get($id, array $query = [], $headers = [])
121130
{
122131
$uri = $this->getBaseUrl() . $this->baseUri . '/' . $id;
123132
if (!empty($query)) {
124133
$uri .= '?' . http_build_query($query);
125134
}
135+
136+
if (empty($headers)) {
137+
$headers = [
138+
'accept' => 'application/json',
139+
'content-type' => 'application/json'
140+
];
141+
}
142+
126143
$request = new Request(
127144
$uri,
128145
'GET',
129146
'php://temp',
130-
[
131-
'accept' => 'application/json',
132-
'content-type' => 'application/json'
133-
]
147+
$headers
134148
);
135149

136150
$response = $this->getClient()->send($request);
@@ -284,13 +298,17 @@ public function setLastRequest(RequestInterface $request) : self
284298
/**
285299
* Allows form URL-encoded POST requests
286300
*/
287-
public function submit(array $formData = [], string $uri = '') : string
301+
public function submit(array $formData = [], string $uri = '', $headers = []) : string
288302
{
303+
if (empty($headers)) {
304+
$headers = ['content-type' => 'application/x-www-form-urlencoded'];
305+
}
306+
289307
$request = new Request(
290308
$this->baseUrl . $this->baseUri . $uri,
291309
'POST',
292310
'php://temp',
293-
['content-type' => 'application/x-www-form-urlencoded']
311+
$headers
294312
);
295313

296314
$request->getBody()->write(http_build_query($formData));
@@ -307,13 +325,17 @@ public function submit(array $formData = [], string $uri = '') : string
307325
return $response->getBody()->getContents();
308326
}
309327

310-
public function update(string $id, array $body) : ?array
328+
public function update(string $id, array $body, $headers = []) : ?array
311329
{
330+
if (empty($headers)) {
331+
$headers = ['content-type' => 'application/json'];
332+
}
333+
312334
$request = new Request(
313335
$this->getBaseUrl() . $this->baseUri . '/' . $id,
314336
'PUT',
315337
'php://temp',
316-
['content-type' => 'application/json']
338+
$headers
317339
);
318340

319341
$request->getBody()->write(json_encode($body));

0 commit comments

Comments
 (0)