From 598e94fbc298b33e0427defdee510186c404c148 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Thu, 1 Oct 2020 10:49:33 +0200 Subject: [PATCH 1/4] Add custom endpoints --- src/Api/Custom.php | 41 +++++++++++++++++++++++++++++++++++++++++ src/Magento.php | 14 +++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/Api/Custom.php diff --git a/src/Api/Custom.php b/src/Api/Custom.php new file mode 100644 index 0000000..0290f0d --- /dev/null +++ b/src/Api/Custom.php @@ -0,0 +1,41 @@ +endpoint = $endpoint; + + parent::__construct($magento); + } + + /** + * Dynamic call to passthrough. + * + * @param string $method + * @param array $args + * @return mixed + */ + public function __call($method, $args) + { + if ($method == 'get' || $method == 'post') { + $args[0] = rtrim($this->endpoint, '/') . '/' . $args[0]; + } + + return call_user_func_array([$this, $method], $args); + } +} diff --git a/src/Magento.php b/src/Magento.php index 03a8e3c..9eebb36 100644 --- a/src/Magento.php +++ b/src/Magento.php @@ -2,6 +2,7 @@ namespace Grayloon\Magento; +use Grayloon\Magento\Api\Custom; use Illuminate\Support\Str; use InvalidArgumentException; @@ -49,6 +50,12 @@ class Magento */ public $storeCode; + /** + * Magento constructor. + * + * @param string $baseUrl + * @param string $token + */ public function __construct($baseUrl = null, $token = null) { $this->baseUrl = $baseUrl ?: config('magento.base_url'); @@ -68,12 +75,13 @@ public function __construct($baseUrl = null, $token = null) */ public function api($name) { - $apiMethodExists = class_exists($name = "\Grayloon\Magento\Api\\".Str::ucfirst($name)); + $className = $name; + $apiMethodExists = class_exists($className = "\Grayloon\Magento\Api\\".Str::ucfirst($className)); if (! $apiMethodExists) { - throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name)); + return new Custom($name, $this); } - return new $name($this); + return new $className($this); } } From 4f85fca154d51a78f335664be34be60d2940c067 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Thu, 1 Oct 2020 10:53:18 +0200 Subject: [PATCH 2/4] StyleCI --- src/Api/Custom.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Api/Custom.php b/src/Api/Custom.php index 0290f0d..a28f819 100644 --- a/src/Api/Custom.php +++ b/src/Api/Custom.php @@ -13,7 +13,7 @@ class Custom extends AbstractApi /** * Custom constructor. - * + * * @param string $endpoint */ public function __construct(string $endpoint, Magento $magento) @@ -26,14 +26,14 @@ public function __construct(string $endpoint, Magento $magento) /** * Dynamic call to passthrough. * - * @param string $method - * @param array $args + * @param string $method + * @param array $args * @return mixed */ public function __call($method, $args) { if ($method == 'get' || $method == 'post') { - $args[0] = rtrim($this->endpoint, '/') . '/' . $args[0]; + $args[0] = rtrim($this->endpoint, '/').'/'.$args[0]; } return call_user_func_array([$this, $method], $args); From b7f313c8be04583be23fbfc97db49c3148fcc596 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Thu, 1 Oct 2020 11:19:38 +0200 Subject: [PATCH 3/4] Update readme --- README.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index adc8063..be4a6f7 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ A Magento 2 API Object Oriented wrapper for a Laravel application. - [Products](#products) - [Schema](#schema) - [Source Items](#source-items) + - [Custom modules](#custom modules) ## Installation @@ -223,6 +224,25 @@ Get info about a product by the product SKU: $magento->api('products')->show($sku); ``` +### Custom modules +Magento modules can have their own API endpoints. +For example: +```xml + + ... + + + ... + +``` +To use these you can directly use get/post methods: +```php +$magento->api('custom')->post('save', [...]); +``` +```php +$magento->api('custom')->get('get/1'); +``` + ### Schema @@ -265,4 +285,4 @@ If you discover any security related issues, please email webmaster@grayloon.com ## License -The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. From dfa59cb65f7bc95c8a5c63264fe6701583674730 Mon Sep 17 00:00:00 2001 From: ahinkle Date: Thu, 1 Oct 2020 06:37:05 -0500 Subject: [PATCH 4/4] Add CustomTest; update docs --- README.md | 8 +++---- tests/Api/CustomTest.php | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/Api/CustomTest.php diff --git a/README.md b/README.md index be4a6f7..4093c18 100644 --- a/README.md +++ b/README.md @@ -228,19 +228,19 @@ $magento->api('products')->show($sku); Magento modules can have their own API endpoints. For example: ```xml - + ... - + ... ``` To use these you can directly use get/post methods: ```php -$magento->api('custom')->post('save', [...]); +$magento->api('my-custom-endpoint')->post('save', [...]); ``` ```php -$magento->api('custom')->get('get/1'); +$magento->api('my-custom-endpoint')->get('get/1'); ``` diff --git a/tests/Api/CustomTest.php b/tests/Api/CustomTest.php new file mode 100644 index 0000000..8d2f3f2 --- /dev/null +++ b/tests/Api/CustomTest.php @@ -0,0 +1,47 @@ + Http::response([], 200), + ]); + + $magento = new Magento(); + $customApi = $magento->api('/foo')->get('bar'); + + $this->assertTrue($customApi->ok()); + } + + public function test_can_post_custom_endpoint() + { + Http::fake([ + '*rest/all/V1/foo/bar' => Http::response([], 200), + ]); + + $magento = new Magento(); + $customApi = $magento->api('/foo')->post('bar'); + + $this->assertTrue($customApi->ok()); + } + + public function test_custom_post_endpoint_passes_params() + { + Http::fake([ + '*rest/all/V1/foo/bar' => Http::response([], 200, ['baz' => 'test']), + ]); + + $magento = new Magento(); + $customApi = $magento->api('/foo')->post('bar', [ + 'baz' => 'test', + ]); + + $this->assertTrue($customApi->ok()); + } +}