From 215e3ae4b6628b549f2fb8af12d3437b0206d779 Mon Sep 17 00:00:00 2001 From: Grzegorz Gorczyca Date: Mon, 21 Dec 2020 15:43:49 +0100 Subject: [PATCH 1/2] TICKETS-9549 - support for zip files --- README.md | 15 +++++++++++++++ src/FreshMail/Client.php | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/README.md b/README.md index 2d7ad1b..286fa64 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,21 @@ Below some simple examples, for whole API function see [full API V2 doc](https:/ $apiClient->doRequest('subscriber/add', $data); +#### get file from async api + + use \FreshMail\ApiV2\Client; + + $token = 'MY_APP_TOKEN'; + $apiClient = new Client($token); + + $data = [ + 'id_job' => 'XXX' + ]; + + $zipContent = $apiClient->doFileRequest('async_result/getFile', $data); + + file_put_contents('/testLocation/testfile.zip', $zipContent); + ## Proxy setup To use proxy You can pass Your own GuzzleHttp Client: diff --git a/src/FreshMail/Client.php b/src/FreshMail/Client.php index 372eea7..2665d8f 100644 --- a/src/FreshMail/Client.php +++ b/src/FreshMail/Client.php @@ -80,6 +80,33 @@ public function doRequest(string $uri, array $params = []) } } + /** + * @param $uri + * @param array $params + * @return string + * @throws Exception + */ + public function doFileRequest(string $uri, array $params = []) + { + try { + $method = ($params) ? 'POST' : 'GET'; + + $response = $this->guzzle->request($method, $uri, $this->getRequestOptions($params)); + if ($response->getHeaderLine('Content-Type') !== 'application/zip') { + throw new ServerException(sprintf('Response content type is not supported: %s', $response->getHeaderLine('Content-Type'))); + } + return $response->getBody()->getContents(); + } catch (\GuzzleHttp\Exception\ClientException $exception) { + if ($exception->getCode() == 401) { + throw new UnauthorizedException('Request unauthorized'); + } + + throw new \FreshMail\ApiV2\ClientException(sprintf('Connection error, error message: '.$exception->getMessage())); + } catch (\GuzzleHttp\Exception\ConnectException $exception) { + throw new ConnectionException(sprintf('Connection error, error message: '.$exception->getMessage())); + } + } + /** * @param LoggerInterface $logger */ From 0f5b7ad652e3ce5e894324abf95ccb045e990bc1 Mon Sep 17 00:00:00 2001 From: Grzegorz Gorczyca Date: Mon, 21 Dec 2020 15:46:13 +0100 Subject: [PATCH 2/2] TICKETS-9549 - fix text --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 286fa64..1faa4f5 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Below some simple examples, for whole API function see [full API V2 doc](https:/ $apiClient->doRequest('subscriber/add', $data); -#### get file from async api +#### Get file from async api use \FreshMail\ApiV2\Client;