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/** 
2425 * @author Xenofon Spafaridis <[email protected] > 2526 * @since 0.0.0 
2627 * @todo handle errors 
28+  * @todo add postbatch 
2729 */ 
2830abstract  class  Client
2931{
@@ -120,8 +122,9 @@ protected function prepareHeaders(\stdClass $additional = null)
120122     * @param IncludeRelationship|null $include Include directive 
121123     * @param \stdClass|null           $additionalHeaders Will override global 
122124     *     headers 
123-      * @param \string[]                ...$additional Additional url 
125+      * @param \string[]                ...$additional Additional url parts  
124126     * @return Collection 
127+      * @throws ResponseException When response code is not on of 200, 201, 202, 203 or 204 
125128     * @example 
126129     * ```php 
127130     * $users = Users::get( 
@@ -150,7 +153,11 @@ protected function prepareHeaders(\stdClass $additional = null)
150153     *    new Fields((object) [ 
151154     *        'user' => ['name', 'email'] 
152155     *    ]), 
153-      *    new IncludeRelationship('project', 'group') 
156+      *    new IncludeRelationship('project', 'group'), 
157+      *    (object) [ 
158+      *        'Accept' => 'application/vnd.api+json' 
159+      *    ], 
160+      *    '&token=1234' 
154161     * ); 
155162     * ``` 
156163     */ 
@@ -237,7 +244,7 @@ public static function get(
237244     *     headers 
238245     * @param \string[]                ...$additional Additional url 
239246     * @return Resource 
240-      * @throws Exception  
247+      * @throws ResponseException When response code is not on of 200, 201, 202, 203 or 204  
241248     * @example 
242249     * ```php 
243250     * $user = User::getById( 
@@ -300,35 +307,183 @@ public static function getById(
300307        return  $ resource ;
301308    }
302309
310+     /** 
311+      * @param \stdClass|null         $attributes 
312+      * @param RelationshipsData|null $relationships 
313+      * @param \stdClass|null         $additionalHeaders 
314+      * @param \string[]              ...$additional 
315+      * @return $this 
316+      * @throws ResponseException 
317+      * @throws \Exception 
318+      */ 
303319    public  static  function  post (
304320        \stdClass   $ attributes  = null ,
305-         \stdClass   $ relationships  = null 
321+         RelationshipsData    $ relationships  = null ,
322+         \stdClass   $ additionalHeaders  = null ,
323+         string  ...$ additional
306324    ) {
325+         $ API   = self ::getGlobalAPI ();
326+ 
327+         $ url  = $ API   . static ::$ endpoint  . '/ ' ;
328+ 
329+         //Append additional 
330+         $ url  = $ url  . implode ('' , $ additional );
331+ 
332+         $ headers  = static ::prepareHeaders ($ additionalHeaders );
333+ 
334+         $ body  = (object ) [
335+             'data '  => (object ) [
336+                 'type '  => static ::$ type
337+             ]
338+         ];
339+ 
340+         if  ($ attributes  !== null ) {
341+             $ body ->data ->attributes  = $ attributes ;
342+         }
343+ 
344+         if  ($ relationships  !== null ) {
345+             $ body ->data ->relationships  = $ relationships ->getRelationships ();
346+         }
347+ 
348+         list (
349+             $ responseStatusCode ,
350+             $ responseHeaders ,
351+             $ responseBody
352+         ) = static ::request (
353+             $ url ,
354+             self ::METHOD_POST ,
355+             $ headers ,
356+             $ body
357+         );
358+ 
359+         $ resource  = (new  Resource ())->parse (
360+             $ responseBody
361+         );
307362
363+         $ resource ->setStatusCode ($ responseStatusCode );
364+         $ resource ->setHeaders ($ responseHeaders );
365+ 
366+         return  $ resource ;
308367    }
309368
310369    public  static  function  patch (
311370        string  $ id ,
312371        \stdClass   $ attributes  = null ,
313-         \stdClass   $ relationships  = null 
372+         RelationshipsData    $ relationships  = null ,
373+         \stdClass   $ additionalHeaders  = null ,
374+         string  ...$ additional
314375    ) {
376+         $ API   = self ::getGlobalAPI ();
377+ 
378+         $ url  = $ API   . static ::$ endpoint  . '/ '  . $ id  . '/ ' ;
379+ 
380+         //Append additional 
381+         $ url  = $ url  . implode ('' , $ additional );
382+ 
383+         $ headers  = static ::prepareHeaders ($ additionalHeaders );
384+ 
385+         $ body  = (object ) [
386+             'data '  => (object ) [
387+                 'type '  => static ::$ type
388+             ]
389+         ];
390+ 
391+         if  ($ attributes  !== null ) {
392+             $ body ->data ->attributes  = $ attributes ;
393+         }
315394
395+         if  ($ relationships  !== null ) {
396+             $ body ->data ->relationships  = $ relationships ->getRelationships ();
397+         }
398+ 
399+         list (
400+             $ responseStatusCode ,
401+             $ responseHeaders ,
402+             $ responseBody
403+         ) = static ::request (
404+             $ url ,
405+             self ::METHOD_PATCH ,
406+             $ headers ,
407+             $ body
408+         );
409+ 
410+         $ resource  = (new  Resource ())->parse (
411+             $ responseBody
412+         );
413+ 
414+         $ resource ->setStatusCode ($ responseStatusCode );
415+         $ resource ->setHeaders ($ responseHeaders );
416+ 
417+         return  $ resource ;
316418    }
317419
318420    public  static  function  delete (
319-         string  $ id
421+         string  $ id ,
422+         \stdClass   $ additionalHeaders  = null ,
423+         string  ...$ additional
320424    ) {
425+         $ API   = self ::getGlobalAPI ();
426+ 
427+         $ url  = $ API   . static ::$ endpoint  . '/ '  . $ id  . '/ ' ;
428+ 
429+         //Append additional 
430+         $ url  = $ url  . implode ('' , $ additional );
431+ 
432+         $ headers  = static ::prepareHeaders ($ additionalHeaders );
321433
434+         list (
435+             $ responseStatusCode ,
436+             $ responseHeaders ,
437+             $ responseBody
438+         ) = static ::request (
439+             $ url ,
440+             self ::METHOD_DELETE ,
441+             $ headers
442+         );
443+ 
444+         $ resource  = (new  Resource ())->parse (
445+             $ responseBody
446+         );
447+ 
448+         $ resource ->setStatusCode ($ responseStatusCode );
449+         $ resource ->setHeaders ($ responseHeaders );
450+ 
451+         return  $ resource ;
322452    }
323453
324454    /** 
325-      * Perform an cURL request 
455+      * Perform an cURL request to a JSON API web service over HTTP 
456+      * @param string         $url Request url 
457+      * @param string         $method Request HTTP method 
458+      * @param \stdClass|null $headers Request headers 
459+      * @param \stdClass|null $data Request body 
460+      * @return array which contains 
461+      * - $responseStatusCode 
462+      * - $responseHeaders 
463+      * - $responseBody (JSON encoded) 
464+      * @throws ResponseException When response code is not on of 200, 201, 202, 203 or 204 
465+      * @throws \Exception 
466+      * @example 
467+      * ```php 
468+      * list( 
469+      *     $responseStatusCode, 
470+      *     $responseHeaders, 
471+      *     $responseBody 
472+      * ) = Client::request( 
473+      *     'http://myapi.com/user/', 
474+      *     Client::METHOD_GET, 
475+      *     (object) [ 
476+      *         'Content-Type' => 'application/vnd.api+json', 
477+      *         'Accept' => 'application/vnd.api+json' 
478+      *     ] 
479+      * ); 
480+      * ``` 
326481     */ 
327482    public  static  function  request (
328483        string  $ url ,
329484        string  $ method  = self ::METHOD_GET ,
330485        \stdClass   $ headers  = null ,
331-         $ data  = null ,
486+         \ stdClass   $ data  = null ,
332487        $ flags  = self ::REQUEST_EMPTY_FLAG ,
333488        //$accept = 'application/json', 
334489        $ encoding  = null 
@@ -337,7 +492,7 @@ public static function request(
337492        //Is the request binary 
338493        $ binary  = ($ flags  & self ::REQUEST_BINARY ) != 0 ;
339494        //If the request parameters form encoded 
340-         $ form_encoded  = !(($ flags  & self ::REQUEST_NOT_URL_ENCODED ) != 0 );
495+         $ form_encoded  = false ; //  !(($flags & self::REQUEST_NOT_URL_ENCODED) != 0);
341496        //Initialize headers 
342497
343498        /*$headers = array( 
@@ -398,7 +553,7 @@ public static function request(
398553                    curl_setopt (
399554                        $ handle ,
400555                        CURLOPT_POSTFIELDS ,
401-                         $ data
556+                         json_encode ( $ data) 
402557                    );
403558                }
404559                break ;
@@ -417,7 +572,7 @@ public static function request(
417572                curl_setopt ($ handle , CURLOPT_CUSTOMREQUEST , self ::METHOD_DELETE );
418573                break ;
419574            default :
420-                 throw  new  Exception ('Unsupported method ' );
575+                 throw  new  \ Exception ('Unsupported method ' );
421576        }
422577
423578        //Get response 
@@ -442,27 +597,24 @@ public static function request(
442597
443598        curl_close ($ handle );
444599
600+         //Throw exception on response failure 
601+         if  (!in_array ($ responseStatusCode , [200 , 201 , 202 , 203 , 204 ])) {
602+             var_dump ($ responseBody );
603+             throw  new  ResponseException (
604+                 (new  Errors ())
605+                     ->parse (json_decode ($ responseBody ) ?? new  \stdClass )
606+                     ->setHeaders ($ responseHeaders )
607+                     ->setStatusCode ($ responseStatusCode )
608+             );
609+         }
610+ 
611+         //Return the data of response 
445612        return  [
446613            $ responseStatusCode ,
447614            $ responseHeaders ,
448615            json_decode ($ responseBody )
449616        ];
450617
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- 
466618        /*return ( 
467619
468620            $accept == 'application/json' ? json_decode($response, true) : $response );*/ 
0 commit comments