-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: allow dependency injection of httpClient #2
Open
andheiberg
wants to merge
1
commit into
iagomelanias:master
Choose a base branch
from
andheiberg:refactor/di-http-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,43 @@ | |
|
||
namespace Heap; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\Exception\ServerException; | ||
use Heap\Exception\HeapException; | ||
use Heap\Helper\Request; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class Client | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $apiUri = 'https://heapanalytics.com/api'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $appId; | ||
|
||
/** | ||
* @var ClientInterface | ||
*/ | ||
private $httpClient; | ||
|
||
/** | ||
* Client constructor. | ||
* | ||
* @param null $appId | ||
* @param null $appId | ||
* @param ClientInterface|null $httpClient | ||
*/ | ||
public function __construct($appId) | ||
public function __construct($appId, ClientInterface $httpClient = null) | ||
{ | ||
if (! $httpClient) { | ||
$httpClient = new \GuzzleHttp\Client(); | ||
} | ||
|
||
$this->appId = $appId; | ||
$this->request = new Request(); | ||
$this->httpClient = $httpClient; | ||
} | ||
|
||
/** | ||
|
@@ -35,20 +56,21 @@ public function setAppId($appId) | |
} | ||
|
||
/** | ||
* @param $event | ||
* @param $identity | ||
* @param $event | ||
* @param $identity | ||
* @param array $properties | ||
* | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @return ResponseInterface | ||
* @throws HeapException | ||
* @throws GuzzleException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Within this scope I see HeapException being thrown. Why GuzzleException has been added to the doc block? |
||
*/ | ||
public function track($event, $identity, $properties = array()) | ||
{ | ||
if(!$event || empty($event)) { | ||
if (!$event || empty($event)) { | ||
throw new HeapException('You need to set the event name.'); | ||
} | ||
|
||
if(!$identity || empty($identity)) { | ||
if (!$identity || empty($identity)) { | ||
throw new HeapException('You need to set the identity. More info: https://heapanalytics.com/docs/server-side'); | ||
} | ||
|
||
|
@@ -59,19 +81,20 @@ public function track($event, $identity, $properties = array()) | |
'identity' => $identity, | ||
); | ||
|
||
return $this->request->call('POST', '/track', $data); | ||
return $this->call('POST', '/track', $data); | ||
} | ||
|
||
/** | ||
* @param $identity | ||
* @param $identity | ||
* @param array $properties | ||
* | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @return ResponseInterface | ||
* @throws HeapException | ||
* @throws GuzzleException | ||
*/ | ||
public function addUserProperties($identity, $properties = array()) | ||
{ | ||
if(!$identity || empty($identity)) { | ||
if (!$identity || empty($identity)) { | ||
throw new HeapException('You need to set the identity. More info: https://heapanalytics.com/docs/server-side'); | ||
} | ||
|
||
|
@@ -81,6 +104,43 @@ public function addUserProperties($identity, $properties = array()) | |
'identity' => $identity, | ||
); | ||
|
||
return $this->request->call('POST', '/add_user_properties', $data); | ||
return $this->call('POST', '/add_user_properties', $data); | ||
} | ||
|
||
/** | ||
* @param $endpoint | ||
* | ||
* @return string | ||
*/ | ||
public function generateUri($endpoint) | ||
{ | ||
return $this->apiUri.$endpoint; | ||
} | ||
|
||
/** | ||
* @param string $method | ||
* @param string $endpoint | ||
* @param array $data | ||
* | ||
* @return ResponseInterface | ||
* @throws HeapException | ||
* @throws GuzzleException | ||
*/ | ||
public function call($method, $endpoint, $data) | ||
{ | ||
$fullUri = $this->generateUri($endpoint); | ||
|
||
try { | ||
$response = $this->httpClient->request($method, $fullUri, array( | ||
'headers' => array( | ||
'Content-Type' => 'application/json', | ||
), | ||
'body' => \GuzzleHttp\json_encode($data), | ||
)); | ||
} catch (ServerException $e) { | ||
throw new HeapException($e->getMessage()); | ||
} | ||
|
||
return $response; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about adding support for PSR-18 and using its ClientInterface?
https://www.php-fig.org/psr/psr-18/