304ec363bf1d7acdbf59ecf80b2e418c12b217a8
3 namespace seekat\API\Call
;
6 Client
, Client\Request
, Client\Response
8 use Psr\Log\LoggerInterface
;
14 * The response importer
35 * @var LoggerInterface
40 * The executed request
47 * The promised response
74 * Create a deferred promise for the response of $request
76 * @param API $api The endpoint of the request
77 * @param Request $request The request to execute
78 * @param Cache\Service $cache
80 function __construct(API
$api, Request
$request, Cache\Service
$cache = null) {
81 $this->request
= $request;
82 $this->client
= $api->getClient();
83 $this->logger
= $api->getLogger();
84 $this->result
= new Result($api);
85 $this->cache
= new Cache($cache);
87 $future = $api->getFuture();
88 $context = $future->createContext(function() {
89 if ($this->response
) {
90 /* we did finish in the meantime */
93 $this->client
->dequeue($this->request
);
94 ($this->reject
)("Cancelled");
97 $this->promise
= $future->getPromise($context);
98 $this->resolve
= API\Future\resolver
($future, $context);
99 $this->reject
= API\Future\rejecter
($future, $context);
102 function __invoke() {
103 if ($this->cache
->load($this->request
, $cached)) {
104 $this->logger
->info("deferred -> cached", [
105 "method" => $this->request
->getRequestMethod(),
106 "url" => $this->request
->getRequestUrl(),
109 $this->response
= $cached;
112 $this->client
->enqueue($this->request
, function(Response
$response) use($cached) {
113 if ($response->getResponseCode() == 304) {
114 $this->response
= $cached;
116 $this->response
= $response;
121 $this->logger
->info("deferred -> enqueued", [
122 "method" => $this->request
->getRequestMethod(),
123 "url" => $this->request
->getRequestUrl(),
126 $this->client
->once();
129 return $this->promise
;
133 * Completion callback
135 private function complete() {
136 if ($this->response
) {
138 $api = ($this->result
)($this->response
);
140 $this->cache
->save($this->request
, $this->response
);
142 ($this->resolve
)($api);
143 } catch (\Throwable
$e) {
147 ($this->reject
)($this->client
->getTransferInfo($this->request
)->error
);