/**
  * @param RequestInterface $request
  * @param array $options
  * @return RequestInterface
  */
 public function configure(RequestInterface $request, array $options)
 {
     $this->cleanQueryString = true;
     $changes['query'] = '';
     if (array_key_exists('fields', $options) && array_key_exists('excludeFields', $options)) {
         throw new \InvalidArgumentException('$options may not contain both fields and excludeFields');
     }
     if (array_key_exists('limit', $options)) {
         $changes['query'] .= $this->getQueryStringSeparator() . 'count=' . $options['limit'];
     }
     if (array_key_exists('offset', $options)) {
         $changes['query'] .= $this->getQueryStringSeparator() . 'offset=' . $options['offset'];
     }
     if (array_key_exists('fields', $options)) {
         $changes['query'] .= $this->getQueryStringSeparator() . 'fields=' . implode(',', $options['fields']);
     }
     if (array_key_exists('excludeFields', $options)) {
         $changes['query'] .= $this->getQueryStringSeparator() . 'exclude_fields=' . implode(',', $options['excludeFields']);
     }
     if (array_key_exists('filters', $options) && is_array($options['filters'])) {
         foreach ($options['filters'] as $field => $value) {
             $changes['query'] .= $this->getQueryStringSeparator() . "{$field}={$value}";
         }
     }
     return \GuzzleHttp\Psr7\modify_request($request, $changes);
 }
 /**
  * @param $endpoint
  * @param ServerRequestInterface $request
  *
  * @return ResponseInterface
  */
 public function proxy($endpoint, ServerRequestInterface $request)
 {
     /** @var ClientInterface $client */
     $client = $this->container->get('bangpound_guzzle_proxy.client.' . $endpoint);
     $rel = $request->getAttribute('path');
     if ($request->getQueryParams()) {
         $rel .= '?' . \GuzzleHttp\Psr7\build_query($request->getQueryParams());
     }
     $rel = new Uri($rel);
     $uri = $client->getConfig('base_url');
     $uri = new Uri($uri);
     $uri = Uri::resolve($uri, $rel);
     $request = \GuzzleHttp\Psr7\modify_request($request, array('uri' => $uri));
     $response = $client->send($request);
     if ($response->hasHeader('Transfer-Encoding')) {
         $response = $response->withoutHeader('Transfer-Encoding');
     }
     return $response;
 }
예제 #3
0
 protected function getQuoteDataFromAPI()
 {
     $api_path = '/api/v1/quote/all';
     $client = new GuzzleClient();
     $data = ['apitoken' => $this->api_token];
     $request = new \GuzzleHttp\Psr7\Request('GET', $this->quotebot_url . $api_path);
     $request = \GuzzleHttp\Psr7\modify_request($request, ['query' => http_build_query($data, null, '&', PHP_QUERY_RFC3986)]);
     // send request
     try {
         $response = $client->send($request);
     } catch (RequestException $e) {
         if ($response = $e->getResponse()) {
             // interpret the response and error message
             $code = $response->getStatusCode();
             try {
                 $json = json_decode($response->getBody(), true);
             } catch (Exception $parse_json_exception) {
                 // could not parse json
                 $json = null;
             }
             if ($json and isset($json['message'])) {
                 throw new Exception($json['message'], $code);
             }
         }
         // if no response, then just throw the original exception
         throw $e;
     }
     $code = $response->getStatusCode();
     if ($code == 204) {
         // empty content
         return [];
     }
     $json = json_decode($response->getBody(), true);
     if (!is_array($json)) {
         throw new Exception("Unexpected response", 1);
     }
     return $json;
 }
예제 #4
0
 protected function newAPIRequest($method, $path, $data = [])
 {
     $api_path = '/api/v1' . $path;
     $client = new GuzzleClient();
     if ($data and ($method == 'POST' or $method == 'PATCH')) {
         $body = \GuzzleHttp\Psr7\stream_for(json_encode($data));
         $headers = ['Content-Type' => 'application/json'];
         $request = new \GuzzleHttp\Psr7\Request($method, $this->xchain_url . $api_path, $headers, $body);
     } else {
         if ($method == 'GET') {
             $request = new \GuzzleHttp\Psr7\Request($method, $this->xchain_url . $api_path);
             $request = \GuzzleHttp\Psr7\modify_request($request, ['query' => http_build_query($data, null, '&', PHP_QUERY_RFC3986)]);
         } else {
             $request = new \GuzzleHttp\Psr7\Request($method, $this->xchain_url . $api_path);
         }
     }
     // add auth
     $request = $this->getAuthenticationGenerator()->addSignatureToGuzzle6Request($request, $this->api_token, $this->api_secret_key);
     // send request
     try {
         $response = $client->send($request);
     } catch (RequestException $e) {
         if ($response = $e->getResponse()) {
             // interpret the response and error message
             $code = $response->getStatusCode();
             try {
                 $json = json_decode($response->getBody(), true);
             } catch (Exception $parse_json_exception) {
                 // could not parse json
                 $json = null;
             }
             if ($json and isset($json['message'])) {
                 // throw an XChainException with the errorName
                 if (isset($json['errorName'])) {
                     $xchain_exception = new XChainException($json['message'], $code);
                     $xchain_exception->setErrorName($json['errorName']);
                     throw $xchain_exception;
                 }
                 // generic exception
                 throw new Exception($json['message'], $code);
             }
         }
         // if no response, then just throw the original exception
         throw $e;
     }
     $code = $response->getStatusCode();
     if ($code == 204) {
         // empty content
         return [];
     }
     $json = json_decode($response->getBody(), true);
     if (!is_array($json)) {
         throw new Exception("Unexpected response: " . $response->getBody(), 1);
     }
     return $json;
 }