src/Client.php
/**
     * Helper method to execute deferred HTTP requests.
     * @param  null  $expectedClass
     * @throws KiotVietException
     */
Helper method to execute deferred HTTP requests.
@param  null  $expectedClass
@throws KiotVietException
Helper method to execute deferred HTTP requests . @param null $expectedClass @throws KiotVietException
public function execute(
        RequestInterface $request,
        $expectedClass = null,
        ?string $collectionClass = null
    ): KiotVietResponse {
        $request = $request
            ->withHeader('Accept', 'application/json')
            ->withHeader('Authorization', 'Bearer ' . $this->getAccessToken())
            ->withHeader('Retailer', $this->getRetailerCode())
            ->withHeader('Content-Type', 'application/json');
        $client = $this->getHttpClient();
        try {
            $response = $client->send($request);
        } catch (BadResponseException $exception) {
            if ($response = $exception->getResponse()) {
                $responseContent = json_decode($response->getBody()->getContents(), true);
                $kvException = new KiotVietException(
                    $responseContent['responseStatus']['message'] ?? $exception->getMessage(),
                    $response->getStatusCode()
                );
                $kvException->setErrorCode($responseContent['responseStatus']['errorCode'] ?? null);
                $kvException->setErrors($responseContent['responseStatus']['errors'] ?? []);
                $kvException->setResponse(new KiotVietResponse($response, $expectedClass, $collectionClass));
                throw $kvException;
            }
        }
        return new KiotVietResponse($response, $expectedClass, $collectionClass);
    }
public function execute ( RequestInterface $ request , $ expectedClass = null , ? string $ collectionClass = null ) : KiotVietResponse { $ request = $ request -> withHeader ( 'Accept' , 'application/json' ) -> withHeader ( 'Authorization' , 'Bearer ' . $ this -> getAccessToken ( ) ) -> withHeader ( 'Retailer' , $ this -> getRetailerCode ( ) ) -> withHeader ( 'Content-Type' , 'application/json' ) ; $ client = $ this -> getHttpClient ( ) ; try { $ response = $ client -> send ( $ request ) ; } catch ( BadResponseException $ exception ) { if ( $ response = $ exception -> getResponse ( ) ) { $ responseContent = json_decode ( $ response -> getBody ( ) -> getContents ( ) , true ) ; $ kvException = new KiotVietException ( $ responseContent [ 'responseStatus' ] [ 'message' ] ?? $ exception -> getMessage ( ) , $ response -> getStatusCode ( ) ) ; $ kvException -> setErrorCode ( $ responseContent [ 'responseStatus' ] [ 'errorCode' ] ?? null ) ; $ kvException -> setErrors ( $ responseContent [ 'responseStatus' ] [ 'errors' ] ?? [ ] ) ; $ kvException -> setResponse ( new KiotVietResponse ( $ response , $ expectedClass , $ collectionClass ) ) ; throw $ kvException ; } } return new KiotVietResponse ( $ response , $ expectedClass , $ collectionClass ) ; }
Helper method to execute deferred HTTP requests.
Helper method to execute deferred HTTP requests .