Example #1
0
 /**
  * Build request and execute him
  *
  * @param string $method
  * @param string $uri
  * @param array $data
  * @param string $contentType
  * @return Response
  */
 protected function makeRequest($method, $uri, array $data = NULL, $contentType = Http::CONTENT_JSON)
 {
     // Invoke events
     $this->trigger('onRequest', [$method, $uri, $data]);
     // Verify that client is authenticated
     if (!$this->client->hasToken()) {
         // Do authorization
         $this->doAuthorization();
     }
     $request = new Request();
     // Set-up URL
     $request->setUrl(Gateway::getFullApiUrl($uri));
     // Set-up headers
     $headers = ['Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->client->getToken()->accessToken, 'Content-Type' => $contentType];
     $request->setHeaders($headers);
     // Set-up opts
     $request->setOpts($this->options);
     // Set-up method
     switch ($method) {
         // GET =========================================
         case HttpClient::METHOD_GET:
             $request->appendOpts([CURLOPT_HTTPGET => TRUE]);
             break;
             // POST ========================================
         // POST ========================================
         case HttpClient::METHOD_POST:
             $request->appendOpts([CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $contentType === Http::CONTENT_FORM ? http_build_query($data) : json_encode($data)]);
             break;
         default:
             throw new InvalidStateException('Unsupported http method');
     }
     return $this->client->call($request);
 }