1616 */
1717namespace Phramework \JSONAPI \Client ;
1818
19- use Phramework \JSONAPI \Client \Exceptions \Exception ;
19+ use Phramework \JSONAPI \Client \Exceptions \ResponseException ;
2020use Phramework \JSONAPI \Client \Response \Collection ;
21+ use Phramework \JSONAPI \Client \Response \Errors ;
2122use Phramework \JSONAPI \Client \Response \Resource ;
2223
2324/**
@@ -120,8 +121,9 @@ protected function prepareHeaders(\stdClass $additional = null)
120121 * @param IncludeRelationship|null $include Include directive
121122 * @param \stdClass|null $additionalHeaders Will override global
122123 * headers
123- * @param \string[] ...$additional Additional url
124+ * @param \string[] ...$additional Additional url parts
124125 * @return Collection
126+ * @throws ResponseException When response code is not on of 200, 201, 202, 203 or 204
125127 * @example
126128 * ```php
127129 * $users = Users::get(
@@ -150,7 +152,11 @@ protected function prepareHeaders(\stdClass $additional = null)
150152 * new Fields((object) [
151153 * 'user' => ['name', 'email']
152154 * ]),
153- * new IncludeRelationship('project', 'group')
155+ * new IncludeRelationship('project', 'group'),
156+ * (object) [
157+ * 'Accept' => 'application/vnd.api+json'
158+ * ],
159+ * '&token=1234'
154160 * );
155161 * ```
156162 */
@@ -237,7 +243,7 @@ public static function get(
237243 * headers
238244 * @param \string[] ...$additional Additional url
239245 * @return Resource
240- * @throws Exception
246+ * @throws ResponseException When response code is not on of 200, 201, 202, 203 or 204
241247 * @example
242248 * ```php
243249 * $user = User::getById(
@@ -302,33 +308,64 @@ public static function getById(
302308
303309 public static function post (
304310 \stdClass $ attributes = null ,
305- \stdClass $ relationships = null
311+ \stdClass $ relationships = null ,
312+ \stdClass $ additionalHeaders = null ,
313+ string ...$ additional
306314 ) {
307315
308316 }
309317
310318 public static function patch (
311319 string $ id ,
312320 \stdClass $ attributes = null ,
313- \stdClass $ relationships = null
321+ \stdClass $ relationships = null ,
322+ \stdClass $ additionalHeaders = null ,
323+ string ...$ additional
314324 ) {
315325
316326 }
317327
318328 public static function delete (
319- string $ id
329+ string $ id ,
330+ \stdClass $ additionalHeaders = null ,
331+ string ...$ additional
320332 ) {
321333
322334 }
323335
324336 /**
325- * Perform an cURL request
337+ * Perform an cURL request to a JSON API web service over HTTP
338+ * @param string $url Request url
339+ * @param string $method Request HTTP method
340+ * @param \stdClass|null $headers Request headers
341+ * @param \stdClass|null $data Request body
342+ * @return array which contains
343+ * - $responseStatusCode
344+ * - $responseHeaders
345+ * - $responseBody (JSON encoded)
346+ * @throws ResponseException When response code is not on of 200, 201, 202, 203 or 204
347+ * @throws \Exception
348+ * @example
349+ * ```php
350+ * list(
351+ * $responseStatusCode,
352+ * $responseHeaders,
353+ * $responseBody
354+ * ) = Client::request(
355+ * 'http://myapi.com/user/',
356+ * Client::METHOD_GET,
357+ * (object) [
358+ * 'Content-Type' => 'application/vnd.api+json',
359+ * 'Accept' => 'application/vnd.api+json'
360+ * ]
361+ * );
362+ * ```
326363 */
327364 public static function request (
328365 string $ url ,
329366 string $ method = self ::METHOD_GET ,
330367 \stdClass $ headers = null ,
331- $ data = null ,
368+ \ stdClass $ data = null ,
332369 $ flags = self ::REQUEST_EMPTY_FLAG ,
333370 //$accept = 'application/json',
334371 $ encoding = null
@@ -417,7 +454,7 @@ public static function request(
417454 curl_setopt ($ handle , CURLOPT_CUSTOMREQUEST , self ::METHOD_DELETE );
418455 break ;
419456 default :
420- throw new Exception ('Unsupported method ' );
457+ throw new \ Exception ('Unsupported method ' );
421458 }
422459
423460 //Get response
@@ -442,27 +479,23 @@ public static function request(
442479
443480 curl_close ($ handle );
444481
482+ //Throw exception on response failure
483+ if (!in_array ($ responseStatusCode , [200 , 201 , 202 , 203 , 204 ])) {
484+ throw new ResponseException (
485+ (new Errors ())
486+ ->parse (json_decode ($ responseBody ))
487+ ->setHeaders ($ responseHeaders )
488+ ->setStatusCode ($ responseStatusCode )
489+ );
490+ }
491+
492+ //Return the data of response
445493 return [
446494 $ responseStatusCode ,
447495 $ responseHeaders ,
448496 json_decode ($ responseBody )
449497 ];
450498
451- /* if (!$response) {
452- throw new Exception('Error: ' . curl_error($handle));
453- }*/
454-
455- /* //Throw exception on response failure
456- if (!in_array($code, array(200, 201, 202))) { // OK, Created, Accepted
457- $decoded = json_decode($response, true);
458- throw new Exception($decoded['error'], $code);
459- }*/
460-
461- curl_close ($ handle );
462- //Return the data of response
463-
464- return $ response ;
465-
466499 /*return (
467500
468501 $accept == 'application/json' ? json_decode($response, true) : $response );*/
0 commit comments