<commit_before><?php namespace Todaymade\Daux\Format\Confluence;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ParseException;
use GuzzleHttp\Exception\TransferException;

class Api
{

    protected $base_url;
    protected $user;
    protected $pass;

    protected $space;

    public function __construct($base_url, $user, $pass)
    {
        $this->base_url = $base_url;
        $this->user = $user;
        $this->pass = $pass;
    }

    public function setSpace($space_id)
    {
        $this->space = $space_id;
    }

    protected function getClient()
    {
        $options = [
            'base_url' => $this->base_url . 'rest/api/',
            'defaults' => [
                'auth' => [$this->user, $this->pass]
            ]
        ];

        return new Client($options);
    }

    /**
     * The standard error message from guzzle is quite poor in informations,
     * this will give little bit more sense to it and return it
     *
     * @param BadResponseException $e
     * @return BadResponseException
     */
    protected function handleError(BadResponseException $e)
    {
        $request = $e->getRequest();
        $response = $e->getResponse();

        $level = floor($response->getStatusCode() / 100);

        if ($level == '4') {
            $label = 'Client error response';
        } elseif ($level == '5') {
            $label = 'Server error response';
        } else {
            $label = 'Unsuccessful response';
        }

        $message = $label .
            ' [url] ' . $request->getUrl() .
            ' [status code] ' . $response->getStatusCode() .
            ' [message] ';

        try {
            $message .= $response->json()['message'];
        } catch (ParseException $e) {
            $message .= (string) $response->getBody();
        }

        return new BadResponseException($message, $request, $response, $e->getPrevious());
    }

    /**
     * /rest/api/content/{id}/child/{type}
     *
     * @param $rootPage
     * @return mixed
     */
    public function getList($rootPage)
    {
        $url = "content/$rootPage/child/page?expand=version";

        $pages = [];

        do {
            try {
                $list = $this->getClient()->get($url)->json();
            } catch (BadResponseException $e) {
                throw $this->handleError($e);
            }

            foreach ($list['results'] as $result) {
                $pages[$result['title']] = [
                    "id" => $result['id'],
                    "title" => $result['title'],
                    "version" => $result['version']['number'],
                ];
            }

            if (array_key_exists('next', $list['_links'])) {
                $url = $list['_links']['next'];
            }

        } while (array_key_exists('next', $list['_links']));

        return $pages;
    }

    /**
     * /rest/api/content/{id}/child/{type}<commit_msg>Preload more data in getList<commit_after><?php namespace Todaymade\Daux\Format\Confluence;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ParseException;
use GuzzleHttp\Exception\TransferException;

class Api
{

    protected $base_url;
    protected $user;
    protected $pass;

    protected $space;

    public function __construct($base_url, $user, $pass)
    {
        $this->base_url = $base_url;
        $this->user = $user;
        $this->pass = $pass;
    }

    public function setSpace($space_id)
    {
        $this->space = $space_id;
    }

    protected function getClient()
    {
        $options = [
            'base_url' => $this->base_url . 'rest/api/',
            'defaults' => [
                'auth' => [$this->user, $this->pass]
            ]
        ];

        return new Client($options);
    }

    /**
     * The standard error message from guzzle is quite poor in informations,
     * this will give little bit more sense to it and return it
     *
     * @param BadResponseException $e
     * @return BadResponseException
     */
    protected function handleError(BadResponseException $e)
    {
        $request = $e->getRequest();
        $response = $e->getResponse();

        $level = floor($response->getStatusCode() / 100);

        if ($level == '4') {
            $label = 'Client error response';
        } elseif ($level == '5') {
            $label = 'Server error response';
        } else {
            $label = 'Unsuccessful response';
        }

        $message = $label .
            ' [url] ' . $request->getUrl() .
            ' [status code] ' . $response->getStatusCode() .
            ' [message] ';

        try {
            $message .= $response->json()['message'];
        } catch (ParseException $e) {
            $message .= (string) $response->getBody();
        }

        return new BadResponseException($message, $request, $response, $e->getPrevious());
    }

    /**
     * /rest/api/content/{id}/child/{type}
     *
     * @param $rootPage
     * @return mixed
     */
    public function getList($rootPage)
    {
        //We do a limit of 15 as it appears that confluence has
        //a bug when retrieving more than 20 entries with "body.storage"
        $url = "content/$rootPage/child/page?expand=version,body.storage&limit=15";

        $pages = [];

        do {
            try {
                $list = $this->getClient()->get($url)->json();
            } catch (BadResponseException $e) {
                throw $this->handleError($e);
            }

            foreach ($list['results'] as $result) {
                $pages[$result['title']] = [
                    "id" => $result['id'],
                    "title" => $result['title'],
                    "version" => $result['version']['number'],
                    "content" => $result['body']['storage']['value'],
                ];
            }

            if (array_key_exists('next', $list['_links'])) {
                $url = $list['_links']['next'];
            }

        } while (array_key_exists('next', $list['_links']));

        return $pages;
    }

    /**
     * /rest/api/content/{id}/child/{type}
{'fraction_non_alphanumeric': 0.15275974025974026, 'fraction_numerical': 0.0025974025974025974, 'mean_word_length': 1.8132420091324202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, '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': '2337889', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1726, 'n_words': 373}