diff --git a/README.md b/README.md index 2d7ad1b..1faa4f5 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 */