@@ -27,6 +27,7 @@ class Client
27
27
'logger ' => null ,
28
28
'base_uri ' => 'https://api.abicart.com/v2/ ' ,
29
29
'allow_redirects ' => false ,
30
+ 'parse_result ' => false ,
30
31
];
31
32
32
33
private $ config ;
@@ -51,8 +52,7 @@ public function get(string $resource, array $query = []): ?object
51
52
$ response = $ client ->get ("{$ resource }{$ params }" , [
52
53
'query ' => $ query ,
53
54
]);
54
- $ content = $ response ->getBody ()->__toString ();
55
- return json_decode ($ content );
55
+ return $ this ->result ($ response );
56
56
}
57
57
58
58
public function post (string $ resource , object $ body ): string
@@ -61,7 +61,7 @@ public function post(string $resource, object $body): string
61
61
$ response = $ client ->post ("{$ resource }" , [
62
62
'body ' => json_encode ($ body ),
63
63
]);
64
- return $ response -> getHeaderLine ( ' Location ' );
64
+ return $ this -> result ( $ response );
65
65
}
66
66
67
67
public function put (string $ resource , object $ body ): string
@@ -70,14 +70,32 @@ public function put(string $resource, object $body): string
70
70
$ response = $ client ->put ("{$ resource }" , [
71
71
'body ' => json_encode ($ body ),
72
72
]);
73
- return $ response -> getHeaderLine ( ' Location ' );
73
+ return $ this -> result ( $ response );
74
74
}
75
75
76
76
public function delete (string $ resource ): bool
77
77
{
78
78
$ client = $ this ->getClient ();
79
79
$ 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 );
81
99
}
82
100
83
101
// Set up Guzzle client with middleware
0 commit comments