Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to utilize Guzzle Http Request instead of your custom request? #635

Open
saas786 opened this issue Sep 27, 2021 · 2 comments
Labels

Comments

@saas786
Copy link

saas786 commented Sep 27, 2021

On my local server (wamp), my recurly requests takes more than few minutes to complete the request.
Spent few days tracing the issue, but cant put my finger on what the actual issue could be.

But changing:

$context = stream_context_create(['http' => $options]);

        $context = stream_context_create(['http' => $options]);
        $result = file_get_contents($url, false, $context);

to

        $guzzle = new \GuzzleHttp\Client();
        $request = new \GuzzleHttp\Psr7\Request($method, $url, $headers);
        $response = $guzzle
            ->send($request, ['body' => $body]);

        $result = $response->getBody()->getContents();

Make it snappier, and

$result contains the json data.

But $http_response_header causes the issue, as with:

        $http_response_header = [
            "HTTP/{$response->getProtocolVersion()} {$response->getStatusCode()} {$response->getReasonPhrase()}"
        ];
        foreach ($response->getHeaders() as $name => $values) {
            $http_response_header[] = $name . ': ' . implode(', ', $values) . "\r\n";
        }

It Emulates the headers, but only returns 7 headers, compared to original codes 18 headers.

Which ultimately fails the rest of the code after :

list($result, $response_header) = $this->http->execute($request->getMethod(), $request->getPath(), $request->getBodyAsJson(), $request->getHeaders());

Is there any fix I can use to make existing code work?

Or can you please utilize Guzzle http? Or make it pluggable?

###Update
It seems like everything is fine, was using $request instead of $response for getting headers.

@saas786
Copy link
Author

saas786 commented Sep 28, 2021

Created a PR: #636

So instead of implementing the Guzzle Request & Client, opted to make core code pluggable.

If you merge this, I can plug my own HTTPAdapter and use my custom guzzle based request & client.

@saas786
Copy link
Author

saas786 commented Sep 28, 2021

My custom HTTP Adapter class. Any suggestions are welcome.

<?php
/**
 * This class abstracts away all the PHP-level HTTP
 * code. This allows us to easily mock out the HTTP
 * calls in BaseClient by injecting a mocked version of
 * this adapter.
 */

namespace MyApp;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Recurly\HttpAdapterInterface;

/**
 * @codeCoverageIgnore
 */
class HttpAdapter implements HttpAdapterInterface
{
    private static $_default_options = [
        'ignore_errors' => true
    ];

    /**
     * Performs HTTP request
     *
     * @param string $method  HTTP method to use
     * @param string $url     Fully qualified URL
     * @param string $body    The request body
     * @param array  $headers HTTP headers
     *
     * @return array The API response as a string and the headers as an array
     */
    public function execute($method, $url, $body, $headers): array
    {

        // The Content-Length header is required by Recurly API infrastructure
        $headers['Content-Length'] = strlen($body);

        $guzzle = new Client();
        $request = new Request($method, $url, $headers);
        $response = $guzzle
            ->send($request, ['body' => $body]);

        $http_response_header = [
            "HTTP/{$response->getProtocolVersion()} {$response->getStatusCode()} {$response->getReasonPhrase()}"
        ];

        foreach ($response->getHeaders() as $name => $values) {
            $http_response_header[] = $name . ': ' . implode(', ', $values);
        }

        $result = $response->getBody()->getContents();

        return [$result, $http_response_header];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant