Exemplo n.º 1
0
 /**
  * Constructor.
  *
  * @param ClientInterface $httpClient
  * @param array           $options
  * @param LoggerInterface $logger
  */
 public function __construct(ClientInterface $httpClient, array $options, LoggerInterface $logger = null)
 {
     $this->httpClient = $httpClient;
     $scheme = isset($options['scheme']) ? $options['scheme'] : static::DEFAULT_SCHEME;
     $host = isset($options['host']) ? $options['host'] : static::DEFAULT_HOST;
     $port = isset($options['port']) ? $options['port'] : static::DEFAULT_PORT;
     $pathPrefix = isset($options['path_prefix']) ? $options['path_prefix'] : static::DEFAULT_PATH_PREFIX;
     $accessKey = isset($options['access_key']) ? $options['access_key'] : null;
     $secretKey = isset($options['secret_key']) ? $options['secret_key'] : null;
     $this->scheme = $scheme;
     $this->host = $host;
     $this->port = $port;
     $this->pathPrefix = $pathPrefix;
     $this->accessKey = $accessKey;
     $this->secretKey = $secretKey;
     $this->companyName = isset($options['company']) ? $options['company'] : null;
     $this->collectionName = isset($options['collection']) ? $options['collection'] : null;
     $this->options = $options;
     $this->logger = $logger;
     $exceptionFactory = new NamespaceExceptionFactory(new EngineExceptionParser(), 'Sajari\\Engine\\Exception', 'Sajari\\Engine\\Exception\\EngineException');
     $this->httpClient->addSubscriber(new StatusCodeListener());
     $this->httpClient->addSubscriber(new ExceptionListener($exceptionFactory));
     $connectTimeout = 10;
     $timeout = 10;
     if (isset($this->options['curl.options']['CURLOPT_CONNECTTIMEOUT'])) {
         $connectTimeout = (int) $this->options['curl.options']['CURLOPT_CONNECTTIMEOUT'];
     }
     if (isset($this->options['curl.options']['CURLOPT_TIMEOUT'])) {
         $timeout = (int) $this->options['curl.options']['CURLOPT_TIMEOUT'];
     }
     $this->httpClient->setBaseUrl(sprintf('%s://%s', $this->scheme, $this->host));
     $this->httpClient->setConfig(array(HttpClient::SSL_CERT_AUTHORITY => false, HttpClient::CURL_OPTIONS => array(CURLOPT_CONNECTTIMEOUT => $connectTimeout, CURLOPT_TIMEOUT => $timeout)));
 }
Exemplo n.º 2
0
 /**
  * Perform an HTTP request on MusicBrainz
  *
  * @param  string $path
  * @param  array  $params
  * @param  string $options
  * @param  boolean $isAuthRequred
  * @return array
  */
 public function call($path, array $params = array(), array $options = array(), $isAuthRequred = false)
 {
     if ($options['user-agent'] == '') {
         throw new Exception('You must set a valid User Agent before accessing the MusicBrainz API');
     }
     $this->client->setBaseUrl(MbClient::URL);
     $this->client->setConfig(array('data' => $params));
     $request = $this->client->get($path . '{?data*}');
     $request->setHeader('Accept', 'application/json');
     $request->setHeader('User-Agent', $options['user-agent']);
     if ($isAuthRequred) {
         if ($options['user'] != null && $options['password'] != null) {
             $request->setAuth($options['user'], $options['password'], CURLAUTH_DIGEST);
         } else {
             throw new Exception('Authentication is required');
         }
     }
     $request->getQuery()->useUrlEncoding(false);
     return $request->send()->json();
 }