Skip to content

Commit 76fec6c

Browse files
committed
Add initial project set up
1 parent bbf5c2f commit 76fec6c

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,54 @@
1-
# php-http-client-middleware
1+
# HTTP Client middleware
2+
3+
HTTP Client middleware is a middleware solution for [PSR-18 (HTTP Client)](http://www.php-fig.org/psr/psr-18).
4+
5+
This package contains both a middleware interface as well as a ready to use HTTP Client that is capable of handling middleware.
6+
7+
**NB** This package does not contain any middleware implementations.
8+
9+
## Rationale
10+
The interface offered by PSR-18 (HTTP Client) does not offer any (configuration) options offered by known HTTP abstractions - for example Guzzle and Symfony HttpClient. This makes implementations of this interface exchangeable, but does require you to write these configurations yourself. This can mean additional code that is repeated in multiple locations. Something that might be undesirable.
11+
12+
This might be solved by using a solution similar to the middleware defined in [PSR-15 (HTTP Server Request Handlers)](https://www.php-fig.org/psr/psr-15). Using middleware will allow centralization of functionality without the necessity of extending or wrapping a client. It also enables you to perform actions both _before_ performing the actual request and _after_ the actual request.
13+
14+
## Installation
15+
16+
```bash
17+
composer require coolblue/http-client-middleware
18+
```
19+
20+
## Usage
21+
Middleware needs to comply to the interface `\Coolblue\Client\MiddlewareInterface`:
22+
23+
```php
24+
<?php
25+
26+
declare(strict_types=1);
27+
28+
namespace Coolblue\Http\Client;
29+
30+
use Psr\Http\Client\ClientInterface;
31+
use Psr\Http\Message\RequestInterface;
32+
use Psr\Http\Message\ResponseInterface;
33+
34+
interface MiddlewareInterface
35+
{
36+
public function process(RequestInterface $request, ClientInterface $client): ResponseInterface;
37+
}
38+
39+
```
40+
41+
To create a middleware enabled client:
42+
43+
```php
44+
45+
$client = new Client(); // an instance of \Psr\Http\Client\ClientInterface
46+
$middlewareOne = new Middleware(); // an instance of \Coolblue\Client\Http\MiddlewareInterface
47+
$middlewareTwo = new Middleware(); // an instance of \Coolblue\Client\Http\MiddlewareInterface
48+
49+
$middlewareClient = new \Coolblue\Http\Client\MiddlewareClient(
50+
$client,
51+
$middlewareOne,
52+
$middlewareTwo
53+
);
54+
```

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "coolblue/http-client-middleware",
3+
"description": "A middleware solution for PSR-18 compliant http clients.",
4+
"type": "library",
5+
"license": "MIT",
6+
"require": {
7+
"psr/http-client": "^1.0",
8+
"psr/http-message": "^1.0"
9+
},
10+
"autoload": {
11+
"psr-4": {
12+
"Coolblue\\Http\\Client\\": "src/"
13+
}
14+
},
15+
"config": {
16+
"sort-packages": true
17+
}
18+
}

0 commit comments

Comments
 (0)