-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
974 additions
and
393 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.idea | ||
composer.lock | ||
vendor |
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 @@ | ||
{"version":1,"defects":{"HttpExchange\\Adapters\\Guzzle6Test::testGet":7,"HttpExchange\\Adapters\\Guzzle6Test::testHttpMethods":5,"HttpExchange\\Adapters\\Guzzle6Test::testHead":5,"HttpExchange\\Adapters\\Guzzle6Test::testGet__exeption":5,"HttpExchange\\Adapters\\Guzzle6Test::testBatch__exception":7,"HttpExchange\\Adapters\\Guzzle6Test::testDelete":7},"times":{"PhpRedisQueue\\ClientTest::testPush":0.003,"PhpRedisQueue\\ClientTest::testPush__newJobsGoToBack":0.002,"PhpRedisQueue\\ClientTest::testPushToFront":0.004,"PhpRedisQueue\\ClientTest::testRerun":0.002,"PhpRedisQueue\\ClientTest::testRerun__missingJob":0,"PhpRedisQueue\\ClientTest::testRemove":0.002,"PhpRedisQueue\\ClientTest::testRemove__jobNotInQueue":0,"PhpRedisQueue\\QueueWorkerTest::testWork__noCallback":0.004,"PhpRedisQueue\\QueueWorkerTest::testWork__callbacksFire":0.006,"PhpRedisQueue\\QueueWorkerTest::testWork__successulJob":0.003,"PhpRedisQueue\\QueueWorkerTest::testWork__failedJob":0.004,"PhpRedisQueue\\QueueWorkerTest::testWork__jobStuckInProcessing":0.006,"PhpRedisQueue\\QueueWorkerTest::testWork__rerunJob":0.003,"PhpRedisQueue\\QueueWorkerTest::testWork__trimSucessLists":0.007,"PhpRedisQueue\\QueueWorkerTest::testWork__trimFailedLists":0.009,"HttpExchange\\Adapters\\Guzzle6Test::testGet":0.06,"HttpExchange\\Adapters\\Guzzle6Test::testHttpMethods":0.277,"HttpExchange\\Adapters\\Guzzle6Test::testDelete":0.053,"HttpExchange\\Adapters\\Guzzle6Test::testHead":0.057,"HttpExchange\\Adapters\\Guzzle6Test::testPatch":0.05,"HttpExchange\\Adapters\\Guzzle6Test::testPost":0.244,"HttpExchange\\Adapters\\Guzzle6Test::testPut":0.064,"HttpExchange\\Adapters\\Guzzle6Test::testGet__exeption":0.05,"HttpExchange\\Adapters\\Guzzle6Test::testDelete__exeption":0.053,"HttpExchange\\Adapters\\Guzzle6Test::testPatch__exeption":0.053,"HttpExchange\\Adapters\\Guzzle6Test::testPost__exeption":0.052,"HttpExchange\\Adapters\\Guzzle6Test::testPut__exeption":0.588,"HttpExchange\\Adapters\\Guzzle6Test::testBatch":0.068,"HttpExchange\\Adapters\\Guzzle6Test::testBatch__exception":0.08,"HttpExchange\\Adapters\\Guzzle7Test::testGet":0.128,"HttpExchange\\Adapters\\Guzzle7Test::testDelete":0.055,"HttpExchange\\Adapters\\Guzzle7Test::testPatch":0.436,"HttpExchange\\Adapters\\Guzzle7Test::testPost":0.058,"HttpExchange\\Adapters\\Guzzle7Test::testPut":0.052,"HttpExchange\\Adapters\\Guzzle7Test::testBatch":0.065,"HttpExchange\\Adapters\\Guzzle7Test::testGet__exeption":0.177,"HttpExchange\\Adapters\\Guzzle7Test::testDelete__exeption":0.067,"HttpExchange\\Adapters\\Guzzle7Test::testPatch__exeption":0.052,"HttpExchange\\Adapters\\Guzzle7Test::testPost__exeption":0.05,"HttpExchange\\Adapters\\Guzzle7Test::testPut__exeption":0.05,"HttpExchange\\Adapters\\Guzzle7Test::testBatch__exception":0.06}} |
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 |
---|---|---|
@@ -1,4 +1,278 @@ | ||
HttpExchange | ||
============ | ||
# HTTP Exchange | ||
|
||
A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy. | ||
A collection of PHP HTTP client adapters to make swapping out HTTP client dependencies quick and easy. | ||
|
||
Available adapters: | ||
|
||
* [Guzzle 6](https://docs.guzzlephp.org/en/6.5/) | ||
* [Guzzle 7](https://docs.guzzlephp.org/en/7.0/) | ||
|
||
## Requirements | ||
|
||
* PHP >= 8.0 | ||
* Guzzle >= 6.5 | ||
|
||
## Installation | ||
|
||
To install the library, you will need to use Composer in your project. | ||
|
||
```bash | ||
composer require johnshopkins/http-exchange | ||
``` | ||
|
||
## Basic usage | ||
|
||
### Single request | ||
|
||
```php | ||
$client = new GuzzleHttp\Client(); | ||
$http = new HttpExchange\Adapters\Guzzle7($client); | ||
|
||
$response = $http->get('http://httpbin.org/get'); | ||
$body = $response->getBody(); | ||
echo $body->url; | ||
|
||
// prints: http://httpbin.org/get | ||
``` | ||
|
||
### Batch requests | ||
|
||
```php | ||
$client = new GuzzleHttp\Client(); | ||
$http = new HttpExchange\Adapters\Guzzle7($client); | ||
|
||
$responses = $http->batch([ | ||
['get', 'http://httpbin.org/get'], | ||
['post', 'http://httpbin.org/post'] | ||
]); | ||
|
||
foreach ($responses as $response) { | ||
$body = $response->getBody(); | ||
echo $body->url . "\n"; | ||
} | ||
|
||
// prints: | ||
// http://httpbin.org/get | ||
// http://httpbin.org/post | ||
``` | ||
|
||
## Error handling | ||
|
||
### Single request | ||
|
||
If a request fails, a `HttpExchange\Exceptions\HTTP` exception is thrown. See the [exception methods documentation](#exception-methods) for more information. | ||
|
||
```php | ||
$client = new GuzzleHttp\Client(); | ||
$http = new HttpExchange\Adapters\Guzzle7($client); | ||
|
||
try { | ||
$response = $http->get('http://httpbin.org/status/503'); | ||
$body = $response->getBody(); | ||
} catch (\Exception $e) { | ||
echo $->getCode() . ': ' . $e->getMessage(); | ||
} | ||
|
||
// prints: 503: Server error: `GET http://httpbin.org/status/503` resulted in a `503 SERVICE UNAVAILABLE` response | ||
``` | ||
|
||
### Batch requests | ||
|
||
Instead of throwing a `HttpExchange\Exceptions\HTTP` when any one of the requests in a batch request fails, the exception is instead _returned_. This allows your application to gracefully handle failed requests, while processing successful ones. | ||
|
||
```php | ||
$client = new GuzzleHttp\Client(); | ||
$http = new HttpExchange\Adapters\Guzzle7($client); | ||
|
||
$responses = $http->batch([ | ||
['get', 'http://httpbin.org/get'], | ||
['get', 'http://httpbin.org/status/503'] | ||
]); | ||
|
||
foreach ($responses as $response) { | ||
if ($response->getStatusCode() === 200) { | ||
$body = $response->getBody(); | ||
echo $body->url . "\n"; | ||
} else { | ||
echo $body->url . "This request failed :(\n"; | ||
} | ||
} | ||
|
||
// prints: | ||
// http://httpbin.org/get | ||
// This request failed :( | ||
``` | ||
Alternatively, you can check which kind of object was returned from each request (`HttpExchange\Response` or `HttpExchange\Exceptions\HTTP`) and proceed accordingly: | ||
|
||
```php | ||
$client = new GuzzleHttp\Client(); | ||
$http = new HttpExchange\Adapters\Guzzle7($client); | ||
|
||
$responses = $http->batch([ | ||
['get', 'http://httpbin.org/get'], | ||
['get', 'http://httpbin.org/status/503'] | ||
]); | ||
|
||
foreach ($responses as $response) { | ||
if ($response instanceof HttpExchange\Response) { | ||
$body = $response->getBody(); | ||
echo $body->url . "\n"; | ||
} else { | ||
echo $body->url . "This request failed :(\n"; | ||
} | ||
} | ||
|
||
// prints: | ||
// http://httpbin.org/get | ||
// This request failed :( | ||
``` | ||
|
||
## Documentation | ||
|
||
### Initialization | ||
|
||
#### Guzzle 6 | ||
```php | ||
$client = new GuzzleHttp\Client(); | ||
$http = new HttpExchange\Adapters\Guzzle6($client); | ||
``` | ||
|
||
#### Guzzle 7 | ||
```php | ||
$client = new GuzzleHttp\Client(); | ||
$http = new HttpExchange\Adapters\Guzzle7($client); | ||
``` | ||
|
||
### Adapter methods | ||
|
||
#### __`batch(array $requests)`__ | ||
|
||
Send multiple requests concurrently. | ||
|
||
Returns: array containing the result of each request. A `HttpExchange\Response` object indicates a successful request while a `HttpExchange\Exceptions\HTTP` exception object indicates a failed request. | ||
|
||
Arguments: | ||
|
||
* `$requests`: An array of requests to make concurrently. Format: | ||
```php | ||
$requests = [[$method, $url, $request_options], ...]; | ||
``` | ||
* `$method`: HTTP method | ||
* `$uri`: Request URI | ||
* `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html)) | ||
|
||
|
||
#### __`get(array $requests)`__ | ||
|
||
Make a GET request. | ||
|
||
Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails. | ||
|
||
Arguments: | ||
|
||
* `$method`: HTTP method | ||
* `$uri`: Request URI | ||
* `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html)) | ||
|
||
|
||
#### __`post(array $requests)`__ | ||
|
||
Make a POST request. | ||
|
||
Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails. | ||
|
||
Arguments: | ||
|
||
* `$method`: HTTP method | ||
* `$uri`: Request URI | ||
* `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html)) | ||
|
||
|
||
#### __`put(array $requests)`__ | ||
|
||
Make a PUT request. | ||
|
||
Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails. | ||
|
||
Arguments: | ||
|
||
* `$method`: HTTP method | ||
* `$uri`: Request URI | ||
* `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html)) | ||
|
||
|
||
#### __`delete(array $requests)`__ | ||
|
||
Make a DELETE request. | ||
|
||
Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails. | ||
|
||
Arguments: | ||
|
||
* `$method`: HTTP method | ||
* `$uri`: Request URI | ||
* `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html)) | ||
|
||
|
||
#### __`patch(array $requests)`__ | ||
|
||
Make a PATCH request. | ||
|
||
Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails. | ||
|
||
Arguments: | ||
|
||
* `$method`: HTTP method | ||
* `$uri`: Request URI | ||
* `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html)) | ||
|
||
|
||
#### __`head(array $requests)`__ | ||
|
||
Make a HEAD request. | ||
|
||
Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails. | ||
|
||
Arguments: | ||
|
||
* `$method`: HTTP method | ||
* `$uri`: Request URI | ||
* `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html)) | ||
|
||
|
||
#### __`options(array $requests)`__ | ||
|
||
Make an OPTIONS request. | ||
|
||
Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails. | ||
|
||
Arguments: | ||
|
||
* `$method`: HTTP method | ||
* `$uri`: Request URI | ||
* `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html)) | ||
|
||
|
||
### Response methods | ||
|
||
#### __`getBody()`__ | ||
|
||
Get the body of the response. | ||
|
||
Returns: `SimpleXMLElement` object if the response is XML. Object or array if the response is JSON. | ||
|
||
#### __`getStatusCode()`__ | ||
|
||
Get the HTTP status code of the response; | ||
|
||
Returns: Integer | ||
|
||
### Exception methods | ||
|
||
All default methods available on PHP exceptions are available plus: | ||
|
||
#### __`getStatusCode()`__ | ||
|
||
Get the HTTP status code of the response; | ||
|
||
Returns: Integer |
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,3 @@ | ||
<?php | ||
|
||
require __DIR__ . '/vendor/autoload.php'; |
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 |
---|---|---|
@@ -1,14 +1,21 @@ | ||
{ | ||
"name": "johnshopkins/http-exchange", | ||
"description": "A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.", | ||
"homepage": "http://github.com/johnshopkins/http-exchange", | ||
"description": "A collection of PHP HTTP client adapters to make swapping out HTTP client dependencies quick and easy.", | ||
"homepage": "https://github.com/johnshopkins/http-exchange", | ||
"license": "MIT", | ||
"require": { | ||
"php": ">=8.1" | ||
"php": ">=8.0" | ||
}, | ||
"require-dev": { | ||
"guzzlehttp/guzzle": "^7.0", | ||
"phpunit/phpunit": "^10.3" | ||
}, | ||
"suggest": { | ||
"guzzlehttp/guzzle": "HTTP client supported by this library" | ||
}, | ||
"autoload": { | ||
"psr-4" : { | ||
"HttpExchange\\" : ["src/"] | ||
"HttpExchange\\" : ["src/", "tests/src/", "tests/mocks/"] | ||
} | ||
} | ||
} |
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,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"> | ||
<coverage/> | ||
<source> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
Oops, something went wrong.