From 659a93003768952da0349f8f49c3e30d2ac4313b Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 2 Jun 2015 14:48:39 +0300 Subject: [PATCH] Post requests test implementation. --- .../PointToolsBundle/Service/AbstractApi.php | 52 +++++++++++++++++-- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/AbstractApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/AbstractApi.php index 2bcdea5..7affc4f 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/AbstractApi.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/AbstractApi.php @@ -24,6 +24,16 @@ class AbstractApi */ 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 bool $https Use HTTPS instead of HTTP @@ -41,14 +51,14 @@ class AbstractApi /** * Make GET request and return Response object - ** + * * @param string $path Request path * @param array $parameters Key => Value array of query parameters * @return GuzzleResponse */ public function sendGetRequest($path, array $parameters = []) { - /** @var $request GuzzleRequest */ + /** @var GuzzleRequest $request */ $request = $this->client->get($path); $query = $request->getQuery(); @@ -60,6 +70,23 @@ class AbstractApi 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 * @@ -67,7 +94,7 @@ class AbstractApi * @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 array|string + * @return mixed */ public function getGetRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false) { @@ -86,11 +113,26 @@ class AbstractApi /** * 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); + } } /**