Skip to content

Commit

Permalink
abstracted http request
Browse files Browse the repository at this point in the history
  • Loading branch information
tolu-paystack committed May 21, 2020
1 parent 9079b9d commit abc3f69
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
16 changes: 16 additions & 0 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,20 @@ public function setSecretKey($value)
{
$this->setParameter('secret_key', $value);
}

protected function sendRequest($method, $endpoint, array $data = null)
{

$headers = [
'Authorization' => 'Bearer ' . $this->getSecretKey(),
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
];

$response = $this->httpClient->request($method, $this->baseApiEndpoint. $endpoint, $headers, json_encode($data));
$responseData = json_decode((string)$response->getBody(), true);

return $responseData;

}
}
12 changes: 4 additions & 8 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ public function getData()
public function sendData($data)
{
try {
$headers = [
'Authorization' => 'Bearer ' . $this->getSecretKey(),
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
];

$response = $this->httpClient->request('GET', $this->getApiEndpoint(), $headers, json_encode($data));
$responseData = json_decode((string)$response->getBody(), true);

$response = $this->sendRequest('GET', $this->getApiEndpoint(), json_encode($data));

} catch (\Exception $e) {

throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
}

Expand Down
11 changes: 2 additions & 9 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,12 @@ public function getData()
public function sendData($data)
{
try {
$headers = [
'Authorization' => 'Bearer ' . $this->getSecretKey(),
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
];

$response = $this->httpClient->request('POST', $this->getApiEndpoint(), $headers, json_encode($data));
$responseData = json_decode((string)$response->getBody(), true);
$response = $this->sendRequest('POST', $this->getApiEndpoint(), $data);
} catch (\Exception $e) {
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
}

return $this->response = new PurchaseResponse($this, $responseData);
return $this->response = new PurchaseResponse($this, $response);
}

/**
Expand Down

0 comments on commit abc3f69

Please sign in to comment.