Skip to content

Commit d0aac16

Browse files
authored
Merge pull request #15 from FreshMail/feature-TICKETS-9549
TICKETS-9549 - support for zip files
2 parents 9c83728 + 0f5b7ad commit d0aac16

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ Below some simple examples, for whole API function see [full API V2 doc](https:/
6262

6363
$apiClient->doRequest('subscriber/add', $data);
6464

65+
#### Get file from async api
66+
67+
use \FreshMail\ApiV2\Client;
68+
69+
$token = 'MY_APP_TOKEN';
70+
$apiClient = new Client($token);
71+
72+
$data = [
73+
'id_job' => 'XXX'
74+
];
75+
76+
$zipContent = $apiClient->doFileRequest('async_result/getFile', $data);
77+
78+
file_put_contents('/testLocation/testfile.zip', $zipContent);
79+
6580
## Proxy setup
6681

6782
To use proxy You can pass Your own GuzzleHttp Client:

src/FreshMail/Client.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ public function doRequest(string $uri, array $params = [])
8383
}
8484
}
8585

86+
/**
87+
* @param $uri
88+
* @param array $params
89+
* @return string
90+
* @throws Exception
91+
*/
92+
public function doFileRequest(string $uri, array $params = [])
93+
{
94+
try {
95+
$method = ($params) ? 'POST' : 'GET';
96+
97+
$response = $this->guzzle->request($method, $uri, $this->getRequestOptions($params));
98+
if ($response->getHeaderLine('Content-Type') !== 'application/zip') {
99+
throw new ServerException(sprintf('Response content type is not supported: %s', $response->getHeaderLine('Content-Type')));
100+
}
101+
return $response->getBody()->getContents();
102+
} catch (\GuzzleHttp\Exception\ClientException $exception) {
103+
if ($exception->getCode() == 401) {
104+
throw new UnauthorizedException('Request unauthorized');
105+
}
106+
107+
throw new \FreshMail\ApiV2\ClientException(sprintf('Connection error, error message: '.$exception->getMessage()));
108+
} catch (\GuzzleHttp\Exception\ConnectException $exception) {
109+
throw new ConnectionException(sprintf('Connection error, error message: '.$exception->getMessage()));
110+
}
111+
}
112+
86113
/**
87114
* @param LoggerInterface $logger
88115
*/

0 commit comments

Comments
 (0)