Skip to content

Commit

Permalink
Adding support for REST resources
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Feb 18, 2022
1 parent 3d9c5c6 commit 05ed892
Show file tree
Hide file tree
Showing 15 changed files with 1,159 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ jobs:

- name: Run tests
run: composer test

# Enable this after adding the resources
# - name: Run REST resource tests
# run: composer test_rest_resources
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

### Added

- [#139](https://github.com/Shopify/shopify-php-api/pull/139) Add support for REST resources
- [#134](https://github.com/Shopify/shopify-php-api/pull/134) ⚠️ [Breaking] Add support for PHP 8.1 and remove 7.3 from the supported list, since it's no longer supported
- [#136](https://github.com/Shopify/shopify-php-api/pull/136) Allow full paths in REST requests

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@
"psr/log": "^1.1",
"firebase/php-jwt": "^5.2",
"psr/http-client": "^1.0",
"guzzlehttp/guzzle": "^7.0"
"guzzlehttp/guzzle": "^7.0",
"doctrine/inflector": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3.6",
"mikey179/vfsstream": "^1.6"
},
"scripts": {
"test": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --color",
"test": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --color --testsuite default",
"test_rest_resources": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --color --testsuite rest_resources",
"lint": "./vendor/bin/phpcs --standard=PSR12 src tests"
}
}
93 changes: 92 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions docs/usage/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ You can read our [REST Admin API](https://shopify.dev/docs/admin-api/getting-sta

## Making your first REST request

REST Admin API endpoints are organized by [resource](https://shopify.dev/docs/admin-api/rest/reference#selecting-apis-for-your-app) . You'll need to use different API endpoints depending on the service that your app provides.
REST Admin API endpoints are organized by [resource](https://shopify.dev/docs/admin-api/rest/reference#selecting-apis-for-your-app) . You'll need to use different API endpoints depending on the service that your app provides. There are two different ways of doing that with this library:
* [REST resources](#rest-resources)
* [REST client](#rest-client)

REST uses `get`, `post`, `put`, and `delete` requests to retrieve, create, update, and delete resources respectively.
### REST resources

REST resources are objects that represent the REST endpoints in the Admin API. This library provides classes for the endpoints in all supported versions of the API. We don't provide classes for the `unstable` version because it may change at any point, which would cause the resource classes to become outdated.

The resource classes will provide methods for all endpoints described in [the REST reference docs](https://shopify.dev/api/admin-rest). Please see the references for how to use a specific resource.

### REST client

The REST client uses `get`, `post`, `put`, and `delete` requests to retrieve, create, update, and delete resources respectively.

| Parameter | Type | Required? | Default Value | Notes |
|:----------|:----------------|:---------:|:----------------:|:-------------------------------------------------|
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
<exclude>tests/Rest/Admin*</exclude>
</testsuite>
<testsuite name="rest_resources">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

Expand Down
4 changes: 3 additions & 1 deletion src/Clients/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,13 @@ protected function request(

$client = Context::$HTTP_CLIENT_FACTORY->client();

$query = preg_replace("/%5B[0-9]+%5D/", "%5B%5D", http_build_query($query));

$url = (new Uri())
->withScheme('https')
->withHost($this->domain)
->withPath($this->getRequestPath($path))
->withQuery(http_build_query($query));
->withQuery($query);

$request = new Request($method, $url, $headers);
$request = $request->withHeader(HttpHeaders::USER_AGENT, implode(' | ', $userAgentParts));
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/RestResourceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Shopify\Exception;

class RestResourceException extends ShopifyException
{
}
21 changes: 21 additions & 0 deletions src/Exception/RestResourceRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Shopify\Exception;

class RestResourceRequestException extends ShopifyException
{
private int $statusCode;

public function __construct($message, $statusCode)
{
parent::__construct($message);
$this->statusCode = $statusCode;
}

public function getStatusCode(): int
{
return $this->statusCode;
}
}
Loading

0 comments on commit 05ed892

Please # to comment.