Basic-Shopify-API icon indicating copy to clipboard operation
Basic-Shopify-API copied to clipboard

Paginated response returns an array object for all response items but the last iteration which it returns as an object

Open securit opened this issue 5 years ago • 1 comments

Using the code below to test, I have noted that when paginating responses from API, all but the last returned response is an array. The final response returns as an object.

public function list(string $resource_path, array $params = [], array $headers = [], $sync = true): ?Collection
    {
        $get = $this->api->rest('GET', $this->endpoint . $resource_path . '.json', $params, $headers, $sync);

        if ($get['errors']) {
            Log::error($get["exception"]->getMessage());

            return null;
        }

        $resource = strrpos($resource_path, '/') ? substr($resource_path, strrpos($resource_path, '/') + 1) : $resource_path;
        $response = collect($get["body"][$resource] ?? $get["body"][Str::singular($resource)]);

        while (isset($get['link']['next'])) {
            $get = $this->api->rest('GET', $this->endpoint . $resource_path . '.json',
                array_filter([
                    'page_info' => $get['link']['next'],
                    'limit'     => $params['limit'] ?? null,
                    'fields'    => $params['fields'] ?? null,
                ]),
                $headers,
                $sync);

            if ($get['errors']) {
                Log::error($get["exception"]->getMessage());
            } else {
                $concat = $get["body"][$resource] ?? $get["body"][Str::singular($resource)];
                dump(is_array($concat)?'is an array' :'');
                dump(is_object($concat)? 'is an object ': '');
                $response = $response->concat($concat);
            }
            unset($page_params);
        }

        return $response;
    }

securit avatar Sep 21 '20 06:09 securit

Can be more clear on what you mean by all are an array and the last is an object?

gnikyt avatar Sep 22 '20 15:09 gnikyt