<commit_before><?php

namespace DLJField\CompaniesHouse;

use DLJField\CompaniesHouse\Exceptions\InvalidCompanyProfile;
use DLJField\CompaniesHouse\Exceptions\InvalidCompanySearch;
use DLJField\CompaniesHouse\Exceptions\InvalidOfficerList;
use DLJField\CompaniesHouse\Responses\CompanyProfile;
use DLJField\CompaniesHouse\Responses\CompanySearch;
use DLJField\CompaniesHouse\Responses\OfficerList;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Response;

class Client
{
    /**
     * @var GuzzleClient
     */
    private $guzzleClient;

    /**
     * The base URL for the Companies House API
     *
     * @var string
     */
    private $apiBaseUrl = 'https://api.companieshouse.gov.uk';

    /**
     * @var string
     */
    private $apiKey;

    /**
     * @param string $apiKey
     */
    public function __construct($apiKey)
    {
        $this->apiKey = $apiKey;

        $this->setupClient();
    }

    /**
     * Get the company profile for the provided company number
     *
     * https://developer.companieshouse.gov.uk/api/docs/company/company_number/company_number.html
     *
     * @param string $companyNumber
     * @return CompanyProfile
     * @throws InvalidCompanyProfile
     */
    public function getCompanyProfile($companyNumber)
    {
        $response = $this->get('/company/' . $companyNumber);

        if ($response->getStatusCode() != 200) {
            throw new InvalidCompanyProfile($response->getStatusCode(), $response->getReasonPhrase(), $response->getBody()->getContents());
        }

        return CompanyProfile::fromApiResponse($response);
    }

    /**
     * List the company officers
     *
     * https://developer.companieshouse.gov.uk/api/docs/company/company_number/officers/officers.html
     *
     * @param string $companyNumber
     * @param int $itemsPerPage
     * @param int $startIndex
     * @param string $orderBy
     * @return OfficerList
     * @throws InvalidOfficerList
     */
    public function getOfficerList($companyNumber, $itemsPerPage = null, $startIndex = null, $orderBy = null)
    {
        $response = $this->get('/company/' . $companyNumber . '/officers', [
            'query' => [
                'items_per_page' => $itemsPerPage,
                'start_index' => $startIndex,
                'order_by' => $orderBy,
            ],
        ]);

        if ($response->getStatusCode() != 200) {
            throw new InvalidOfficerList($response->getStatusCode(), $response->getReasonPhrase(), $response->getBody()->getContents());
        }

        return OfficerList::fromApiResponse($response);
    }

    /**
     * Search for a company
     *
     * https://developer.companieshouse.gov.uk/api/docs/search/companies/companysearch.html
     *
     * @param string $q
     * @param int $itemsPerPage
     * @param int $startIndex
     * @return CompanySearch
     * @throws InvalidCompanySearch
     */
    public function companySearch($q, $itemsPerPage = null, $startIndex = null)
    {
        $response = $this->get('/search/companies', [
            'query' => [
                'q' => $q,
                'items_per_page' => $itemsPerPage,
                'start_index' => $startIndex,
            ],
        ]);

        if ($response->getStatusCode() != 200) {
            throw new InvalidCompanySearch($response->getStatusCode(), $response->getReasonPhrase(), $response->getBody()->getContents());
        }

        return CompanySearch::fromApiResponse($response);
    }

    /**
     * Perform a GET request on the given resource
     *
     * @param string $resource
     * @param array $headers
     * @return Response
     */
    private function get($resource, array $headers = [])
    {
        try {
            return $this->guzzleClient->request('GET', $resource, ['auth' => [$this->apiKey]] + $headers);
        } catch (ClientException $e) {
            return $e->getResponse();<commit_msg>Fixing issue with auth headers in guzzle<commit_after><?php

namespace DLJField\CompaniesHouse;

use DLJField\CompaniesHouse\Exceptions\InvalidCompanyProfile;
use DLJField\CompaniesHouse\Exceptions\InvalidCompanySearch;
use DLJField\CompaniesHouse\Exceptions\InvalidOfficerList;
use DLJField\CompaniesHouse\Responses\CompanyProfile;
use DLJField\CompaniesHouse\Responses\CompanySearch;
use DLJField\CompaniesHouse\Responses\OfficerList;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Response;

class Client
{
    /**
     * @var GuzzleClient
     */
    private $guzzleClient;

    /**
     * The base URL for the Companies House API
     *
     * @var string
     */
    private $apiBaseUrl = 'https://api.companieshouse.gov.uk';

    /**
     * @var string
     */
    private $apiKey;

    /**
     * @param string $apiKey
     */
    public function __construct($apiKey)
    {
        $this->apiKey = $apiKey;

        $this->setupClient();
    }

    /**
     * Get the company profile for the provided company number
     *
     * https://developer.companieshouse.gov.uk/api/docs/company/company_number/company_number.html
     *
     * @param string $companyNumber
     * @return CompanyProfile
     * @throws InvalidCompanyProfile
     */
    public function getCompanyProfile($companyNumber)
    {
        $response = $this->get('/company/' . $companyNumber);

        if ($response->getStatusCode() != 200) {
            throw new InvalidCompanyProfile($response->getStatusCode(), $response->getReasonPhrase(), $response->getBody()->getContents());
        }

        return CompanyProfile::fromApiResponse($response);
    }

    /**
     * List the company officers
     *
     * https://developer.companieshouse.gov.uk/api/docs/company/company_number/officers/officers.html
     *
     * @param string $companyNumber
     * @param int $itemsPerPage
     * @param int $startIndex
     * @param string $orderBy
     * @return OfficerList
     * @throws InvalidOfficerList
     */
    public function getOfficerList($companyNumber, $itemsPerPage = null, $startIndex = null, $orderBy = null)
    {
        $response = $this->get('/company/' . $companyNumber . '/officers', [
            'query' => [
                'items_per_page' => $itemsPerPage,
                'start_index' => $startIndex,
                'order_by' => $orderBy,
            ],
        ]);

        if ($response->getStatusCode() != 200) {
            throw new InvalidOfficerList($response->getStatusCode(), $response->getReasonPhrase(), $response->getBody()->getContents());
        }

        return OfficerList::fromApiResponse($response);
    }

    /**
     * Search for a company
     *
     * https://developer.companieshouse.gov.uk/api/docs/search/companies/companysearch.html
     *
     * @param string $q
     * @param int $itemsPerPage
     * @param int $startIndex
     * @return CompanySearch
     * @throws InvalidCompanySearch
     */
    public function companySearch($q, $itemsPerPage = null, $startIndex = null)
    {
        $response = $this->get('/search/companies', [
            'query' => [
                'q' => $q,
                'items_per_page' => $itemsPerPage,
                'start_index' => $startIndex,
            ],
        ]);

        if ($response->getStatusCode() != 200) {
            throw new InvalidCompanySearch($response->getStatusCode(), $response->getReasonPhrase(), $response->getBody()->getContents());
        }

        return CompanySearch::fromApiResponse($response);
    }

    /**
     * Perform a GET request on the given resource
     *
     * @param string $resource
     * @param array $headers
     * @return Response
     */
    private function get($resource, array $headers = [])
    {
        try {
            return $this->guzzleClient->request('GET', $resource, ['auth' => [$this->apiKey, '']] + $headers);
        } catch (ClientException $e) {
            return $e->getResponse();
{'fraction_non_alphanumeric': 0.1306425248333962, 'fraction_numerical': 0.002514774299006664, 'mean_word_length': 2.5194690265486726, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 8, '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': '4351929', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2237, 'n_words': 437}