11
11
use MultiSafepay \Api \Base \RequestBodyInterface ;
12
12
use MultiSafepay \Api \Base \Response as ApiResponse ;
13
13
use MultiSafepay \Exception \ApiException ;
14
+ use MultiSafepay \Exception \ApiUnavailableException ;
14
15
use MultiSafepay \Exception \InvalidApiKeyException ;
15
- use MultiSafepay \Exception \StrictModeException ;
16
16
use Psr \Http \Client \ClientExceptionInterface ;
17
17
use Psr \Http \Client \ClientInterface ;
18
18
use Psr \Http \Message \RequestFactoryInterface ;
@@ -104,8 +104,7 @@ public function __construct(
104
104
* @param RequestBodyInterface|null $requestBody
105
105
* @param array $context
106
106
* @return ApiResponse
107
- * @throws ClientExceptionInterface
108
- * @throws ApiException
107
+ * @throws ClientExceptionInterface|ApiException|ApiUnavailableException
109
108
*/
110
109
public function createPostRequest (
111
110
string $ endpoint ,
@@ -115,10 +114,12 @@ public function createPostRequest(
115
114
$ request = $ this ->createRequest ($ endpoint , self ::METHOD_POST )
116
115
->withBody ($ this ->createBody ($ this ->getRequestBody ($ requestBody )))
117
116
->withHeader ('Content-Length ' , strlen ($ this ->getRequestBody ($ requestBody )));
117
+ $ httpResponse = $ this ->httpClient ->sendRequest ($ request );
118
118
119
119
$ context ['headers ' ] = $ request ->getHeaders ();
120
120
$ context ['request_body ' ] = $ this ->getRequestBody ($ requestBody );
121
- $ httpResponse = $ this ->httpClient ->sendRequest ($ request );
121
+ $ context ['http_response_code ' ] = $ httpResponse ->getStatusCode () ?? 0 ;
122
+
122
123
return ApiResponse::withJson ($ httpResponse ->getBody ()->getContents (), $ context );
123
124
}
124
125
@@ -128,7 +129,7 @@ public function createPostRequest(
128
129
* @param RequestBodyInterface|null $requestBody
129
130
* @param array $context
130
131
* @return ApiResponse
131
- * @throws ClientExceptionInterface|ApiException
132
+ * @throws ClientExceptionInterface|ApiException|ApiUnavailableException
132
133
*/
133
134
public function createPatchRequest (
134
135
string $ endpoint ,
@@ -138,10 +139,12 @@ public function createPatchRequest(
138
139
$ request = $ this ->createRequest ($ endpoint , self ::METHOD_PATCH )
139
140
->withBody ($ this ->createBody ($ this ->getRequestBody ($ requestBody )))
140
141
->withHeader ('Content-Length ' , strlen ($ this ->getRequestBody ($ requestBody )));
142
+ $ httpResponse = $ this ->httpClient ->sendRequest ($ request );
141
143
142
144
$ context ['headers ' ] = $ request ->getHeaders ();
143
145
$ context ['request_body ' ] = $ this ->getRequestBody ($ requestBody );
144
- $ httpResponse = $ this ->httpClient ->sendRequest ($ request );
146
+ $ context ['http_response_code ' ] = $ httpResponse ->getStatusCode () ?? 0 ;
147
+
145
148
return ApiResponse::withJson ($ httpResponse ->getBody ()->getContents (), $ context );
146
149
}
147
150
@@ -150,14 +153,17 @@ public function createPatchRequest(
150
153
* @param array $parameters
151
154
* @param array $context
152
155
* @return ApiResponse
153
- * @throws ClientExceptionInterface|ApiException
156
+ * @throws ClientExceptionInterface|ApiException|ApiUnavailableException
154
157
*/
155
158
public function createGetRequest (string $ endpoint , array $ parameters = [], array $ context = []): ApiResponse
156
159
{
157
160
$ request = $ this ->createRequest ($ endpoint , self ::METHOD_GET , $ parameters );
158
161
$ httpResponse = $ this ->httpClient ->sendRequest ($ request );
162
+
159
163
$ context ['headers ' ] = $ request ->getHeaders ();
160
164
$ context ['request_params ' ] = $ parameters ;
165
+ $ context ['http_response_code ' ] = $ httpResponse ->getStatusCode () ?? 0 ;
166
+
161
167
return ApiResponse::withJson ($ httpResponse ->getBody ()->getContents (), $ context );
162
168
}
163
169
@@ -166,14 +172,17 @@ public function createGetRequest(string $endpoint, array $parameters = [], array
166
172
* @param array $parameters
167
173
* @param array $context
168
174
* @return ApiResponse
169
- * @throws ClientExceptionInterface|ApiException
175
+ * @throws ClientExceptionInterface|ApiException|ApiUnavailableException
170
176
*/
171
177
public function createDeleteRequest (string $ endpoint , array $ parameters = [], array $ context = []): ApiResponse
172
178
{
173
179
$ request = $ this ->createRequest ($ endpoint , self ::METHOD_DELETE , $ parameters );
174
180
$ httpResponse = $ this ->httpClient ->sendRequest ($ request );
181
+
175
182
$ context ['headers ' ] = $ request ->getHeaders ();
176
183
$ context ['request_params ' ] = $ parameters ;
184
+ $ context ['http_response_code ' ] = $ httpResponse ->getStatusCode () ?? 0 ;
185
+
177
186
return ApiResponse::withJson ($ httpResponse ->getBody ()->getContents (), $ context );
178
187
}
179
188
@@ -202,6 +211,7 @@ public function getRequestUrl(string $endpoint, $parameters = []): string
202
211
$ parameters ['locale ' ] = $ this ->locale ;
203
212
}
204
213
$ endpoint .= '? ' . http_build_query ($ parameters );
214
+
205
215
return $ this ->url . $ endpoint ;
206
216
}
207
217
@@ -235,6 +245,7 @@ public function getHttpClient(): ClientInterface
235
245
public function useStrictMode (bool $ strictMode ): Client
236
246
{
237
247
$ this ->strictMode = $ strictMode ;
248
+
238
249
return $ this ;
239
250
}
240
251
@@ -248,6 +259,7 @@ private function createRequest(string $endpoint, string $method, array $paramete
248
259
{
249
260
$ url = $ this ->getRequestUrl ($ endpoint , $ parameters );
250
261
$ requestFactory = $ this ->getRequestFactory ();
262
+
251
263
return $ requestFactory ->createRequest ($ method , $ url )
252
264
->withHeader ('api_key ' , $ this ->apiKey ->get ())
253
265
->withHeader ('accept-encoding ' , 'application/json ' )
@@ -261,6 +273,7 @@ private function createRequest(string $endpoint, string $method, array $paramete
261
273
private function getRequestBody (RequestBodyInterface $ requestBody ): string
262
274
{
263
275
$ requestBody ->useStrictMode ($ this ->strictMode );
276
+
264
277
return json_encode ($ requestBody ->getData (), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
265
278
}
266
279
}
0 commit comments