gnikyt/Basic-Shopify-API

For query strings other than GET methods in the Admin REST API

inaryu opened this issue · 1 comments

Hi.

First off, let me tell you something. I use a translation app because I'm not very good at English. sorry.

I am currently using laravel-shopify.
Thanks!

Now, about this issue, the Admin REST API has an API that requires a query string in addition to the GET method.

https://shopify.dev/docs/admin-api/rest/reference/customers/customer-address#set-2020-07
https://shopify.dev/docs/admin-api/rest/reference/online-store/asset#destroy-2020-07

When using the above API, we expect many people to write the following.

$query = ['asset[key]' => 'snippets/sample.liquid'];
$res = $api->rest('DELETE', "/admin/api/2020-07/themes/1234567/assets.json", $query);

// or

$res = $api->rest('DELETE', "/admin/api/2020-07/themes/1234567/assets.json?" . http_build_query($query));

However, if you write as above, the response will be an error.

var_export($response['errors']); // true
var_export($response['body']); // Not Found

The reason for the error response is that the request method does not correspond to a query string.

// src/Osiset/BasicShopifyAPI/Clients/Rest.php
public function request(string $type, string $path, array $params = null, array $headers = [], bool $sync = true)
{
    // Build URI
    $uri = $this->getBaseUri()->withPath($path);

    // Build the request parameters for Guzzle
    $guzzleParams = [];
    if ($params !== null) {
        $guzzleParams[strtoupper($type) === 'GET' ? 'query' : 'json'] = $params; // <- This is the reason.
    }
...
}

This is how I am currently dealing with it.

$query = ['query' => ['asset[key]' => 'snippets/sample.liquid']];
$res = $api->rest('DELETE', "/admin/api/2020-07/themes/1234567/assets.json", $query);

// and

$query = ['json' => ['asset' => ['key' => 'snippets/sample.liquid', 'value' => 'test']]];
$res = $api->rest('PUT', "/admin/api/2020-07/themes/1234567/assets.json", $query);
// src/Osiset/BasicShopifyAPI/Clients/Rest.php
public function request(string $type, string $path, array $params = null, array $headers = [], bool $sync = true)
{
    // Build URI
    $uri = $this->getBaseUri()->withPath($path);

    // Build the request parameters for Guzzle
    // $guzzleParams = [];
    // if ($params !== null) {
    //     $guzzleParams[strtoupper($type) === 'GET' ? 'query' : 'json'] = $params;
    // }
    $guzzleParams = $params ?? [];
...
}

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • Package Version: v9.1.2
  • laravel-shopify Package Version: v12.1.0
  • Laravel Version: v6.18.34
  • PHP Version: v7.3.5
  • Using a toolset (Docker, Laradock, Vagrant, etc.): Vagrant

Let me know if you know of any other good ways to do this. Thank you for your help.

Makes sense, I have implemented a fix for this.

https://github.com/osiset/Basic-Shopify-API#overriding-request-type

Reopen if you experience issues.