Post requests test implementation.

This commit is contained in:
Alexey Skobkin 2015-06-02 14:48:39 +03:00
parent ca28ebd67d
commit 659a930037

View file

@ -24,6 +24,16 @@ class AbstractApi
*/ */
protected $useHttps; protected $useHttps;
/**
* @var string Authentication token for API
*/
protected $authToken;
/**
* @var string CSRF-token for API
*/
protected $csRfToken;
/** /**
* @param Client $httpClient HTTP-client from Guzzle * @param Client $httpClient HTTP-client from Guzzle
* @param bool $https Use HTTPS instead of HTTP * @param bool $https Use HTTPS instead of HTTP
@ -41,14 +51,14 @@ class AbstractApi
/** /**
* Make GET request and return Response object * Make GET request and return Response object
** *
* @param string $path Request path * @param string $path Request path
* @param array $parameters Key => Value array of query parameters * @param array $parameters Key => Value array of query parameters
* @return GuzzleResponse * @return GuzzleResponse
*/ */
public function sendGetRequest($path, array $parameters = []) public function sendGetRequest($path, array $parameters = [])
{ {
/** @var $request GuzzleRequest */ /** @var GuzzleRequest $request */
$request = $this->client->get($path); $request = $this->client->get($path);
$query = $request->getQuery(); $query = $request->getQuery();
@ -60,6 +70,23 @@ class AbstractApi
return $request->send(); return $request->send();
} }
/**
* 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();
}
/** /**
* Make GET request and return data from response * Make GET request and return data from response
* *
@ -67,7 +94,7 @@ class AbstractApi
* @param array $parameters Parameters array used to fill path template * @param array $parameters Parameters array used to fill path template
* @param bool $decodeJsonResponse Decode JSON or return plaintext * @param bool $decodeJsonResponse Decode JSON or return plaintext
* @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays * @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays
* @return array|string * @return mixed
*/ */
public function getGetRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false) public function getGetRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false)
{ {
@ -86,11 +113,26 @@ class AbstractApi
/** /**
* Make POST request and return data from response * Make POST request and return data from response
* @todo implement method *
* @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
*/ */
public function getPostRequestData() public function getPostRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false)
{ {
$response = $this->sendPostRequest($path, $parameters);
if ($decodeJsonResponse) {
if ($decodeJsonToObjects) {
return json_decode($response->getBody(true));
} else {
return $response->json();
}
} else {
return $response->getBody(true);
}
} }
/** /**