Skip to content

Commit

Permalink
[FEATURE] Allow passing of request options and own error handling (#79)
Browse files Browse the repository at this point in the history
With this change, it is now possible to set options to be passed
with the request method on a per callee level. Each client creation
is able to accept options, that will be applied to each call to
any request function and be respected by Guzzle Requests.
The default behaviour will be as before that change.

Additionally, the exception handling and the Response wrapping
into a SevenShores\Hubspot\Http\Response object can be avoided.
So it is possible to care for the error handling in the own
application.
Again, the default behaviour remains unchanged, the response
is wrapped and SevenShores\Hubspot\Exceptions are thrown.
  • Loading branch information
maddy2101 authored and ryanwinchester committed Jan 18, 2017
1 parent e5d214c commit 93df25e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ $hubspot = new SevenShores\Hubspot\Factory([
```
*Note:* The Client class checks for a `HUBSPOT_SECRET` environment variable if you don't include an api key or oauth token during instantiation.

*Note:* You can prevent any error handling provided by this package by passing following options into client creation routine:
(applies also to `Factory::create()` and `Factory::createWithToken()`)

```php
$hubspot = new SevenShores\Hubspot\Factory([
'key' => 'demo',
'oauth' => false, // default
'base_url' => 'https://api.hubapi.com' // default
],
[
'http_errors' = true // pass any Guzzle related option to any request, e.g. throw no exceptions
],
false // return Guzzle Response object for any ->request(*) call
);
```

By setting `http_errors` to true, you will not receive any exceptions at all, but pure responses.
For possible options, see http://docs.guzzlephp.org/en/latest/request-options.html.

#### Get a single contact:

```php
Expand Down
30 changes: 18 additions & 12 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,42 @@ class Factory
/**
* C O N S T R U C T O R ( ^_^)y
*
* @param array $config An array of configurations. You need at least the 'key'.
* @param Client $client
* @param array $config An array of configurations. You need at least the 'key'.
* @param Client $client
* @param array $clientOptions options to be send with each request
* @param bool $wrapResponse wrap request response in own Response object
*/
function __construct($config = [], $client = null)
public function __construct($config = [], $client = null, $clientOptions = [], $wrapResponse = true)
{
$this->client = $client ?: new Client($config);
$this->client = $client ?: new Client($config, null, $clientOptions, $wrapResponse);
}

/**
* Create an instance of the service with an API key.
*
* @param string $api_key Hubspot API key.
* @param Client $client An Http client.
* @param string $api_key Hubspot API key.
* @param Client $client An Http client.
* @param array $clientOptions options to be send with each request
* @param bool $wrapResponse wrap request response in own Response object
* @return static
*/
static function create($api_key = null, $client = null)
public static function create($api_key = null, $client = null, $clientOptions = [], $wrapResponse = true)
{
return new static(['key' => $api_key], $client);
return new static(['key' => $api_key], $client, $clientOptions, $wrapResponse);
}

/**
* Create an instance of the service with an Oauth token.
*
* @param string $token Hubspot oauth access token.
* @param Client $client An Http client.
* @param string $token Hubspot oauth access token.
* @param Client $client An Http client.
* @param array $clientOptions options to be send with each request
* @param bool $wrapResponse wrap request response in own Response object
* @return static
*/
static function createWithToken($token, $client = null)
public static function createWithToken($token, $client = null, $clientOptions = [], $wrapResponse = true)
{
return new static(['key' => $token, 'oauth' => true], $client);
return new static(['key' => $token, 'oauth' => true], $client, $clientOptions, $wrapResponse);
}

/**
Expand Down
35 changes: 29 additions & 6 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace SevenShores\Hubspot\Http;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use SevenShores\Hubspot\Exceptions\BadRequest;
use SevenShores\Hubspot\Exceptions\InvalidArgument;

class Client
{
/** @var string */
public $key;

/** @var bool */
public $oauth;

Expand All @@ -23,18 +24,36 @@ class Client
/** @var \GuzzleHttp\Client */
public $client;

/**
* Guzzle allows options into its request method. Prepare for some defaults
* @var array
*/
protected $clientOptions = [];

/**
* if set to false, no Response object is created, but the one from Guzzle is directly returned
* comes in handy own error handling
*
* @var bool
*/
protected $wrapResponse = true;

/** @var string */
private $user_agent = "SevenShores_Hubspot_PHP/1.0.0-rc.1 (https://github.com/ryanwinchester/hubspot-php)";

/**
* Make it, baby.
*
* @param array $config Configuration array
* @param array $config Configuration array
* @param GuzzleClient $client The Http Client (Defaults to Guzzle)
* @throws \SevenShores\Hubspot\Exceptions\InvalidArgument
* @param array $clientOptions options to be passed to Guzzle upon each request
* @param bool $wrapResponse wrap request response in own Response object
*/
function __construct($config = [], $client = null)
public function __construct($config = [], $client = null, $clientOptions = [], $wrapResponse = true)
{
$this->clientOptions = $clientOptions;
$this->wrapResponse = $wrapResponse;

$this->key = isset($config["key"]) ? $config["key"] : getenv("HUBSPOT_SECRET");
if (empty($this->key)) {
throw new InvalidArgument("You must provide a Hubspot api key or token.");
Expand All @@ -61,20 +80,24 @@ function __construct($config = [], $client = null)
* @param array $options An array of options to send with the request
* @param string $query_string A query string to send with the request
* @param boolean $requires_auth Whether or not this HubSpot API endpoint requires authentication
* @return \SevenShores\Hubspot\Http\Response
* @return \SevenShores\Hubspot\Http\Response|ResponseInterface
* @throws \SevenShores\Hubspot\Exceptions\BadRequest
*/
function request($method, $endpoint, $options = [], $query_string = null, $requires_auth = true)
public function request($method, $endpoint, $options = [], $query_string = null, $requires_auth = true)
{
$url = $this->generateUrl($endpoint, $query_string, $requires_auth);

$options = array_merge($this->clientOptions, $options);
$options["headers"]["User-Agent"] = $this->user_agent;

if ($this->oauth2) {
$options["headers"]["Authorization"] = "Bearer " . $this->key;
}

try {
if ($this->wrapResponse === false) {
return $this->client->request($method, $url, $options);
}
return new Response($this->client->request($method, $url, $options));
} catch (\Exception $e) {
throw new BadRequest($e->getMessage(), $e->getCode(), $e);
Expand Down

0 comments on commit 93df25e

Please sign in to comment.