From 8f5ba1dc2ea57acceaf7d3778d7844c21a961bdf Mon Sep 17 00:00:00 2001 From: RandomWeasel <16453825+RandomWeasel@users.noreply.github.com> Date: Wed, 31 Oct 2018 12:36:53 +0000 Subject: [PATCH] Allow Harvest API Credentials be added later - Update Gateway to allow Harvest API Credentials to be set after initialisation: - Get credentials from config in __construct method - remove exception thrown by $this->hasCredentials in __construct, as they may not be set at this point - modify get() method to explicitly set headers with credentials from config --- src/Api/Gateway.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Api/Gateway.php b/src/Api/Gateway.php index b24be9b..03e7671 100644 --- a/src/Api/Gateway.php +++ b/src/Api/Gateway.php @@ -19,9 +19,9 @@ public function __construct() $this->token = config('harvest.personal_access_token'); $this->account_id = config('harvest.account_id'); - if (! $this->hasCredentials()) { - throw new Exception('Credentials not found, please ensure you published the package config file with artisan vendor:publish'); - } +// if (! $this->hasCredentials()) { +// throw new Exception('Credentials not found, please ensure you published the package config file with artisan vendor:publish'); +// } $apiClient = new Client([ 'headers' => [ @@ -44,8 +44,17 @@ public function getWithoutBase($uri, $data = null) public function get($uri, $data = null) { + //explicitly pass the headers here + //this accounts for a case where the harvest credentials weren't set when __construct was run + //or where they have since been updated + $response = $this->apiClient->request('GET', $this->uri . $uri, [ - 'json' => $data + 'json' => $data, + 'headers' => [ + 'Authorization' => 'Bearer ' . config('harvest.personal_access_token'), + 'Harvest-Account-Id' => config('harvest.account_id'), + 'User-Agent' => 'Harvest API App' + ] ]); return $this->transformResponse($response); @@ -87,4 +96,4 @@ public function transformResponse($response) { return (new Response($response))->toObject(); } -} \ No newline at end of file +}