<commit_before><?php namespace App\Http\Controllers;

use Session;
use GuzzleHttp\Client;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;

class PocketController extends BaseController {

    const POCKET_REQUEST   = 'https://getpocket.com/v3/oauth/request';
    const POCKET_AUTHORIZE = 'https://getpocket.com/auth/authorize';
    const POCKET_OAUTH_AUTHORIZE = 'https://getpocket.com/v3/oauth/authorize';

    const POCKET_SESSION_USERNAME = 'pocket_username';
    const POCKET_SESSION_ACCESS_TOKEN = 'pocket_access_token';


    public function __construct()
    {
        parent::__construct();
    }

    public function login()
    {
        $client = new Client;

        try {
            /** @var \GuzzleHttp\Message\ResponseInterface $response */
            $response = $client->post(self::POCKET_REQUEST, [
                'headers' => ['X-Accept' => 'application/json'],
                'body' => [
                    'consumer_key' => env('POCKET_API'),
                    'redirect_uri' => action('PocketController@loginResponse')
                ]
            ]);
        } catch (RequestException $e) {
            echo $e->getRequest();
            if ($e->hasResponse()) {
                echo $e->getResponse();
            }
        }

        $statusCode = $response->getStatusCode();
        if ($statusCode != 200) {
            if ($statusCode == 400) {
                Log::error('Invalid pocket parameters');

            } elseif($statusCode == 403) {
                Log::error('Pocket invalid consumer key');

            } elseif($statusCode == 500) {
                Log::error('Pocket server issue');
            }
        }

        $json = $response->json();

        if (!isset($json['code'])) {
            Log::error('Pocket should have responded with an error');
        }

        Session::put('pocket_code', $json['code']);

        $params = http_build_query(
            array(
                'request_token' => $json['code'],
                'redirect_uri' => action('PocketController@loginResponse')
            )
        );

        return redirect(self::POCKET_AUTHORIZE . '?' . $params);
    }

    public function loginResponse() {

        $client = new Client;

        try {
            /** @var \GuzzleHttp\Message\ResponseInterface $response */
            $response = $client->post(self::POCKET_OAUTH_AUTHORIZE, [
                'headers' => ['X-Accept' => 'application/json'],
                'body'    => [
                    'consumer_key' => env('POCKET_API'),
                    'code'         => Session::get('pocket_code')
                ]
            ]);
        } catch (RequestException $e) {
            echo $e->getRequest();
            if ($e->hasResponse()) {
                echo $e->getResponse();
            }
        }

        $statusCode = $response->getStatusCode();
        if ($statusCode != 200) {
            if ($statusCode == 400) {
                Log::error('Missing consumer key');

            } elseif ($statusCode == 403) {
                Log::error('Invalid consumer key');

            } elseif ($statusCode == 500) {
                Log::error('Pocket server issue');
            }
        }

        $json = $response->json();

        $this->auth->login($json['username'], $json['access_token']);

        return redirect('/dashboard');
    }

    /**
     * Logout by resetting session and redirecting to home
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout() {
        $this->auth->logout();
        return redirect('/');
    }
}<commit_msg>This will be slow but synchronise items upon login so that we don't wait an hour for cron.<commit_after><?php namespace App\Http\Controllers;

use Session;
use GuzzleHttp\Client;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use \App\Services\Pocket;

class PocketController extends BaseController {

    const POCKET_REQUEST   = 'https://getpocket.com/v3/oauth/request';
    const POCKET_AUTHORIZE = 'https://getpocket.com/auth/authorize';
    const POCKET_OAUTH_AUTHORIZE = 'https://getpocket.com/v3/oauth/authorize';

    const POCKET_SESSION_USERNAME = 'pocket_username';
    const POCKET_SESSION_ACCESS_TOKEN = 'pocket_access_token';


    public function __construct()
    {
        parent::__construct();
    }

    public function login()
    {
        $client = new Client;

        try {
            /** @var \GuzzleHttp\Message\ResponseInterface $response */
            $response = $client->post(self::POCKET_REQUEST, [
                'headers' => ['X-Accept' => 'application/json'],
                'body' => [
                    'consumer_key' => env('POCKET_API'),
                    'redirect_uri' => action('PocketController@loginResponse')
                ]
            ]);
        } catch (RequestException $e) {
            echo $e->getRequest();
            if ($e->hasResponse()) {
                echo $e->getResponse();
            }
        }

        $statusCode = $response->getStatusCode();
        if ($statusCode != 200) {
            if ($statusCode == 400) {
                Log::error('Invalid pocket parameters');

            } elseif($statusCode == 403) {
                Log::error('Pocket invalid consumer key');

            } elseif($statusCode == 500) {
                Log::error('Pocket server issue');
            }
        }

        $json = $response->json();

        if (!isset($json['code'])) {
            Log::error('Pocket should have responded with an error');
        }

        Session::put('pocket_code', $json['code']);

        $params = http_build_query(
            array(
                'request_token' => $json['code'],
                'redirect_uri' => action('PocketController@loginResponse')
            )
        );

        return redirect(self::POCKET_AUTHORIZE . '?' . $params);
    }

    public function loginResponse() {

        $client = new Client;

        try {
            /** @var \GuzzleHttp\Message\ResponseInterface $response */
            $response = $client->post(self::POCKET_OAUTH_AUTHORIZE, [
                'headers' => ['X-Accept' => 'application/json'],
                'body'    => [
                    'consumer_key' => env('POCKET_API'),
                    'code'         => Session::get('pocket_code')
                ]
            ]);
        } catch (RequestException $e) {
            echo $e->getRequest();
            if ($e->hasResponse()) {
                echo $e->getResponse();
            }
        }

        $statusCode = $response->getStatusCode();
        if ($statusCode != 200) {
            if ($statusCode == 400) {
                Log::error('Missing consumer key');

            } elseif ($statusCode == 403) {
                Log::error('Invalid consumer key');

            } elseif ($statusCode == 500) {
                Log::error('Pocket server issue');
            }
        }

        $json = $response->json();

        $this->auth->login($json['username'], $json['access_token']);

        $pocket = new Pocket($this->auth->getUser());
        $pocket->synchronisePocketItems();

        return redirect('/dashboard');
    }

    /**
     * Logout by resetting session and redirecting to home
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout() {
        $this->auth->logout();
        return redirect('/');
    }
}
{'fraction_non_alphanumeric': 0.14184963406520293, 'fraction_numerical': 0.006919494344644045, 'mean_word_length': 1.850208570345089, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, 'https://': 6, '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': '3212431', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2000, 'n_words': 387}