Skip to content

Commit fedc2d5

Browse files
author
Sören
committed
Result parsing
1 parent 0f6b627 commit fedc2d5

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/Abicart/V2/Client.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Client
2727
'logger' => null,
2828
'base_uri' => 'https://api.abicart.com/v2/',
2929
'allow_redirects' => false,
30+
'parse_result' => false,
3031
];
3132

3233
private $config;
@@ -51,8 +52,7 @@ public function get(string $resource, array $query = []): ?object
5152
$response = $client->get("{$resource}{$params}", [
5253
'query' => $query,
5354
]);
54-
$content = $response->getBody()->__toString();
55-
return json_decode($content);
55+
return $this->result($response);
5656
}
5757

5858
public function post(string $resource, object $body): string
@@ -61,7 +61,7 @@ public function post(string $resource, object $body): string
6161
$response = $client->post("{$resource}", [
6262
'body' => json_encode($body),
6363
]);
64-
return $response->getHeaderLine('Location');
64+
return $this->result($response);
6565
}
6666

6767
public function put(string $resource, object $body): string
@@ -70,14 +70,32 @@ public function put(string $resource, object $body): string
7070
$response = $client->put("{$resource}", [
7171
'body' => json_encode($body),
7272
]);
73-
return $response->getHeaderLine('Location');
73+
return $this->result($response);
7474
}
7575

7676
public function delete(string $resource): bool
7777
{
7878
$client = $this->getClient();
7979
$response = $client->delete("{$resource}");
80-
return true;
80+
return $this->result($response);
81+
}
82+
83+
public function parse(ResponseInterface $response)
84+
{
85+
switch ($response->getStatusCode()) {
86+
case 200:
87+
return json_decode($response->getBody()->__toString());
88+
case 201:
89+
case 204:
90+
return true;
91+
case 404:
92+
return null;
93+
}
94+
}
95+
96+
private function result(ResponseInterface $response)
97+
{
98+
return $this->config['parse_result'] ? $response : $this->parse($response);
8199
}
82100

83101
// Set up Guzzle client with middleware

0 commit comments

Comments
 (0)