Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
securit opened this issue Sep 21, 2020 · 1 comment

Comments

@securit
Copy link

securit commented Sep 21, 2020

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 securit changed the title Paginated response returns an array object for all response items but the last iteration with it returns as an object Paginated response returns an array object for all response items but the last iteration which it returns as an object Sep 21, 2020
@gnikyt
Copy link
Owner

gnikyt commented Sep 22, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants