/** * This method is invoked before every HTTP request is sent to the API. When this happens, it * checks to see whether a token is set and valid, and then sets the ``X-Auth-Token`` header * for the HTTP request before letting it continue on its merry way. * * @param BeforeEvent $event * * @return mixed|void */ public function checkTokenIsValid(BeforeEvent $event) { $request = $event->getRequest(); if ($this->shouldIgnore($request)) { return; } if (!$this->token || $this->token->hasExpired()) { $this->authenticate(); } $request->setHeader('X-Auth-Token', $this->token->getId()); }
/** * This method is invoked before every HTTP request is sent to the API. When this happens, it * checks to see whether a token is set and valid, and then sets the ``X-Auth-Token`` header * for the HTTP request before letting it continue on its merry way. * * @param RequestInterface $request * @param array $options * * @return mixed|void */ public function __invoke(RequestInterface $request, array $options) { $fn = $this->nextHandler; if ($this->shouldIgnore($request)) { return $fn($request, $options); } if (!$this->token || $this->token->hasExpired()) { $this->token = call_user_func($this->tokenGenerator); } $modify = ['set_headers' => ['X-Auth-Token' => $this->token->getId()]]; return $fn(modify_request($request, $modify), $options); }