<commit_before><?php

namespace Instagram\Http\Client;

use Exception;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use Instagram\Http\Response;

/**
 * Instagram client class.
 *
 * @package    Instagram
 * @author     <NAME> <<EMAIL>>
 * @link       https://github.com/hassankhan/instagram-sdk
 * @license    MIT
 */
final class GuzzleAdapter implements AdapterInterface
{
    /**
     * The Guzzle client instance.
     *
     * @var ClientInterface
     */
    protected $guzzle;

    /**
     * Creates a new instance of `GuzzleAdapter`.
     *
     * @param ClientInterface $guzzle
     */
    public function __construct(ClientInterface $guzzle)
    {
        $this->guzzle = $guzzle;
    }

    /**
     * @inheritDoc
     */
    public function request($method, $uri, array $parameters = [])
    {
        try {
            $response = $this->guzzle->$method($uri, $parameters);
        } catch (ClientException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }

        return Response::createFromResponse(json_decode($response->getBody()->getContents(), true));
    }

    /**
     * @inheritDoc
     */
    public function paginate(Response $response, $limit = null)
    {
        // If there's nothing to paginate, return response as-is
        if (!$response->hasPages()) {
            return $response;
        }

        // Set `$count`, save the response data and get the next URL
        // @TODO: Remove $count
        $count         = 0;
        $responseStack = [$response->get()];
        $nextUrl       = $response->nextUrl();

        // If we run out of pages OR reach `$limit`, then stop and return response
        while($nextUrl !== null || ($limit !== null && $count === $limit)) {
            $nextResponseJson = $this->guzzle->get($nextUrl)->getBody()->getContents();
//            $nextResponseJson = $this->request('GET', $nextUrl)->getBody()->getContents();
            $nextResponse     = Response::createFromResponse(json_decode($nextResponseJson, true));

            $responseStack[] = $nextResponse->get();
            $nextUrl         = $nextResponse->nextUrl();
            $count++;
        }

        return new Response($response->getRaw()['meta'], array_flatten($responseStack, 1));
    }
}<commit_msg>Refactored `GuzzleAdapter::paginate()`<commit_after><?php

namespace Instagram\Http\Client;

use Exception;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use Instagram\Http\Response;

/**
 * Instagram client class.
 *
 * @package    Instagram
 * @author     <NAME> <<EMAIL>>
 * @link       https://github.com/hassankhan/instagram-sdk
 * @license    MIT
 */
final class GuzzleAdapter implements AdapterInterface
{
    /**
     * The Guzzle client instance.
     *
     * @var ClientInterface
     */
    protected $guzzle;

    /**
     * Creates a new instance of `GuzzleAdapter`.
     *
     * @param ClientInterface $guzzle
     */
    public function __construct(ClientInterface $guzzle)
    {
        $this->guzzle = $guzzle;
    }

    /**
     * @inheritDoc
     */
    public function request($method, $uri, array $parameters = [])
    {
        try {
            $response = $this->guzzle->$method($uri, $parameters);
        } catch (ClientException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }

        return Response::createFromResponse(json_decode($response->getBody()->getContents(), true));
    }

    /**
     * @inheritDoc
     */
    public function paginate(Response $response, $limit = null)
    {
        // If there's nothing to paginate, return response as-is
        if (!$response->hasPages()) {
            return $response;
        }

        // Set `$count`, save the response data and get the next URL
        // @TODO: Remove $count
        $responseStack = [$response->get()];
        $nextUrl       = $response->nextUrl();

        // If we run out of pages OR reach `$limit`, then stop and return response
        while($nextUrl !== null || ($limit !== null && count($responseStack) <= $limit)) {
            $nextResponse    = $this->request('GET', $nextUrl);
            $responseStack[] = $nextResponse->get();
            $nextUrl         = $nextResponse->nextUrl();
        }

        return new Response($response->getRaw()['meta'], array_flatten($responseStack, 1));
    }
}
{'fraction_non_alphanumeric': 0.14243592651394874, 'fraction_numerical': 0.0006804264005443411, 'mean_word_length': 2.269088213491475, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 39, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7038256', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1221, 'n_words': 297}