public function addAuthHeaders(BeforeEvent $event) { /* * Get Consumer ID and Private Key from auth and then unset it */ $auth = $event->getClient()->getDefaultOption('auth'); if ($auth === null) { throw new \Exception('Http client is missing \'auth\' parameters', 1466965269); } $consumerId = $auth[0]; $privateKey = $auth[1]; $event->getClient()->setDefaultOption('auth', null); /* * Get Request URL, method, and timestamp to calculate signature */ $requestUrl = $event->getRequest()->getUrl(); //decode url back to normal to nextCursor issue. automatic url encoding $requestUrl = rawurldecode($requestUrl); $event->getRequest()->setUrl($requestUrl); $requestMethod = $event->getRequest()->getMethod(); $timestamp = Utils::getMilliseconds(); $signature = Signature::calculateSignature($consumerId, $privateKey, $requestUrl, $requestMethod, $timestamp); /* * Add required headers to request */ $headers = ['WM_SVC.NAME' => 'Walmart Marketplace', 'WM_QOS.CORRELATION_ID' => base64_encode(Random::string(16)), 'WM_SEC.TIMESTAMP' => $timestamp, 'WM_SEC.AUTH_SIGNATURE' => $signature, 'WM_CONSUMER.ID' => $consumerId]; $currentHeaders = $event->getRequest()->getHeaders(); unset($currentHeaders['Authorization']); $updatedHeaders = array_merge($currentHeaders, $headers); $event->getRequest()->setHeaders($updatedHeaders); }
public function onBefore(BeforeEvent $event) { $body = $event->getRequest()->getBody(); // Only works when there is a body with a known size. if (!$body || !$body->getSize()) { return; } // Wrap the existing request body in an upload decorator. $progressBody = new UploadProgressStream($body, $this->uploadProgress, $event->getClient(), $event->getRequest()); $event->getRequest()->setBody($progressBody); }
/** * {@inheritdoc} */ public function onBefore(BeforeEvent $event, $name) { $client = $event->getClient(); $request = $event->getRequest(); $time = time() + $client->getTimeDrift(); $authenticated = $client->isAuthenticated(); $signature = null; if ($authenticated) { $signature = $this->getSignature($request, $time); } $this->createOvhHeader($request, $signature, $time); }
public function testInterceptsWithEvent() { $response = new Response(200); $res = null; $t = new Transaction(new Client(), new Request('GET', '/')); $t->getRequest()->getEmitter()->on('complete', function ($e) use(&$res) { $res = $e; }); $e = new BeforeEvent($t); $e->intercept($response); $this->assertTrue($e->isPropagationStopped()); $this->assertSame($res->getClient(), $e->getClient()); }
function let(SiteConfigBuilder $siteConfigBuilder, SiteConfig $siteConfig, Factory $authenticatorFactory, ClientInterface $guzzle, Emitter $emitter, BeforeEvent $beforeEvent, CompleteEvent $completeEvent, RequestInterface $request, ResponseInterface $response, Factory $authenticatorFactory) { $siteConfig->getHost()->willReturn('example.com'); $siteConfigBuilder->buildForHost('example.com')->willReturn($siteConfig); $guzzle->getEmitter()->willReturn($emitter); $request->getHost()->willReturn('example.com'); $beforeEvent->getRequest()->willReturn($request); $beforeEvent->getClient()->willReturn($guzzle); $response->getBody()->willReturn('<html></html>'); $completeEvent->getResponse()->willReturn($response); $completeEvent->getRequest()->willReturn($request); $completeEvent->getClient()->willReturn($guzzle); $this->beConstructedWith($siteConfigBuilder, $authenticatorFactory); }
public function loginIfRequired(BeforeEvent $event) { if (($config = $this->buildSiteConfig($event->getRequest())) === false) { return; } if (!$config->requiresLogin()) { return; } $client = $event->getClient(); $authenticator = $this->authenticatorFactory->buildFromSiteConfig($config); if (!$authenticator->isLoggedIn($client)) { $emitter = $client->getEmitter(); $emitter->detach($this); $authenticator->login($client); $emitter->attach($this); } }
/** * @throws \OutOfBoundsException|\Exception */ public function onBefore(BeforeEvent $event) { if (!($item = array_shift($this->queue))) { throw new \OutOfBoundsException('Mock queue is empty'); } elseif ($item instanceof RequestException) { throw $item; } // Emulate the receiving of the response headers $request = $event->getRequest(); $transaction = new Transaction($event->getClient(), $request); $transaction->setResponse($item); $request->getEmitter()->emit('headers', new HeadersEvent($transaction)); // Emulate reading a response body if ($this->readBodies && $request->getBody()) { while (!$request->getBody()->eof()) { $request->getBody()->read(8096); } } $event->intercept($item); }