Skip to content

Commit 3bf3941

Browse files
committed
version 1.0.0
0 parents  commit 3bf3941

16 files changed

+362
-0
lines changed

.github/CODE_OF_CONDUCT.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

.github/CONTRIBUTING.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Contributing
2+
3+
## Report an issue
4+
5+
Please follow the guidelines below when creating an issue so that your issue can be more promptly resolved:
6+
* Provide information including: the version of PHP and This package, the type of operating system and Web server;
7+
* Provide the complete error call stack if available;
8+
* Describe the steps for reproducing the issue. It would be even better if you could provide code to reproduce the issue;
9+
10+
Do not report an issue if you are asking how to use some feature. You should use the `README.md` file.
11+
12+
Before you report an issue, please search through existing issues to see if your issue is already reported or fixed to make sure you are not reporting a duplicated issue. Also make sure you have the latest version of package and see if the issue still exists.

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom: "https://www.paypal.me/ddrv"

.github/ISSUE_TEMPLATE/bug_report.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: 'Bug'
5+
labels: bug
6+
assignees: ddrv
7+
8+
---
9+
10+
### What steps will reproduce the problem?
11+
12+
### What is the expected result?
13+
14+
### What do you get instead?
15+
16+
### Additional info
17+
18+
| Q | A
19+
| ---------------- | ---
20+
| Library version | 1.?
21+
| PHP version | ?
22+
| Operating system | ?

