2015-03-23 23:41:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service;
|
|
|
|
|
|
|
|
use Guzzle\Service\Client;
|
|
|
|
use Guzzle\Http\Message\Request as GuzzleRequest;
|
|
|
|
use Guzzle\Http\Message\Response as GuzzleResponse;
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
/**
|
|
|
|
* @todo Implement commands
|
|
|
|
* @see https://github.com/misd-service-development/guzzle-bundle/blob/master/Resources/doc/serialization.md
|
|
|
|
* @see https://github.com/misd-service-development/guzzle-bundle/blob/master/Resources/doc/clients.md
|
|
|
|
* @see https://github.com/misd-service-development/guzzle-bundle/blob/master/Resources/doc/param_converter.md
|
|
|
|
*/
|
2015-03-23 23:41:41 +00:00
|
|
|
class AbstractApi
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Client HTTP-client from Guzzle
|
|
|
|
*/
|
|
|
|
protected $client;
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
/**
|
|
|
|
* @var bool Use HTTPS instead of HTTP
|
|
|
|
*/
|
|
|
|
protected $useHttps;
|
|
|
|
|
2015-06-02 11:48:39 +00:00
|
|
|
/**
|
|
|
|
* @var string Authentication token for API
|
|
|
|
*/
|
|
|
|
protected $authToken;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string CSRF-token for API
|
|
|
|
*/
|
|
|
|
protected $csRfToken;
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
/**
|
|
|
|
* @param Client $httpClient HTTP-client from Guzzle
|
|
|
|
* @param bool $https Use HTTPS instead of HTTP
|
|
|
|
* @param string $baseUrl Base URL for API
|
|
|
|
*/
|
|
|
|
public function __construct(Client $httpClient, $https = true, $baseUrl = null)
|
2015-03-23 23:41:41 +00:00
|
|
|
{
|
|
|
|
$this->client = $httpClient;
|
2015-05-28 22:47:06 +00:00
|
|
|
$this->useHttps = ($https) ? true : false;
|
|
|
|
|
|
|
|
if ($baseUrl) {
|
|
|
|
$this->setBaseUrl($baseUrl);
|
|
|
|
}
|
2015-03-23 23:41:41 +00:00
|
|
|
}
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-03-23 23:41:41 +00:00
|
|
|
/**
|
|
|
|
* Make GET request and return Response object
|
2015-06-02 11:48:39 +00:00
|
|
|
*
|
2015-05-28 22:47:06 +00:00
|
|
|
* @param string $path Request path
|
|
|
|
* @param array $parameters Key => Value array of query parameters
|
2015-03-23 23:41:41 +00:00
|
|
|
* @return GuzzleResponse
|
|
|
|
*/
|
2015-05-28 22:47:06 +00:00
|
|
|
public function sendGetRequest($path, array $parameters = [])
|
2015-03-23 23:41:41 +00:00
|
|
|
{
|
2015-06-02 11:48:39 +00:00
|
|
|
/** @var GuzzleRequest $request */
|
2015-03-23 23:41:41 +00:00
|
|
|
$request = $this->client->get($path);
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
$query = $request->getQuery();
|
|
|
|
|
|
|
|
foreach ($parameters as $parameter => $value) {
|
|
|
|
$query->set($parameter, $value);
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:41:41 +00:00
|
|
|
return $request->send();
|
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2015-06-02 11:48:39 +00:00
|
|
|
/**
|
|
|
|
* Make POST request and return Response object
|
|
|
|
*
|
|
|
|
* @param string $path Request path
|
|
|
|
* @param array $parameters Key => Value array of request data
|
|
|
|
* @return GuzzleResponse
|
|
|
|
*/
|
|
|
|
public function sendPostRequest($path, array $parameters = [])
|
|
|
|
{
|
|
|
|
/** @var GuzzleRequest $request */
|
|
|
|
$request = $this->client->post($path, null, null, [
|
|
|
|
'form_params' => $parameters,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $request->send();
|
|
|
|
}
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
/**
|
|
|
|
* Make GET request and return data from response
|
|
|
|
*
|
|
|
|
* @param string $path Path template
|
|
|
|
* @param array $parameters Parameters array used to fill path template
|
|
|
|
* @param bool $decodeJsonResponse Decode JSON or return plaintext
|
|
|
|
* @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays
|
2015-06-02 11:48:39 +00:00
|
|
|
* @return mixed
|
2015-05-28 22:47:06 +00:00
|
|
|
*/
|
|
|
|
public function getGetRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false)
|
|
|
|
{
|
|
|
|
$response = $this->sendGetRequest($path, $parameters);
|
|
|
|
|
|
|
|
if ($decodeJsonResponse) {
|
|
|
|
if ($decodeJsonToObjects) {
|
|
|
|
return json_decode($response->getBody(true));
|
|
|
|
} else {
|
|
|
|
return $response->json();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $response->getBody(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make POST request and return data from response
|
2015-06-02 11:48:39 +00:00
|
|
|
*
|
|
|
|
* @param string $path Path template
|
|
|
|
* @param array $parameters Parameters array used to fill path template
|
|
|
|
* @param bool $decodeJsonResponse Decode JSON or return plaintext
|
|
|
|
* @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays
|
|
|
|
* @return mixed
|
2015-05-28 22:47:06 +00:00
|
|
|
*/
|
2015-06-02 11:48:39 +00:00
|
|
|
public function getPostRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false)
|
2015-05-28 22:47:06 +00:00
|
|
|
{
|
2015-06-02 11:48:39 +00:00
|
|
|
$response = $this->sendPostRequest($path, $parameters);
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2015-06-02 11:48:39 +00:00
|
|
|
if ($decodeJsonResponse) {
|
|
|
|
if ($decodeJsonToObjects) {
|
|
|
|
return json_decode($response->getBody(true));
|
|
|
|
} else {
|
|
|
|
return $response->json();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $response->getBody(true);
|
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get HTTP client base URL
|
|
|
|
*
|
|
|
|
* @return string Base URL of client
|
|
|
|
*/
|
|
|
|
public function getBaseUrl()
|
|
|
|
{
|
|
|
|
return $this->client->getBaseUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set HTTP client base URL
|
|
|
|
*
|
|
|
|
* @param string $baseUrl Base URL of API
|
|
|
|
* @param bool $useProtocol Do not change URL scheme (http/https) defined in $baseUrl
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setBaseUrl($baseUrl, $useProtocol = false)
|
|
|
|
{
|
|
|
|
// Overriding protocol
|
|
|
|
if (!$useProtocol) {
|
|
|
|
$baseUrl = str_replace(['http://', 'https://',], ($this->useHttps) ? 'https://' : 'http://', $baseUrl);
|
|
|
|
}
|
|
|
|
// Adding missing protocol
|
|
|
|
if ((false === strpos(strtolower($baseUrl), 'http://')) && (false === strpos(strtolower($baseUrl), 'https://'))) {
|
|
|
|
$baseUrl = (($this->useHttps) ? 'https://' : 'http://') . $baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->client->setBaseUrl($baseUrl);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if API service uses HTTPS
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isHttps()
|
|
|
|
{
|
|
|
|
return $this->useHttps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable HTTPS
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function enableHttps()
|
|
|
|
{
|
|
|
|
$this->useHttps = true;
|
|
|
|
$this->setBaseUrl($this->getBaseUrl());
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable HTTPS
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function disableHttps()
|
|
|
|
{
|
|
|
|
$this->useHttps = false;
|
|
|
|
$this->setBaseUrl($this->getBaseUrl());
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2015-03-23 23:41:41 +00:00
|
|
|
}
|