Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
],
"require": {
"php": ">=5.4.0",
"guzzlehttp/guzzle": "~6.0"
"guzzlehttp/guzzle": "~7.0",
"ext-json": "*"
}
,
"require-dev": {
"roave/security-advisories": "dev-latest"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion src/Base.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace DevIT\Hasoffers;

class Base
Expand Down Expand Up @@ -92,7 +93,7 @@ public function setEndpointType($endpointType)
/**
* Get the API Endpoint Name.
*
* @return object
* @return string
*/
public function getEndpointName()
{
Expand Down
48 changes: 35 additions & 13 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace DevIT\Hasoffers;

use Exception;
use GuzzleHttp\Client as GuzzleClient;

class Client
Expand Down Expand Up @@ -73,11 +75,15 @@ public function __construct($apiKey, $networkId = null)

$this->setNetworkId($networkId);

$this->setHttpClient(new GuzzleClient([
'defaults' => [
'headers' => $this->getHeaders(),
],
]));
$this->setHttpClient(
new GuzzleClient(
[
'defaults' => [
'headers' => $this->getHeaders(),
],
]
)
);
}

/**
Expand All @@ -101,13 +107,14 @@ public function api($class)
* @param array $parameters
*
* @return object
* @throws Exception
*/
public function get($apiMethod, $parameters = [])
{
$requestUrl = $this->buildUrl($apiMethod, $parameters);
$requestUrl = urldecode($requestUrl);

$response = $this->getHttpClient()->get($requestUrl);
$response = $this->getHttpClient()->get($requestUrl);

return $this->handleResponse($response);
}
Expand All @@ -116,20 +123,34 @@ public function get($apiMethod, $parameters = [])
* Build the request url.
*
* @param string $apiMethod
* @param array $parameter
* @param $parameters
*
* @return string
*/
private function buildUrl($apiMethod, $parameters)
{
$url = null;
switch ($this->getApiType()) {
case 'Brand':
$url = sprintf($this->apiUrlBrand, $this->getNetworkId(), $this->getApiNamespace(), $apiMethod, $this->getApiKey());
break;
$url = sprintf(
$this->apiUrlBrand,
$this->getNetworkId(),
$this->getApiNamespace(),
$apiMethod,
$this->getApiKey()
);
break;

case 'Affiliate':
$url = sprintf($this->apiUrlAffiliate, $this->getNetworkId(), $this->getApiType(), $this->getApiNamespace(), $apiMethod, $this->getApiKey());
break;
$url = sprintf(
$this->apiUrlAffiliate,
$this->getNetworkId(),
$this->getApiType(),
$this->getApiNamespace(),
$apiMethod,
$this->getApiKey()
);
break;
}

return $url.'&'.http_build_query($parameters);
Expand All @@ -141,17 +162,18 @@ private function buildUrl($apiMethod, $parameters)
* @param object $response
*
* @return object
* @throws Exception
*/
private function handleResponse($response)
{
$statusCode = $response->getStatusCode();
$body = json_decode($response->getBody());
$body = json_decode($response->getBody(), true);

if ($statusCode >= 200 && $statusCode < 300) {
return $body;
}

throw new \Exception($response->getBody(), $statusCode);
throw new Exception($response->getBody(), $statusCode);
}

/**
Expand Down