Skip to content

Commit 7b614dd

Browse files
committed
Add Error and Error source class, throw ResponseException on non 20x response
1 parent af9f447 commit 7b614dd

File tree

9 files changed

+291
-34
lines changed

9 files changed

+291
-34
lines changed

src/Client.php

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
*/
1717
namespace Phramework\JSONAPI\Client;
1818

19-
use Phramework\JSONAPI\Client\Exceptions\Exception;
19+
use Phramework\JSONAPI\Client\Exceptions\ResponseException;
2020
use Phramework\JSONAPI\Client\Response\Collection;
21+
use Phramework\JSONAPI\Client\Response\Errors;
2122
use 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 );*/

src/Error.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Xenofon Spafaridis
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Phramework\JSONAPI\Client;
18+
19+
/**
20+
* Error meta-class helpful for documentation and autocomplete
21+
* @author Xenofon Spafaridis <[email protected]>
22+
* @since 0.0.0
23+
* @link http://jsonapi.org/format/#error-objects
24+
*/
25+
class Error
26+
{
27+
/**
28+
* The HTTP status code applicable to this problem, expressed as a
29+
* string value
30+
* @var int
31+
*/
32+
public $status;
33+
34+
/**
35+
* A short, human-readable summary of the problem
36+
* @var string
37+
*/
38+
public $title;
39+
40+
/**
41+
* A human-readable explanation specific to this occurrence of the problem
42+
* @var string
43+
*/
44+
public $detail;
45+
46+
/**
47+
* A unique identifier for this particular occurrence of the problem
48+
* @var string
49+
*/
50+
public $id;
51+
52+
/**
53+
* An object containing references to the source of the error
54+
* **Note** usually it's not set
55+
* @var ErrorSource
56+
*/
57+
public $source;
58+
59+
/**
60+
* A links object
61+
* **Note** usually it's not set
62+
* @var \stdClass
63+
*/
64+
public $links;
65+
66+
/**
67+
* A meta object containing non-standard meta-information about the error
68+
* **Note** usually it's not set
69+
* @var \stdClass
70+
*/
71+
public $meta;
72+
73+
/**
74+
* An application-specific error code, expressed as a string value.
75+
* **Note** usually it's not set
76+
* @var string
77+
*/
78+
public $code;
79+
}

src/ErrorSource.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Xenofon Spafaridis
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Phramework\JSONAPI\Client;
18+
19+
/**
20+
* Error source meta-class helpful for documentation and autocomplete
21+
* @author Xenofon Spafaridis <[email protected]>
22+
* @since 0.0.0
23+
* @link http://jsonapi.org/format/#error-objects
24+
*/
25+
class ErrorSource
26+
{
27+
/**
28+
* A a JSON Pointer [RFC6901] to the associated entity in the request
29+
* document [e.g. "/data" for a primary data object, or
30+
* "/data/attributes/title" for a specific attribute]
31+
* @var string
32+
*/
33+
public $pointer;
34+
35+
/**
36+
* A string indicating which URI query parameter caused the error
37+
* @var string
38+
*/
39+
public $parameter;
40+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Xenofon Spafaridis
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Phramework\JSONAPI\Client\Exceptions;
18+
19+
use Phramework\JSONAPI\Client\Response\Errors;
20+
21+
/**
22+
* @author Xenofon Spafaridis <[email protected]>
23+
* @since 0.0.0
24+
*/
25+
class ResponseException extends \Exception
26+
{
27+
/**
28+
* @var Errors
29+
*/
30+
private $response;
31+
32+
/**
33+
* @param Errors $response
34+
*/
35+
public function __construct(Errors $response)
36+
{
37+
$this->response = $response;
38+
}
39+
40+
41+
/**
42+
* @return Errors
43+
*/
44+
public function getResponse()
45+
{
46+
return $this->response;
47+
}
48+
49+
/**
50+
* @param Errors $response
51+
* @return $this
52+
*/
53+
public function setResponse($response)
54+
{
55+
$this->response = $response;
56+
57+
return $this;
58+
}
59+
}

src/Response/Collection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
class Collection extends Response
2626
{
2727
/**
28+
* An array of resource objects, an array of resource identifier objects,
29+
* or an empty array
2830
* @var ResourceObject[]
2931
*/
30-
public $data;
32+
public $data = [];
3133

3234
/**
35+
* Compound Documents
3336
* @var ResourceObject[]
37+
* @link http://jsonapi.org/format/#document-compound-documents
3438
*/
3539
public $included;
3640
}

src/Response/Errors.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616
*/
1717
namespace Phramework\JSONAPI\Client\Response;
1818

19+
use Phramework\JSONAPI\Client\Error;
20+
1921
/**
2022
* @author Xenofon Spafaridis <[email protected]>
2123
* @since 0.0.0
24+
* @link http://jsonapi.org/format/#error-objects
2225
*/
2326
class Errors extends Response
2427
{
2528
/**
26-
* @var \stdClass[]
27-
* @todo add errors object
29+
* @var Error[]
2830
*/
2931
public $errors;
3032
}

0 commit comments

Comments
 (0)