|
7 | 7 | use function GuzzleHttp\Psr7\stream_for; |
8 | 8 | use Psr\Http\Message\ResponseInterface; |
9 | 9 |
|
10 | | -class IntercomClient { |
11 | | - |
12 | | - /** @var Client $http_client */ |
13 | | - private $http_client; |
14 | | - |
15 | | - /** @var string API user authentication */ |
16 | | - protected $usernamePart; |
17 | | - |
18 | | - /** @var string API password authentication */ |
19 | | - protected $passwordPart; |
20 | | - |
21 | | - /** @var IntercomUsers $users */ |
22 | | - public $users; |
23 | | - |
24 | | - /** @var IntercomEvents $events */ |
25 | | - public $events; |
26 | | - |
27 | | - /** @var IntercomCompanies $companies */ |
28 | | - public $companies; |
29 | | - |
30 | | - /** @var IntercomMessages $messages */ |
31 | | - public $messages; |
32 | | - |
33 | | - /** @var IntercomConversations $conversations */ |
34 | | - public $conversations; |
35 | | - |
36 | | - /** @var IntercomLeads $leads */ |
37 | | - public $leads; |
38 | | - |
39 | | - /** @var IntercomAdmins $admins */ |
40 | | - public $admins; |
41 | | - |
42 | | - /** @var IntercomTags $tags */ |
43 | | - public $tags; |
44 | | - |
45 | | - /** @var IntercomCounts $counts */ |
46 | | - public $counts; |
47 | | - |
48 | | - /** @var IntercomBulk $bulk */ |
49 | | - public $bulk; |
50 | | - |
51 | | - /** |
52 | | - * IntercomClient constructor. |
53 | | - * @param string $usernamePart App ID. |
54 | | - * @param string $passwordPart Api Key. |
55 | | - */ |
56 | | - public function __construct($usernamePart, $passwordPart) |
57 | | - { |
58 | | - $this->setDefaultClient(); |
59 | | - $this->users = new IntercomUsers($this); |
60 | | - $this->events = new IntercomEvents($this); |
61 | | - $this->companies = new IntercomCompanies($this); |
62 | | - $this->messages = new IntercomMessages($this); |
63 | | - $this->conversations = new IntercomConversations($this); |
64 | | - $this->leads = new IntercomLeads($this); |
65 | | - $this->admins = new IntercomAdmins($this); |
66 | | - $this->tags = new IntercomTags($this); |
67 | | - $this->counts = new IntercomCounts($this); |
68 | | - $this->bulk = new IntercomBulk($this); |
69 | | - $this->notes = new IntercomNotes($this); |
70 | | - |
71 | | - $this->usernamePart = $usernamePart; |
72 | | - $this->passwordPart = $passwordPart; |
73 | | - } |
74 | | - |
75 | | - private function setDefaultClient() |
76 | | - { |
77 | | - $this->http_client = new Client(); |
78 | | - } |
79 | | - |
80 | | - /** |
81 | | - * Sets GuzzleHttp client. |
82 | | - * @param Client $client |
83 | | - */ |
84 | | - public function setClient($client) |
85 | | - { |
86 | | - $this->http_client = $client; |
87 | | - } |
88 | | - |
89 | | - /** |
90 | | - * Sends POST request to Intercom API. |
91 | | - * @param string $endpoint |
92 | | - * @param string $json |
93 | | - * @return mixed |
94 | | - * @throws \GuzzleHttp\Exception\GuzzleException |
95 | | - */ |
96 | | - public function post($endpoint, $json) |
97 | | - { |
98 | | - $response = $this->http_client->request('POST', "https://api.intercom.io/$endpoint", [ |
99 | | - 'json' => $json, |
100 | | - 'auth' => $this->getAuth(), |
101 | | - 'headers' => [ |
102 | | - 'Accept' => 'application/json' |
103 | | - ] |
104 | | - ]); |
105 | | - return $this->handleResponse($response); |
106 | | - } |
107 | | - |
108 | | - /** |
109 | | - * Sends DELETE request to Intercom API. |
110 | | - * @param string $endpoint |
111 | | - * @param string $json |
112 | | - * @return mixed |
113 | | - * @throws \GuzzleHttp\Exception\GuzzleException |
114 | | - */ |
115 | | - public function delete($endpoint, $json) |
116 | | - { |
117 | | - $response = $this->http_client->request('DELETE', "https://api.intercom.io/$endpoint", [ |
118 | | - 'json' => $json, |
119 | | - 'auth' => $this->getAuth(), |
120 | | - 'headers' => [ |
121 | | - 'Accept' => 'application/json' |
122 | | - ] |
123 | | - ]); |
124 | | - return $this->handleResponse($response); |
125 | | - } |
126 | | - |
127 | | - /** |
128 | | - * @param string $endpoint |
129 | | - * @param string $query |
130 | | - * @return mixed |
131 | | - * @throws \GuzzleHttp\Exception\GuzzleException |
132 | | - */ |
133 | | - public function get($endpoint, $query) |
134 | | - { |
135 | | - $response = $this->http_client->request('GET', "https://api.intercom.io/$endpoint", [ |
136 | | - 'query' => $query, |
137 | | - 'auth' => $this->getAuth(), |
138 | | - 'headers' => [ |
139 | | - 'Accept' => 'application/json' |
140 | | - ] |
141 | | - ]); |
142 | | - return $this->handleResponse($response); |
143 | | - } |
144 | | - |
145 | | - /** |
146 | | - * Returns next page of the result. |
147 | | - * @param array $pages |
148 | | - * @return mixed |
149 | | - * @throws \GuzzleHttp\Exception\GuzzleException |
150 | | - */ |
151 | | - public function nextPage($pages) |
152 | | - { |
153 | | - $response = $this->http_client->request('GET', $pages['next'], [ |
154 | | - 'auth' => $this->getAuth(), |
155 | | - 'headers' => [ |
156 | | - 'Accept' => 'application/json' |
157 | | - ] |
158 | | - ]); |
159 | | - return $this->handleResponse($response); |
160 | | - } |
161 | | - |
162 | | - /** |
163 | | - * Returns authentication parameters. |
164 | | - * @return array |
165 | | - */ |
166 | | - public function getAuth() |
167 | | - { |
168 | | - return [$this->usernamePart, $this->passwordPart]; |
169 | | - } |
170 | | - |
171 | | - /** |
172 | | - * @param Response $response |
173 | | - * @return mixed |
174 | | - */ |
175 | | - private function handleResponse(Response $response){ |
176 | | - $stream = stream_for($response->getBody()); |
177 | | - $data = json_decode($stream->getContents()); |
178 | | - return $data; |
179 | | - } |
| 10 | +class IntercomClient |
| 11 | +{ |
| 12 | + |
| 13 | + /** @var Client $http_client */ |
| 14 | + private $http_client; |
| 15 | + |
| 16 | + /** @var string API user authentication */ |
| 17 | + protected $usernamePart; |
| 18 | + |
| 19 | + /** @var string API password authentication */ |
| 20 | + protected $passwordPart; |
| 21 | + |
| 22 | + /** @var IntercomUsers $users */ |
| 23 | + public $users; |
| 24 | + |
| 25 | + /** @var IntercomEvents $events */ |
| 26 | + public $events; |
| 27 | + |
| 28 | + /** @var IntercomCompanies $companies */ |
| 29 | + public $companies; |
| 30 | + |
| 31 | + /** @var IntercomMessages $messages */ |
| 32 | + public $messages; |
| 33 | + |
| 34 | + /** @var IntercomConversations $conversations */ |
| 35 | + public $conversations; |
| 36 | + |
| 37 | + /** @var IntercomLeads $leads */ |
| 38 | + public $leads; |
| 39 | + |
| 40 | + /** @var IntercomAdmins $admins */ |
| 41 | + public $admins; |
| 42 | + |
| 43 | + /** @var IntercomTags $tags */ |
| 44 | + public $tags; |
| 45 | + |
| 46 | + /** @var IntercomSegments $segments */ |
| 47 | + public $segments; |
| 48 | + |
| 49 | + /** @var IntercomCounts $counts */ |
| 50 | + public $counts; |
| 51 | + |
| 52 | + /** @var IntercomBulk $bulk */ |
| 53 | + public $bulk; |
| 54 | + |
| 55 | + /** |
| 56 | + * IntercomClient constructor. |
| 57 | + * @param string $usernamePart App ID. |
| 58 | + * @param string $passwordPart Api Key. |
| 59 | + */ |
| 60 | + public function __construct($usernamePart, $passwordPart) |
| 61 | + { |
| 62 | + $this->setDefaultClient(); |
| 63 | + $this->users = new IntercomUsers($this); |
| 64 | + $this->events = new IntercomEvents($this); |
| 65 | + $this->companies = new IntercomCompanies($this); |
| 66 | + $this->messages = new IntercomMessages($this); |
| 67 | + $this->conversations = new IntercomConversations($this); |
| 68 | + $this->leads = new IntercomLeads($this); |
| 69 | + $this->admins = new IntercomAdmins($this); |
| 70 | + $this->tags = new IntercomTags($this); |
| 71 | + $this->segments = new IntercomSegments($this); |
| 72 | + $this->counts = new IntercomCounts($this); |
| 73 | + $this->bulk = new IntercomBulk($this); |
| 74 | + $this->notes = new IntercomNotes($this); |
| 75 | + $this->segments = new IntercomSegments($this); |
| 76 | + |
| 77 | + $this->usernamePart = $usernamePart; |
| 78 | + $this->passwordPart = $passwordPart; |
| 79 | + } |
| 80 | + |
| 81 | + private function setDefaultClient() |
| 82 | + { |
| 83 | + $this->http_client = new Client(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Sets GuzzleHttp client. |
| 88 | + * @param Client $client |
| 89 | + */ |
| 90 | + public function setClient($client) |
| 91 | + { |
| 92 | + $this->http_client = $client; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sends POST request to Intercom API. |
| 97 | + * @param string $endpoint |
| 98 | + * @param string $json |
| 99 | + * @return mixed |
| 100 | + * @throws \GuzzleHttp\Exception\GuzzleException |
| 101 | + */ |
| 102 | + public function post($endpoint, $json) |
| 103 | + { |
| 104 | + $response = $this->http_client->request('POST', "https://api.intercom.io/$endpoint", [ |
| 105 | + 'json' => $json, |
| 106 | + 'auth' => $this->getAuth(), |
| 107 | + 'headers' => [ |
| 108 | + 'Accept' => 'application/json' |
| 109 | + ] |
| 110 | + ]); |
| 111 | + return $this->handleResponse($response); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Sends DELETE request to Intercom API. |
| 116 | + * @param string $endpoint |
| 117 | + * @param string $json |
| 118 | + * @return mixed |
| 119 | + * @throws \GuzzleHttp\Exception\GuzzleException |
| 120 | + */ |
| 121 | + public function delete($endpoint, $json) |
| 122 | + { |
| 123 | + $response = $this->http_client->request('DELETE', "https://api.intercom.io/$endpoint", [ |
| 124 | + 'json' => $json, |
| 125 | + 'auth' => $this->getAuth(), |
| 126 | + 'headers' => [ |
| 127 | + 'Accept' => 'application/json' |
| 128 | + ] |
| 129 | + ]); |
| 130 | + return $this->handleResponse($response); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param string $endpoint |
| 135 | + * @param string $query |
| 136 | + * @return mixed |
| 137 | + * @throws \GuzzleHttp\Exception\GuzzleException |
| 138 | + */ |
| 139 | + public function get($endpoint, $query) |
| 140 | + { |
| 141 | + $response = $this->http_client->request('GET', "https://api.intercom.io/$endpoint", [ |
| 142 | + 'query' => $query, |
| 143 | + 'auth' => $this->getAuth(), |
| 144 | + 'headers' => [ |
| 145 | + 'Accept' => 'application/json' |
| 146 | + ] |
| 147 | + ]); |
| 148 | + return $this->handleResponse($response); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Returns next page of the result. |
| 153 | + * @param array $pages |
| 154 | + * @return mixed |
| 155 | + * @throws \GuzzleHttp\Exception\GuzzleException |
| 156 | + */ |
| 157 | + public function nextPage($pages) |
| 158 | + { |
| 159 | + $response = $this->http_client->request('GET', $pages['next'], [ |
| 160 | + 'auth' => $this->getAuth(), |
| 161 | + 'headers' => [ |
| 162 | + 'Accept' => 'application/json' |
| 163 | + ] |
| 164 | + ]); |
| 165 | + return $this->handleResponse($response); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Returns authentication parameters. |
| 170 | + * @return array |
| 171 | + */ |
| 172 | + public function getAuth() |
| 173 | + { |
| 174 | + return [$this->usernamePart, $this->passwordPart]; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @param Response $response |
| 179 | + * @return mixed |
| 180 | + */ |
| 181 | + private function handleResponse(Response $response) |
| 182 | + { |
| 183 | + $stream = stream_for($response->getBody()); |
| 184 | + $data = json_decode($stream->getContents()); |
| 185 | + return $data; |
| 186 | + } |
180 | 187 | } |
0 commit comments