-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from mewebstudio/psr-logger-integration
PSR-18 HTTP Client | PSR-3 logger support
- Loading branch information
Showing
49 changed files
with
747 additions
and
826 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
composer.lock | ||
/vendor | ||
/.idea | ||
|
||
/var/ |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Mews\Pos\Client; | ||
|
||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use function http_build_query; | ||
|
||
/** | ||
* PSR18 HTTP Client wrapper | ||
*/ | ||
class HttpClient | ||
{ | ||
/** @var ClientInterface */ | ||
protected $client; | ||
|
||
/** @var RequestFactoryInterface */ | ||
protected $requestFactory; | ||
|
||
/** @var StreamFactoryInterface */ | ||
protected $streamFactory; | ||
|
||
/** | ||
* @param ClientInterface $client | ||
* @param RequestFactoryInterface $requestFactory | ||
* @param StreamFactoryInterface $streamFactory | ||
*/ | ||
public function __construct( | ||
ClientInterface $client, | ||
RequestFactoryInterface $requestFactory, | ||
StreamFactoryInterface $streamFactory | ||
) { | ||
$this->client = $client; | ||
$this->requestFactory = $requestFactory; | ||
$this->streamFactory = $streamFactory; | ||
} | ||
|
||
public function post(string $path, ?array $payload = []): ResponseInterface | ||
{ | ||
return $this->send('POST', $path, $payload); | ||
} | ||
|
||
private function send(string $method, $path, ?array $payload = []): ResponseInterface | ||
{ | ||
$request = $this->createRequest($method, $path, $payload); | ||
|
||
return $this->client->sendRequest($request); | ||
} | ||
|
||
private function createRequest(string $method, string $url, ?array $payload = []): RequestInterface | ||
{ | ||
$request = $this->requestFactory->createRequest($method, $url); | ||
|
||
if ('POST' == $method) { | ||
$body = null; | ||
if (isset($payload['form_params'])) { | ||
$request = $request->withHeader('Content-Type', 'application/x-www-form-urlencoded'); | ||
$payload['body'] = http_build_query($payload['form_params']); | ||
} | ||
if (isset($payload['body'])) { | ||
$body = $this->streamFactory->createStream($payload['body']); | ||
} | ||
$request = $request->withBody($body); | ||
} | ||
|
||
|
||
if (isset($payload['headers'])) { | ||
foreach ($payload['headers'] as $key => $value) { | ||
$request = $request->withHeader($key, $value); | ||
} | ||
} | ||
|
||
return $request; | ||
} | ||
} |
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
Oops, something went wrong.