Skip to content

Commit

Permalink
Refactor Client
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Oct 12, 2023
1 parent 9bc1669 commit 422418a
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions lib/ApiClient/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,14 @@ private function execute(
array $formData = []
): Response {
try {
if ($formData) {
/** @var GuzzleResponse $response */
$guzzleResponse = $this->guzzle->request(
$method,
$url,
[
'headers' => $headers,
'form_params' => $formData,
'timeout' => 30
]
);
} else {
/** @var GuzzleResponse $response */
$guzzleResponse = $this->guzzle->request(
$method,
$url,
[
'headers' => $headers,
'body' => $content,
'timeout' => 30
]
);
}
$guzzleResponse = $this->doExecute(
$method,
$url,
$headers,
$content,
$formData
);

} catch (ConnectException $e) {
if (str_contains($e->getMessage(), 'cURL error 28')) {
throw new HttpTimeOutException();
Expand All @@ -100,4 +85,32 @@ private function execute(
$guzzleResponse->getHeaders()
);
}

private function doExecute(
string $method,
string $url,
array $headers,
?string $content,
array $formData
): GuzzleResponse {
$requestData = [
'headers' => $headers,
'timeout' => 30
];

if ($formData) {
$requestData['form_params'] = $formData;
} else {
$requestData['body'] = $content;
}

/** @var GuzzleResponse $response */
$guzzleResponse = $this->guzzle->request(
$method,
$url,
$requestData
);

return $guzzleResponse;
}
}

0 comments on commit 422418a

Please sign in to comment.