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 #3 from ray-di/web-api-query
Support Web API
- Loading branch information
Showing
17 changed files
with
276 additions
and
19 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
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\MediaQuery\Annotation; | ||
|
||
use Attribute; | ||
use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation; | ||
|
||
/** | ||
* @Annotation | ||
* @Target("METHOD") | ||
*/ | ||
#[Attribute(Attribute::TARGET_METHOD)] | ||
final class WebQuery implements NamedArgumentConstructorAnnotation | ||
{ | ||
/** @var string */ | ||
public $method; | ||
|
||
/** @var string */ | ||
public $uri; | ||
|
||
public function __construct(string $method, string $uri) | ||
{ | ||
$this->method = $method; | ||
$this->uri = $uri; | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\MediaQuery\Exception; | ||
|
||
use RuntimeException; | ||
|
||
class WebApiRequestException extends RuntimeException | ||
{ | ||
} |
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\MediaQuery; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Ray\Di\Di\Named; | ||
use Ray\MediaQuery\Exception\WebApiRequestException; | ||
|
||
use function json_decode; | ||
use function uri_template; | ||
|
||
final class WebApiQuery implements WebApiQueryInterface | ||
{ | ||
/** @var ClientInterface */ | ||
private $client; | ||
|
||
/** @var MediaQueryLoggerInterface */ | ||
private $logger; | ||
|
||
/** @var array<string, string> */ | ||
private $domainBindings; | ||
|
||
/** | ||
* @param array<string, string> $domainBindings | ||
* | ||
* @Named("'domainBindings=web_api_query_domain") | ||
*/ | ||
#[Named('domainBindings=web_api_query_domain')] | ||
public function __construct(ClientInterface $client, MediaQueryLoggerInterface $logger, array $domainBindings) | ||
{ | ||
$this->client = $client; | ||
$this->logger = $logger; | ||
$this->domainBindings = $domainBindings; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function request(string $method, string $uri, array $query): array | ||
{ | ||
try { | ||
$this->logger->start(); | ||
$boundUri = uri_template($uri, $this->domainBindings + $query); | ||
$response = $this->client->request($method, $boundUri, $query); | ||
$json = $response->getBody()->getContents(); | ||
/** @var array<string, mixed> $body */ | ||
$body = json_decode($json, true); | ||
$this->logger->log($boundUri, $query); | ||
|
||
return $body; | ||
} catch (GuzzleException $e) { | ||
throw new WebApiRequestException($e->getMessage(), (int) $e->getCode(), $e); | ||
} | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\MediaQuery; | ||
|
||
interface WebApiQueryInterface | ||
{ | ||
/** | ||
* @param array<string, string> $query | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function request(string $method, string $uri, array $query): array; | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\MediaQuery; | ||
|
||
use Ray\Aop\MethodInterceptor; | ||
use Ray\Aop\MethodInvocation; | ||
use Ray\MediaQuery\Annotation\WebQuery; | ||
|
||
class WebQueryInterceptor implements MethodInterceptor | ||
{ | ||
/** @var WebApiQueryInterface */ | ||
private $webApiQuery; | ||
|
||
/** @var ParamInjectorInterface */ | ||
private $paramInjector; | ||
|
||
public function __construct(WebApiQueryInterface $webApiQuery, ParamInjectorInterface $paramInjector) | ||
{ | ||
$this->webApiQuery = $webApiQuery; | ||
$this->paramInjector = $paramInjector; | ||
} | ||
|
||
/** | ||
* @return Pages|array<mixed> | ||
*/ | ||
public function invoke(MethodInvocation $invocation) | ||
{ | ||
$method = $invocation->getMethod(); | ||
/** @var WebQuery $webQuery */ | ||
$webQuery = $method->getAnnotation(WebQuery::class); | ||
/** @var array<string, string> $values */ | ||
$values = $this->paramInjector->getArgumentes($invocation); | ||
|
||
return $this->webApiQuery->request($webQuery->method, $webQuery->uri, $values); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\MediaQuery\WebApi; | ||
|
||
use Ray\MediaQuery\Annotation\WebQuery; | ||
|
||
interface FooItemInterface | ||
{ | ||
#[WebQuery(method: 'GET', uri: 'https://{domain}/anything/{id}')] | ||
public function __invoke(string $id): array; | ||
} |
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
Oops, something went wrong.