public function sign(Request $request, ApiKey $apiKey)
 {
     date_default_timezone_set(self::TIME_ZONE);
     $date = new \DateTime();
     $timeStamp = $date->format(self::TIMESTAMP_FORMAT);
     $requestHeaders = $request->getHeaders();
     unset($requestHeaders[self::STORMPATH_DATE_HEADER]);
     unset($requestHeaders[self::AUTHORIZATION_HEADER]);
     $authorizationHeader = base64_encode($apiKey->getId() . ":" . $apiKey->getSecret());
     $requestHeaders[self::STORMPATH_DATE_HEADER] = $timeStamp;
     $requestHeaders[self::AUTHORIZATION_HEADER] = self::AUTHENTICATION_SCHEME . " " . $authorizationHeader;
     $request->setHeaders($requestHeaders);
 }
 public function executeRequest(Request $request, $redirectsLimit = 10)
 {
     $requestHeaders = $request->getHeaders();
     $apiKey = $request->getApiKey();
     if ($apiKey) {
         $this->signer->sign($request, $apiKey);
         $this->httpClient->setConfig(array(Client::REQUEST_OPTIONS => array('allow_redirects' => false, 'exceptions' => false, 'verify' => false)));
         $this->httpClient->setUserAgent($requestHeaders['User-Agent']);
     }
     $httpRequest = $this->httpClient->createRequest($method = $request->getMethod(), $uri = $request->getResourceUrl(), $headers = $request->getHeaders(), $body = $request->getBody());
     $this->addQueryString($request->getQueryString(), $httpRequest);
     $response = $httpRequest->send();
     if ($response->isRedirect() && $redirectsLimit) {
         $request->setResourceUrl($response->getHeader('location'));
         return $this->executeRequest($request, --$redirectsLimit);
     }
     return new DefaultResponse($response->getStatusCode(), $response->getContentType(), $response->getBody(true), $response->getContentLength());
 }
 private function getSignedHeaders(Request $request)
 {
     $result = '';
     foreach ($request->getHeaders() as $key => $val) {
         if ($result) {
             $result .= ';' . $key;
         } else {
             $result .= $key;
         }
     }
     return strtolower($result);
 }
 private function applyDefaultRequestHeaders(Request $request)
 {
     $headers = $request->getHeaders();
     $headers['Accept'] = 'application/json';
     $userAgentBuilder = new UserAgentBuilder();
     $headers['User-Agent'] = $userAgentBuilder->setOsName(php_uname('s'))->setOsVersion(php_uname('r'))->setPhpVersion(phpversion())->build();
     if ($request->getBody()) {
         $headers['Content-Type'] = 'application/json';
     }
     $request->setHeaders($headers);
 }
 private function applyDefaultRequestHeaders(Request $request)
 {
     $headers = $request->getHeaders();
     $headers['Accept'] = 'application/json';
     $userAgentBuilder = new UserAgentBuilder();
     $headers['User-Agent'] = $userAgentBuilder->setOsName(php_uname('s'))->setOsVersion(php_uname('r'))->setPhpVersion(phpversion())->build();
     if ($body = $request->getBody()) {
         $headers['Content-Type'] = 'application/json';
         if (strpos($request->getResourceUrl(), '/oauth/token')) {
             $arr = json_decode($body);
             $arr = (array) $arr;
             ksort($arr);
             $body = http_build_query($arr);
             $request->setBody($body, strlen($body));
             $headers['Content-Type'] = 'application/x-www-form-urlencoded';
             $headers['Content-Length'] = $request->getContentLength();
         }
     }
     $request->setHeaders($headers);
 }