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

Feature: use curl (or whatever else) as HttpAdapter #675

Open
wants to merge 22 commits into
base: v3-v2021-02-25
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
774dc86
feat: allow for http adapter param
saas786 Sep 28, 2021
e20809e
Merge remote-tracking branch 'github-saas/syed/v3-v2021-02-25/guzzle'…
Mar 4, 2022
1071e2f
Revert "Merge remote-tracking branch 'github-saas/syed/v3-v2021-02-25…
Mar 4, 2022
9414cbe
Added a connection error class, to start handling basic connection er…
Mar 4, 2022
054764a
WIP on error handling
Mar 7, 2022
d703f67
Modified base_client to accept HttpAdapter in "options" parameter.
Mar 7, 2022
e02ce35
Merge branch 'v3-v2021-02-25--connection-errors' into v3-v2021-02-25-…
Mar 7, 2022
0341e5b
Preliminary commit of untested, crappy curl adapter.
Mar 7, 2022
f50c1e5
Oops, dumb modification got left in, now removed.
Mar 7, 2022
1eec2cc
how did this typo get through
Mar 7, 2022
83de8ca
implements, not extends, dammit
Mar 7, 2022
8b9ab35
fixes for curl client
Mar 10, 2022
9b6b3e7
tests/mock_client now supports http_adapter replacements in options
Mar 14, 2022
11d8872
Fallback to guessing error type from HTTP status code even if "Conten…
Mar 14, 2022
8fe6944
Merge branch 'recurly:v3-v2021-02-25' into v3-v2021-02-25--httpadapter
SinusPi Mar 14, 2022
c66fd99
Check response validity always, not just on toResource,
Mar 14, 2022
7caf7ce
Fixed response test to verify assertion, not toResource erroring out,…
Mar 14, 2022
2f4f754
Merge branch 'v3-v2021-02-25--httpadapter' of https://github.com/Sinu…
Mar 14, 2022
4590c71
Merge branch 'v3-v2021-02-25--dev' into v3-v2021-02-25--httpadapter
Mar 14, 2022
c4423aa
Fixed gzip decoding by delegating it to curl
Mar 17, 2022
454d2c3
Added a test for the curl adapter. WARNING: uses live API. Safeguarde…
Mar 17, 2022
32bc00f
Reworked error reporting in Curl adapter, no longer throwing a Recurl…
Mar 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Preliminary commit of untested, crappy curl adapter.
  • Loading branch information
Sinus committed Mar 7, 2022
commit 0341e5b81b79f0e0d084a86822083011bc6cd07b
16 changes: 1 addition & 15 deletions lib/recurly/http_adapter.php
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
/**
* @codeCoverageIgnore
*/
class HttpAdapter
class HttpAdapter extends HttpAdapterInterface
{
private static $_default_options = [
'ignore_errors' => true
@@ -43,28 +43,14 @@ public function execute($method, $url, $body, $headers): array
}
$options['header'] = $headers_str;
$context = stream_context_create(['http' => $options]);
//error_clear_last();
$result = file_get_contents("Q".$url, false, $context);
//$error = error_get_last();

if (!empty($result)) {
foreach($http_response_header as $h) {
if(preg_match('/Content-Encoding:.*gzip/i', $h)) {
$result = gzdecode($result);
}
}
} else {
// handle connection errors that prevented any valid response
//error_log(print_r($error,1));
if ($error['type']==E_WARNING) { error_log(__FILE__.": ".$error['message']); throw new \Recurly\Errors\ConnectionError($error['message']); }

//if (!is_resource($context)) { error_log(__FILE__.": stream not created"); throw new \Recurly\Errors\ConnectionError("stream not created"); }
if (is_resource($context)) {
$meta = stream_get_meta_data($context);
if ($meta['timed_out']) { error_log(__FILE__.": timed out"); throw new \Recurly\Errors\ConnectionError("timed out"); }

error_log(print_r($meta,1));
}
}
return [$result, $http_response_header];
}
69 changes: 69 additions & 0 deletions lib/recurly/http_adapter_curl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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 Recurly;

/**
* @codeCoverageIgnore
*/
class HttpAdapterCurl implements HttpAdapterInterface
{
/**
* 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
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_HEADER, 0);
$flat_headers = array_map($headers,function($k,$v) { return "$k: $v"; });
curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge($flat_headers, [
'Content-Length: '.strlen($body), // The Content-Length header is required by Recurly API infrastructure
]));
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADERFUNCTION, function($curl,$line) use (&$response_header) {
$response_header[]=$line;
});

$result = curl_exec($curl);

$curl_errno = curl_errno($curl);
$curl_error = curl_error($curl);
curl_close($curl);

if (!empty($result)) {
foreach($response_header as $h) {
if(preg_match('/Content-Encoding:.*gzip/i', $h)) {
$result = gzdecode($result);
}
}
} else {
// handle connection errors that prevented any valid response
//error_log(print_r($error,1));
if ($error['type']==E_WARNING) { error_log(__FILE__.": ".$error['message']); throw new \Recurly\Errors\ConnectionError($error['message']); }

//if (!is_resource($context)) { error_log(__FILE__.": stream not created"); throw new \Recurly\Errors\ConnectionError("stream not created"); }
if (is_resource($context)) {
$meta = stream_get_meta_data($context);
if ($meta['timed_out']) { error_log(__FILE__.": timed out"); throw new \Recurly\Errors\ConnectionError("timed out"); }

error_log(print_r($meta,1));
}
}
return [$result, $response_header];
}
}
11 changes: 11 additions & 0 deletions lib/recurly/http_adapter_interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Recurly;

/**
* @codeCoverageIgnore
*/
interface HttpAdapterInterface
{
public function execute($method, $url, $body, $headers): array;
}