src/BaseConnector.php
public function execute( $method, $url, array $data = [], array $headers = [] ) {
        // Define the response
        $this->response = null;
        $response       = null;
        $bodyType       = 'form_params';
        $headers        = $this->sanitizeHeaders($headers);

        // String replace the base url to prevent double baseUri
        $url = str_replace( $this->getBaseUri(), '', $url );

        $cacheKey = hash( 'sha512', $method . $url . md5( json_encode( $data ) ) . md5( json_encode( $headers ) ) );

        if ($this->hasCache($cacheKey)) {
            $response = $this->getCache($cacheKey);
            $this->setResponse( $response );
            $this->response->setCacheHit( true );
            return true;
        }

        // Check headers for Content Type
        if ( isset($headers['content-type']) && $headers['content-type'] === 'application/json' ) {
            $bodyType = 'json';
        }

        $this->log( 'Start ' . __CLASS__ . ' GuzzleRequest: method(' . $method . '), url(' . $url . '), ' , 'debug');


        try {
            // Do the actual request with Guzzle PSR7
            switch ( strtolower( $method ) ) {
                default:
                case 'get':
                    $response = $this->curlClient->request( strtoupper( $method ), $url, [
                        'headers' => $headers,
                    ] );
                    break;
                case 'put':
                case 'post':
                    $response = $this->curlClient->request( strtoupper( $method ), $url, [
                        $bodyType => $data,
                        'headers' => $headers,
                    ] );
                    break;
            }

            $validResponse = $this->checkValidResponse( $response->getStatusCode() );
        }
        catch ( ClientException $e ) {
            $validResponse = $this->checkValidResponse( $e->getCode() );
            if ( $validResponse === false ) {
                $this->log( $e->getFile().':'.$e->getLine().' Client Exception ' . $e->getMessage().' - '.$e->getCode() , 'error');
                throw new \RuntimeException( 'Client Exception ' . $e->getMessage(), $e->getCode() );
            }
            if ( method_exists( $e, 'getResponse' ) ) {
                $response = $e->getResponse();
            }
        }
        catch ( \Exception $e ) {
            $validResponse = $this->checkValidResponse( $e->getCode() );
            if ( $validResponse === false ) {
                $this->log( $e->getFile().':'.$e->getLine().' Client Exception ' . $e->getMessage().' - '.$e->getCode() , 'error');
                throw new \RuntimeException( 'Exception ' . $e->getMessage(), $e->getCode() );
            }
            if ( method_exists( $e, 'getResponse' ) ) {
                $response = $e->getResponse();
            }
        }

        // Write the response to the cache
        $this->setCache($cacheKey, $response, $this->getCacheTime());

        // Write down the response
        $this->setResponse( $response );


        // When the response is not valid return a
        if ( $validResponse === false ) {
            // the response is not valid this can be a configuration issue or the API is not responding well.
            // Base line is that we need to return a "500: Internal Server Error" with as a error the original message from the API
            $this->log( 'HTTP exception, got a ' . $response->getStatusCode() . ' status, expect only one of these (' . implode( ',', $this->getValidStatusCodes() ) . ')', 'error');
            throw new \RuntimeException( 'HTTP exception, got a ' . $response->getStatusCode() . ' status, expect only one of these (' . implode( ',', $this->getValidStatusCodes() ) . ')', $response->getStatusCode() );
        }

        return true;
    }
public function execute( $method, $url, array $data = [], array $headers = [] ) {
        // Define the response
        $this->response = null;
        $response       = null;
        $bodyType       = 'form_params';
        $headers        = $this->sanitizeHeaders($headers);

        // String replace the base url to prevent double baseUri
        $url = str_replace( $this->getBaseUri(), '', $url );

        $cacheKey = hash( 'sha512', $method . $url . md5( json_encode( $data ) ) . md5( json_encode( $headers ) ) );

        if ($this->hasCache($cacheKey)) {
            $response = $this->getCache($cacheKey);
            $this->setResponse( $response );
            $this->response->setCacheHit( true );
            return true;
        }

        // Check headers for Content Type
        if ( isset($headers['content-type']) && $headers['content-type'] === 'application/json' ) {
            $bodyType = 'json';
        }

        $this->log( 'Start ' . __CLASS__ . ' GuzzleRequest: method(' . $method . '), url(' . $url . '), ' , 'debug');


        try {
            // Do the actual request with Guzzle PSR7
            switch ( strtolower( $method ) ) {
                default:
                case 'get':
                    $response = $this->curlClient->request( strtoupper( $method ), $url, [
                        'headers' => $headers,
                    ] );
                    break;
                case 'put':
                case 'post':
                    $response = $this->curlClient->request( strtoupper( $method ), $url, [
                        $bodyType => $data,
                        'headers' => $headers,
                    ] );
                    break;
            }

            $validResponse = $this->checkValidResponse( $response->getStatusCode() );
        }
        catch ( ClientException $e ) {
            $validResponse = $this->checkValidResponse( $e->getCode() );
            if ( $validResponse === false ) {
                $this->log( $e->getFile().':'.$e->getLine().' Client Exception ' . $e->getMessage().' - '.$e->getCode() , 'error');
                throw new \RuntimeException( 'Client Exception ' . $e->getMessage(), $e->getCode() );
            }
            if ( method_exists( $e, 'getResponse' ) ) {
                $response = $e->getResponse();
            }
        }
        catch ( \Exception $e ) {
            $validResponse = $this->checkValidResponse( $e->getCode() );
            if ( $validResponse === false ) {
                $this->log( $e->getFile().':'.$e->getLine().' Client Exception ' . $e->getMessage().' - '.$e->getCode() , 'error');
                throw new \RuntimeException( 'Exception ' . $e->getMessage(), $e->getCode() );
            }
            if ( method_exists( $e, 'getResponse' ) ) {
                $response = $e->getResponse();
            }
        }

        // Write the response to the cache
        $this->setCache($cacheKey, $response, $this->getCacheTime());

        // Write down the response
        $this->setResponse( $response );


        // When the response is not valid return a
        if ( $validResponse === false ) {
            // the response is not valid this can be a configuration issue or the API is not responding well.
            // Base line is that we need to return a "500: Internal Server Error" with as a error the original message from the API
            $this->log( 'HTTP exception, got a ' . $response->getStatusCode() . ' status, expect only one of these (' . implode( ',', $this->getValidStatusCodes() ) . ')', 'error');
            throw new \RuntimeException( 'HTTP exception, got a ' . $response->getStatusCode() . ' status, expect only one of these (' . implode( ',', $this->getValidStatusCodes() ) . ')', $response->getStatusCode() );
        }

        return true;
    }
@param string $method
@param string $url
@param array  $data
@param array  $headers

@return bool|mixed