-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring: testable, PSR-18 http client (#53)
* chore: add phpunit library * refactor: "inject" client through Environment * tests: add basic tests for HttpClient uses Guzzle with MockHandler to simulate http client responses * refactor: extract GuzzleFactory. Extract Response class * refactor: move logic from Guzzle to Request * refactor: use psr7 request object * refactor: create request in GuzzleFactory * refactor: remove Guzzle class * refactor: Request does not depend on concrete client. use GuzzleFactory for default http client / request * chore: ignore .idea dir * fix: uri building. Add test cases for query params / body * refactor: extract "verify" setting outside the factory * tests: cover Request error handling * chore!: release 4.0.0 (please see changelog) --------- Co-authored-by: cb-karthikp <[email protected]>
- Loading branch information
1 parent
15cd331
commit f7f8d9e
Showing
13 changed files
with
669 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace ChargeBee\ChargeBee\HttpClient; | ||
|
||
use ChargeBee\ChargeBee; | ||
use ChargeBee\ChargeBee\Request; | ||
use ChargeBee\ChargeBee\Version; | ||
use Exception; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Uri; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
final class GuzzleFactory implements HttpClientFactory | ||
{ | ||
private $options; | ||
private $connectTimeoutInSecs; | ||
private $requestTimeoutInSecs; | ||
|
||
/** | ||
* @param array $options | ||
* @param float $connectTimeoutInSecs | ||
* @param float $requestTimeoutInSecs | ||
*/ | ||
public function __construct($connectTimeoutInSecs, $requestTimeoutInSecs, $options = []) | ||
{ | ||
$this->connectTimeoutInSecs = $connectTimeoutInSecs; | ||
$this->requestTimeoutInSecs = $requestTimeoutInSecs; | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* @return ClientInterface|Client | ||
*/ | ||
public function createClient() | ||
{ | ||
return new Client( | ||
array_merge( | ||
[ | ||
'allow_redirects' => true, | ||
'http_errors' => false, | ||
'connect_timeout' => $this->connectTimeoutInSecs, | ||
'timeout' => $this->requestTimeoutInSecs | ||
], | ||
$this->options | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
* @return RequestInterface | ||
*/ | ||
public function createRequest($meth, $headers, $env, $url, $params) | ||
{ | ||
if (!in_array($meth, [Request::GET, Request::POST])) { | ||
throw new Exception("Invalid http method $meth"); | ||
} | ||
|
||
$userAgent = "Chargebee-PHP-Client" . " v" . Version::VERSION; | ||
$httpHeaders = array_merge( | ||
$headers, | ||
[ | ||
'Accept' => 'application/json', | ||
'User-Agent' => $userAgent, | ||
'Lang-Version' => phpversion(), | ||
'OS-Version' => PHP_OS, | ||
'Authorization' => 'Basic ' . \base64_encode($env->getApiKey() . ':') | ||
] | ||
); | ||
$body = null; | ||
|
||
$uri = new Uri($url); | ||
|
||
if ($meth == Request::GET) { | ||
if (count($params) > 0) { | ||
$query = \http_build_query($params, '', '&', \PHP_QUERY_RFC3986); | ||
$uri = $uri->withQuery($query); | ||
} | ||
} | ||
|
||
if ($meth == Request::POST) { | ||
$body = \http_build_query($params, '', '&'); | ||
$httpHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; | ||
} | ||
|
||
return new \GuzzleHttp\Psr7\Request($meth, $uri, $httpHeaders, $body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace ChargeBee\ChargeBee\HttpClient; | ||
|
||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
interface HttpClientFactory | ||
{ | ||
/** | ||
* @return ClientInterface | ||
*/ | ||
public function createClient(); | ||
|
||
/** | ||
* @throws \Exception | ||
* @return RequestInterface | ||
*/ | ||
public function createRequest($meth, $headers, $env, $url, $params); | ||
} |
Oops, something went wrong.