Skip to content

Commit 362e305

Browse files
GabrielAncachoran
authored andcommitted
Abstract the HTTP client using HTTPPlug (#270)
* Add http-plug and remove guzzle * Move client initialization to the constructor * Allow HTTP Plug 1.0 or 2.0 * Update tests * Clarify readme * Update error handling section in README * Rename setClient to setHttpClient * Minor improvements * Fix exception handling * Change client implementation link in readme
1 parent ea68849 commit 362e305

17 files changed

+327
-235
lines changed

README.md

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
# intercom-php
22

3-
[![Code
4-
Climate](https://codeclimate.com/repos/537da4a7e30ba062b101be9c/badges/2aa25d4736f09f40282e/gpa.svg)](https://codeclimate.com/repos/537da4a7e30ba062b101be9c/feed) [![Circle CI](https://circleci.com/gh/intercom/intercom-php.png?style=badge)](https://circleci.com/gh/intercom/intercom-php)
3+
[![Code Climate](https://codeclimate.com/repos/537da4a7e30ba062b101be9c/badges/2aa25d4736f09f40282e/gpa.svg)](https://codeclimate.com/repos/537da4a7e30ba062b101be9c/feed) [![Circle CI](https://circleci.com/gh/intercom/intercom-php.png?style=badge)](https://circleci.com/gh/intercom/intercom-php)
54

65
Official PHP bindings to the Intercom API
76

87
## Installation
98

109
This library supports PHP 7.1 and later
1110

12-
The recommended way to install intercom-php is through [Composer](https://getcomposer.org):
13-
14-
First, install Composer:
15-
16-
```sh
17-
curl -sS https://getcomposer.org/installer | php
18-
```
11+
This library uses [HTTPPlug](https://github.com/php-http/httplug) as HTTP client. HTTPPlug is an abstraction that allows this library to support many different HTTP Clients. Therefore, you need to provide it with an adapter for the HTTP library you prefer. You can find all the available adapters [in Packagist](https://packagist.org/providers/php-http/client-implementation). This documentation assumes you use the Guzzle6 Client, but you can replace it with any adapter that you prefer.
1912

20-
Next, install the latest intercom-php:
13+
The recommended way to install intercom-php is through [Composer](https://getcomposer.org):
2114

2215
```sh
23-
php composer.phar require intercom/intercom-php
24-
```
25-
26-
Finally, you need to require the library in your PHP application:
27-
28-
```php
29-
require "vendor/autoload.php";
16+
composer require intercom/intercom-php php-http/guzzle6-adapter
3017
```
3118

3219
## Clients
3320

34-
For OAuth or Access Tokens use:
21+
Initialize your client using your access token:
3522

3623
```php
3724
use Intercom\IntercomClient;
3825

39-
$client = new IntercomClient('<insert_token_here>', null);
26+
$client = new IntercomClient('<insert_token_here>');
4027
```
4128

4229
> If you already have an access token you can find it [here](https://app.intercom.com/a/apps/_/developer-hub). If you want to create or learn more about access tokens then you can find more info [here](https://developers.intercom.com/building-apps/docs/authorization#section-access-tokens).
4330
>
4431
> If you are building a third party application you can get your OAuth token by [setting-up-oauth](https://developers.intercom.com/building-apps/docs/authorization#section-oauth) for Intercom.
4532
33+
For most use cases the code snippet above should suffice. However, if needed, you can customize the Intercom client as follows:
34+
35+
```php
36+
use Intercom\IntercomClient;
37+
38+
$client = new IntercomClient('<insert_token_here>', null, ['Custom-Header' => 'value']);
39+
40+
$client->setHttpClient($myCustomHttpClient); // $myCustomHttpClient implements Psr\Http\Client\ClientInterface
41+
$client->setRequestFactory($myCustomRequestFactory); // $myCustomRequestFactory implements Http\Message\RequestFactory
42+
$client->setUriFactory($myCustomUriFactory); // $myCustomUriFactory implements Http\Message\UriFactory
43+
```
44+
4645
## Users
4746

4847
```php
@@ -458,19 +457,15 @@ while (!empty($resp->scroll_param) && sizeof($resp->users) > 0) {
458457

459458
## Exceptions
460459

461-
Exceptions are handled by [Guzzle](https://github.com/guzzle/guzzle).
460+
Exceptions are handled by HTTPPlug. Every exception thrown implements `Http\Client\Exception`. See the different exceptions that can be thrown [in the HTTPPlug documentation](http://docs.php-http.org/en/latest/httplug/exceptions.html).
462461
The Intercom API may return an unsuccessful HTTP response, for example when a resource is not found (404).
463-
If you want to catch errors you can wrap your API call into a try/catch:
462+
If you want to catch errors you can wrap your API call into a try/catch block:
464463

465464
```php
466-
use GuzzleHttp\Exception\ClientException;
467-
468465
try {
469466
$user = $client->users->getUser("570680a8a1bcbca8a90001b9");
470-
} catch(ClientException $e) {
471-
$response = $e->getResponse();
472-
$statusCode = $response->getStatusCode();
473-
if ($statusCode == '404') {
467+
} catch(Http\Client\Exception $e) {
468+
if ($e->getCode() == '404') {
474469
// Handle 404 error
475470
return;
476471
} else {

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@
2222
"require": {
2323
"php": ">= 7.1",
2424
"ext-json": "*",
25-
"guzzlehttp/guzzle": "~6.0"
25+
"php-http/httplug": "^1.0 || ^2.0",
26+
"php-http/client-implementation": "*",
27+
"php-http/discovery": "^1.4",
28+
"php-http/message": "^1.7",
29+
"psr/http-message": "^1.0"
2630
},
2731
"require-dev": {
2832
"phpunit/phpunit": "^7.0",
29-
"squizlabs/php_codesniffer": "^3.1"
33+
"squizlabs/php_codesniffer": "^3.1",
34+
"php-http/guzzle6-adapter": "^2.0"
3035
}
3136
}

src/IntercomAdmins.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Intercom;
44

5+
use Http\Client\Exception;
6+
57
class IntercomAdmins
68
{
79

@@ -25,8 +27,8 @@ public function __construct($client)
2527
*
2628
* @see https://developers.intercom.io/reference#list-admins
2729
* @param array $options
28-
* @return mixed
29-
* @throws \GuzzleHttp\Exception\GuzzleException
30+
* @return stdClass
31+
* @throws Exception
3032
*/
3133
public function getAdmins($options = [])
3234
{
@@ -39,8 +41,8 @@ public function getAdmins($options = [])
3941
* @see https://developers.intercom.com/v2.0/reference#view-an-admin
4042
* @param integer $id
4143
* @param array $options
42-
* @return mixed
43-
* @throws \GuzzleHttp\Exception\GuzzleException
44+
* @return stdClass
45+
* @throws Exception
4446
*/
4547
public function getAdmin($id, $options = [])
4648
{

src/IntercomBulk.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Intercom;
44

5+
use Http\Client\Exception;
6+
57
class IntercomBulk
68
{
79

@@ -24,8 +26,8 @@ public function __construct($client)
2426
* Creates Users in bulk.
2527
*
2628
* @param array $options
27-
* @return mixed
28-
* @throws \GuzzleHttp\Exception\GuzzleException
29+
* @return stdClass
30+
* @throws Exception
2931
*/
3032
public function users($options)
3133
{
@@ -36,8 +38,8 @@ public function users($options)
3638
* Creates Events in bulk.
3739
*
3840
* @param array $options
39-
* @return mixed
40-
* @throws \GuzzleHttp\Exception\GuzzleException
41+
* @return stdClass
42+
* @throws Exception
4143
*/
4244
public function events($options)
4345
{

0 commit comments

Comments
 (0)