-
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.
[wip] Refactor to use Guzzle and custom Response class
- Loading branch information
Showing
5 changed files
with
204 additions
and
69 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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ticketpark\ApiClient\Http; | ||
|
||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Psr7\Response as GuzzleResponse; | ||
|
||
final class Client | ||
{ | ||
private GuzzleClient $guzzle; | ||
|
||
public function __construct() | ||
{ | ||
$this->guzzle = new GuzzleClient(); | ||
} | ||
|
||
public function head(string $url, array $headers): Response | ||
{ | ||
return $this->execute('head', $url, $headers); | ||
} | ||
|
||
public function get(string $url, array $headers): Response | ||
{ | ||
return $this->execute('get', $url, $headers); | ||
} | ||
|
||
public function post(string $url, string $content, array $headers): Response | ||
{ | ||
return $this->execute('post', $url, $headers, $content); | ||
} | ||
|
||
public function postForm(string $url, array $formData, array $headers): Response | ||
{ | ||
return $this->execute('post', $url, $headers, null, $formData); | ||
} | ||
|
||
public function patch(string $url, string $content, array $headers): Response | ||
{ | ||
return $this->execute('patch', $url, $headers, $content); | ||
} | ||
|
||
public function delete(string $url, array $headers): Response | ||
{ | ||
return $this->execute('delete', $url, $headers); | ||
} | ||
|
||
private function execute( | ||
string $method, | ||
string $url, | ||
array $headers = [], | ||
string $content = null, | ||
array $formData = [] | ||
): Response { | ||
try { | ||
/** @var GuzzleResponse $response */ | ||
$guzzleResponse = $this->guzzle->request( | ||
$method, | ||
$url, | ||
[ | ||
'headers' => $headers, | ||
'body' => $content, | ||
'form_params' => $formData | ||
] | ||
); | ||
} catch (\Exception $e) { | ||
if (!$e instanceof ClientException) { | ||
throw new HttpRequestException($e->getMessage()); | ||
} | ||
|
||
/** @var GuzzleResponse $response */ | ||
$guzzleResponse = $e->getResponse(); | ||
} | ||
|
||
return new Response( | ||
$guzzleResponse->getStatusCode(), | ||
(string) $guzzleResponse->getBody(), | ||
$guzzleResponse->getHeaders() | ||
); | ||
} | ||
} |
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 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ticketpark\ApiClient\Http; | ||
|
||
class HttpRequestException extends \Exception | ||
{ | ||
} |
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ticketpark\ApiClient\Http; | ||
|
||
class Response | ||
{ | ||
public function __construct( | ||
private int $statusCode, | ||
private string $content, | ||
private array $headers | ||
) | ||
{ | ||
} | ||
|
||
public function getStatusCode(): int | ||
{ | ||
return $this->statusCode; | ||
} | ||
|
||
public function getContent(): array | ||
{ | ||
return json_decode($this->content, true); | ||
} | ||
|
||
public function getHeaders(): array | ||
{ | ||
return $this->headers; | ||
} | ||
|
||
public function isSuccessful(): bool | ||
{ | ||
return ($this->statusCode >= 200 && $this->statusCode <= 204); | ||
} | ||
} |
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