.github/ISSUE_TEMPLATE/custom.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
name: Custom issue template
3+
about: Describe this issue template's purpose here.
4+
title: ''
5+
labels: ''
6+
assignees: ddrv
7+
8+
---
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: feature
6+
assignees: ddrv
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/PULL_REQUEST_TEMPLATE.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| Q | A
2+
| ------------- | ---
3+
| Bug fix? | yes/no
4+
| New feature? | yes/no
5+
| Tickets | Fix #...

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor/
2+
composer.lock
3+
phpcs.xml
4+
phpunit.xml

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Ivan Dudarev, https://github.com/ddrv <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[![Latest Stable Version](https://img.shields.io/packagist/v/webclient/ext-protocol-version.svg?style=flat-square)](https://packagist.org/packages/webclient/ext-protocol-version)
2+
[![Total Downloads](https://img.shields.io/packagist/dt/webclient/ext-protocol-version.svg?style=flat-square)](https://packagist.org/packages/webclient/ext-protocol-version/stats)
3+
[![License](https://img.shields.io/packagist/l/webclient/ext-protocol-version.svg?style=flat-square)](https://github.com/phpwebclient/ext-protocol-version/blob/master/LICENSE)
4+
[![PHP](https://img.shields.io/packagist/php-v/webclient/ext-protocol-version.svg?style=flat-square)](https://php.net)
5+
6+
# webclient/ext-protocol-version
7+
8+
Auto change protocol version extension for PSR-18 HTTP client.
9+
10+
# Install
11+
12+
Install this package and your favorite [psr-18 implementation](https://packagist.org/providers/psr/http-client-implementation).
13+
14+
```bash
15+
composer require webclient/ext-protocol-version:^1.0
16+
```
17+
18+
# Using
19+
20+
```php
21+
<?php
22+
23+
use Webclient\Extension\ProtocolVersion\Client;
24+
use Psr\Http\Client\ClientInterface;
25+
use Psr\Http\Message\RequestInterface;
26+
27+
/**
28+
* @var ClientInterface $client Your PSR-18 HTTP Client
29+
*/
30+
$http = new Client($client);
31+
32+
/** @var RequestInterface $request */
33+
$response = $http->sendRequest($request);
34+
```
35+
36+
If server returns a 505 error, this client will repeat the request with the protocol version specified in the server response.

composer.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "webclient/ext-protocol-version",
3+
"type": "library",
4+
"description": "Auto change protocol version extension for PSR-18 HTTP client",
5+
"keywords": ["http", "client", "http-client", "http-client-extension", "http-protocol-version"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Ivan Dudarev",
10+
"email": "[email protected]",
11+
"homepage": "https://ddrv.ru"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.0",
16+
"psr/http-client": "^1.0"
17+
},
18+
"require-dev": {
19+
"nyholm/psr7": "^1.3",
20+
"phpunit/phpunit": "^6.5",
21+
"squizlabs/php_codesniffer": "^3.5",
22+
"webclient/fake-http-client": "^1.0"
23+
},
24+
"provide": {
25+
"psr/http-client-implementation": "1.0"
26+
},
27+
"suggest": {
28+
"psr/http-client-implementation": "Choice your favorite psr-18 implementation"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"Webclient\\Extension\\ProtocolVersion\\": "src/"
33+
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Stuff\\Webclient\\Extension\\ProtocolVersion\\": "stuff/",
38+
"Tests\\Webclient\\Extension\\ProtocolVersion\\": "tests/"
39+
}
40+
}
41+
}

phpcs.xml.dist

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Coding standard">
3+
<description>Coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<!-- use colors in output -->
8+
<arg name="colors"/>
9+
10+
<!-- inherit rules from: -->
11+
<rule ref="PSR12">
12+
<exclude name="PSR12.Properties.ConstantVisibility.NotFound"/>
13+
</rule>
14+
15+
<!-- Paths to check -->
16+
<file>src</file>
17+
<file>stuff</file>
18+
<file>tests</file>
19+
</ruleset>

phpunit.xml.dist

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php" colors="true">
3+
<testsuites>
4+
<testsuite name="all">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

src/Client.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Extension\ProtocolVersion;
6+
7+
use Psr\Http\Client\ClientInterface;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
final class Client implements ClientInterface
12+
{
13+
14+
/**
15+
* @var ClientInterface
16+
*/
17+
private $client;
18+
19+
public function __construct(ClientInterface $client)
20+
{
21+
$this->client = $client;
22+
}
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
public function sendRequest(RequestInterface $request): ResponseInterface
28+
{
29+
$response = $this->client->sendRequest($request);
30+
if ($response->getStatusCode() === 505) {
31+
return $this->client->sendRequest($request->withProtocolVersion($response->getProtocolVersion()));
32+
}
33+
return $response;
34+
}
35+
}

stuff/Handler.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stuff\Webclient\Extension\ProtocolVersion;
6+
7+
use Nyholm\Psr7\Response;
8+
use Psr\Http\Message\ResponseInterface;
9+
use Psr\Http\Message\ServerRequestInterface;
10+
use Psr\Http\Server\RequestHandlerInterface;
11+
12+
class Handler implements RequestHandlerInterface
13+
{
14+
15+
/**
16+
* @var string[]
17+
*/
18+
private $versions;
19+
20+
public function __construct(string $defaultVersion, string ...$supportedVersions)
21+
{
22+
$this->versions[$defaultVersion] = true;
23+
foreach ($supportedVersions as $version) {
24+
$this->versions[$version] = true;
25+
}
26+
}
27+
28+
/**
29+
* @param ServerRequestInterface $request
30+
* @return ResponseInterface
31+
*/
32+
public function handle(ServerRequestInterface $request): ResponseInterface
33+
{
34+
$headers = [
35+
'Content-Type' => 'text/plain',
36+
];
37+
if (!array_key_exists($request->getProtocolVersion(), $this->versions)) {
38+
$version = (string)array_keys($this->versions)[0];
39+
return new Response(505, $headers, 'error', $version, 'HTTP Version Not Supported');
40+
}
41+
return new Response(200, $headers, 'success', $request->getProtocolVersion(), 'OK');
42+
}
43+
}

tests/ClientTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Webclient\Extension\ProtocolVersion;
6+
7+
use Stuff\Webclient\Extension\ProtocolVersion\Handler;
8+
use Nyholm\Psr7\Request;
9+
use PHPUnit\Framework\TestCase;
10+
use Psr\Http\Client\ClientExceptionInterface;
11+
use Webclient\Extension\ProtocolVersion\Client;
12+
use Webclient\Fake\Client as FakeClient;
13+
14+
class ClientTest extends TestCase
15+
{
16+
17+
/**
18+
* @param string $requestVersion
19+
* @param string $responseVersion
20+
* @param string[] $supportedVersions
21+
*
22+
* @throws ClientExceptionInterface
23+
*
24+
* @dataProvider provideProtocol
25+
*/
26+
public function testProtocol(string $requestVersion, string $responseVersion, array $supportedVersions)
27+
{
28+
$client = new Client(new FakeClient(new Handler(...$supportedVersions)));
29+
$request = new Request('GET', '/', ['Accept' => 'text/plain'], null, $requestVersion);
30+
$response = $client->sendRequest($request);
31+
$this->assertSame($responseVersion, $response->getProtocolVersion());
32+
}
33+
34+
public function provideProtocol(): array
35+
{
36+
return [
37+
['1.0', '1.0', ['1.0']],
38+
['3', '1.1', ['1.1']],
39+
['2', '2', ['1.0', '1.1', '2', '2.0']],
40+
['2.1', '2', ['2', '2.0']],
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)