<commit_before><?php
declare(strict_types=1);

namespace WyriHaximus\Github\Resource;

use Psr\Http\Message\ResponseInterface;
use Rx\Observable;
use Rx\ObserverInterface;
use Rx\SchedulerInterface;
use WyriHaximus\ApiClient\Transport\Client;

trait PagingTrait
{
    abstract function getTransport(): Client;

    protected function iteratePages(string $path): Observable\AnonymousObservable
    {
        return Observable::create(function (
            ObserverInterface $observer,
            SchedulerInterface $scheduler
        ) use ($path) {
            $this->getTransport()->
                requestRaw($path)->
                then(function($response) use ($observer) {
                    $this->handleResponse($response, $observer);
                })
            ;
        });
    }

    private function handleResponse(ResponseInterface $response, ObserverInterface $observer)
    {
        if (!$response->hasHeader('link')) {
            $this->handleResponseContents($response, $observer);
            $observer->onCompleted();
            return;
        }

        $links = [
            'next' => false,
            'last' => false,
        ];
        foreach (explode(', ', $response->getHeader('link')[0]) as $link) {
            list($url, $rel) = explode('>; rel="', ltrim(rtrim($link, '"'), '<'));
            if (isset($links[$rel])) {
                $links[$rel] = $url;
            }
        }

        if ($links['next'] === false || $links['last'] === false) {
            $this->handleResponseContents($response, $observer);
            $observer->onCompleted();
            return;
        }

        if ($links['next'] == $links['last']) {
            $this->handleResponseContents($response, $observer);
            $observer->onCompleted();
            return;
        }

        list(, $path) = explode($this->getTransport()->getBaseURL(), $links['next']);

        $this->getTransport()->
            requestRaw($path)->
            then(function($response) use ($observer) {
                $this->handleResponse($response, $observer);
            })
        ;

        $this->handleResponseContents($response, $observer);
    }

    private function handleResponseContents(ResponseInterface $response, ObserverInterface $observer)
    {
        $observer->onNext(json_decode($response->getBody()->getContents(), true));
    }
}
<commit_msg>Using the transports method to decode json<commit_after><?php
declare(strict_types=1);

namespace WyriHaximus\Github\Resource;

use Psr\Http\Message\ResponseInterface;
use Rx\Observable;
use Rx\ObserverInterface;
use Rx\SchedulerInterface;
use WyriHaximus\ApiClient\Transport\Client;

trait PagingTrait
{
    abstract function getTransport(): Client;

    protected function iteratePages(string $path): Observable\AnonymousObservable
    {
        return Observable::create(function (
            ObserverInterface $observer,
            SchedulerInterface $scheduler
        ) use ($path) {
            $this->getTransport()->
                requestRaw($path)->
                then(function($response) use ($observer) {
                    $this->handleResponse($response, $observer);
                })
            ;
        });
    }

    private function handleResponse(ResponseInterface $response, ObserverInterface $observer)
    {
        if (!$response->hasHeader('link')) {
            $this->handleResponseContents($response, $observer);
            $observer->onCompleted();
            return;
        }

        $links = [
            'next' => false,
            'last' => false,
        ];
        foreach (explode(', ', $response->getHeader('link')[0]) as $link) {
            list($url, $rel) = explode('>; rel="', ltrim(rtrim($link, '"'), '<'));
            if (isset($links[$rel])) {
                $links[$rel] = $url;
            }
        }

        if ($links['next'] === false || $links['last'] === false) {
            $this->handleResponseContents($response, $observer);
            $observer->onCompleted();
            return;
        }

        if ($links['next'] == $links['last']) {
            $this->handleResponseContents($response, $observer);
            $observer->onCompleted();
            return;
        }

        list(, $path) = explode($this->getTransport()->getBaseURL(), $links['next']);

        $this->getTransport()->
            requestRaw($path)->
            then(function($response) use ($observer) {
                $this->handleResponse($response, $observer);
            })
        ;

        $this->handleResponseContents($response, $observer);
    }

    private function handleResponseContents(ResponseInterface $response, ObserverInterface $observer)
    {
        $this->getTransport()->
            jsonDecode($response->getBody()->getContents())->
            then(function ($json) use ($observer) {
                $observer->onNext($json);
            })
        ;
    }
}

{'fraction_non_alphanumeric': 0.1507211050172659, 'fraction_numerical': 0.0008125126955108674, 'mean_word_length': 2.1891191709844557, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 56, 'https://': 0, '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': '7632175', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1246, 'n_words': 227}