Skip to content

Commit 3f89c2c

Browse files
committed
HttpClient PSR-18
1 parent 29c6716 commit 3f89c2c

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

config/base.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ services:
7777
psr17.http_factory:
7878
class: Prokl\ServiceProvider\Services\PSR\PSR17\HttpFactory
7979

80+
Prokl\ServiceProvider\Services\PSR\PSR17\HttpFactory: '@psr17.http_factory'
81+
82+
# Http client стандарта PSR-18
83+
psr18.http_client:
84+
class: Prokl\ServiceProvider\Services\PSR\PSR18\PsrClient
85+
86+
Prokl\ServiceProvider\Services\PSR\PSR18\PsrClient: '@psr18.http_client'
87+
8088
# Request приложения.
8189
global.request:
8290
class: Symfony\Component\HttpFoundation\Request

readme.MD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ var_dump(container($micro)->getParameter('example'));
133133
- `bitrix.request.psr7` - Битриксовый Request, приведенный к PSR-7
134134
- `bitrix.response` - Response, полученный из битриксового
135135
- `bitrix.response.psr7` - Битриксовый Response, приведенный к PSR-7
136+
- `psr17.http_factory` - HttpFactory стандарта PSR-17
137+
- `psr18.http_client` - Http client стандарта PSR-18
136138

137139
## Хэлперы
138140

src/Services/PSR/PSR17/HttpFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
/**
2525
* Class HttpFactory
2626
* @package Prokl\ServiceProvider\Services\PSR\PSR17
27+
*
28+
* @see https://github.com/beta-eto-code/bitrix-psr17 (fork)
2729
*/
2830
class HttpFactory implements
2931
RequestFactoryInterface,
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Services\PSR\PSR18;
4+
5+
use Bitrix\Main\Web\HttpClient;
6+
use Bitrix\Main\Web\HttpHeaders;
7+
use GuzzleHttp\Psr7\Response;
8+
use Psr\Http\Client\ClientInterface;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
12+
/**
13+
* Class PsrClient
14+
* @package Prokl\ServiceProvider\Services\PSR\PSR18
15+
*
16+
* @see https://github.com/beta-eto-code/bitrix-psr18 (fork)
17+
*/
18+
class PsrClient implements ClientInterface
19+
{
20+
/**
21+
* @var HttpClient $httpClient
22+
*/
23+
private $httpClient;
24+
25+
/**
26+
* Client constructor.
27+
*
28+
* @param HttpClient|null $httpClient
29+
*/
30+
public function __construct(HttpClient $httpClient = null)
31+
{
32+
$this->httpClient = $httpClient ?? new HttpClient();
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function sendRequest(RequestInterface $request): ResponseInterface
39+
{
40+
$method = strtoupper($request->getMethod());
41+
$bxClient = clone $this->httpClient;
42+
$this->loadHeaders($bxClient, $request);
43+
44+
$body = (string)$request->getBody();
45+
if (empty($body)) {
46+
$body = null;
47+
}
48+
49+
$bxClient->query($method, (string)$request->getUri(), $body);
50+
$responseBody = $bxClient->getResult();
51+
if (empty($responseBody)) {
52+
$responseBody = null;
53+
}
54+
55+
return new Response($bxClient->getStatus(), $this->normalizeHeader($bxClient->getHeaders()), $responseBody);
56+
}
57+
58+
59+
/**
60+
* @param HttpClient $httpClient
61+
* @param RequestInterface $request
62+
*/
63+
private function loadHeaders(HttpClient $httpClient, RequestInterface $request)
64+
{
65+
$httpClient->clearHeaders();
66+
foreach ($request->getHeaders() as $name => $values) {
67+
$httpClient->setHeader($name, implode(", ", $values));
68+
}
69+
}
70+
71+
/**
72+
* @param HttpHeaders $headers
73+
* @return array
74+
*/
75+
private function normalizeHeader(HttpHeaders $headers): array
76+
{
77+
$result = [];
78+
foreach ($headers as $key => $value) {
79+
$result[$key] = implode(", ", (array)$value);
80+
}
81+
82+
return $result;
83+
}
84+
}

0 commit comments

Comments
 (0)