-
Notifications
You must be signed in to change notification settings - Fork 141
/
IntercomClient.php
135 lines (109 loc) · 3.19 KB
/
IntercomClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Intercom;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\stream_for;
class IntercomClient {
/** @var Client $http_client */
private $http_client;
/** @var string API user authentication */
protected $usernamePart;
/** @var string API password authentication */
protected $passwordPart;
/** @var IntercomUsers $users */
public $users;
/** @var IntercomEvents $events */
public $events;
/** @var IntercomCompanies $companies */
public $companies;
/** @var IntercomMessages $messages */
public $messages;
/** @var IntercomConversations $conversations */
public $conversations;
/** @var IntercomLeads $leads */
public $leads;
/** @var IntercomAdmins $admins */
public $admins;
/** @var IntercomTags $tags */
public $tags;
/** @var IntercomCounts $counts */
public $counts;
/** @var IntercomBulk $bulk */
public $bulk;
public function __construct($usernamePart, $passwordPart)
{
$this->setDefaultClient();
$this->users = new IntercomUsers($this);
$this->events = new IntercomEvents($this);
$this->companies = new IntercomCompanies($this);
$this->messages = new IntercomMessages($this);
$this->conversations = new IntercomConversations($this);
$this->leads = new IntercomLeads($this);
$this->admins = new IntercomAdmins($this);
$this->tags = new IntercomTags($this);
$this->counts = new IntercomCounts($this);
$this->bulk = new IntercomBulk($this);
$this->usernamePart = $usernamePart;
$this->passwordPart = $passwordPart;
}
private function setDefaultClient()
{
$this->http_client = new Client();
}
public function setClient($client)
{
$this->http_client = $client;
}
public function post($endpoint, $json)
{
$response = $this->http_client->request('POST', "https://api.intercom.io/$endpoint", [
'json' => $json,
'auth' => $this->getAuth(),
'headers' => [
'Accept' => 'application/json'
]
]);
return $this->handleResponse($response);
}
public function delete($endpoint, $json)
{
$response = $this->http_client->request('DELETE', "https://api.intercom.io/$endpoint", [
'json' => $json,
'auth' => $this->getAuth(),
'headers' => [
'Accept' => 'application/json'
]
]);
return $this->handleResponse($response);
}
public function get($endpoint, $query)
{
$response = $this->http_client->request('GET', "https://api.intercom.io/$endpoint", [
'query' => $query,
'auth' => $this->getAuth(),
'headers' => [
'Accept' => 'application/json'
]
]);
return $this->handleResponse($response);
}
public function nextPage($pages)
{
$response = $this->http_client->request('GET', $pages['next'], [
'auth' => $this->getAuth(),
'headers' => [
'Accept' => 'application/json'
]
]);
return $this->handleResponse($response);
}
public function getAuth()
{
return [$this->usernamePart, $this->passwordPart];
}
private function handleResponse(Response $response){
$stream = stream_for($response->getBody());
$data = json_decode($stream->getContents());
return $data;
}
}