1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LiveEngageRequest class. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace LivePersonInc\LiveEngageLaravel; |
7
|
|
|
|
8
|
|
|
use LivePersonInc\LiveEngageLaravel\Facades\LiveEngageLaravel as LiveEngage; |
9
|
|
|
use GuzzleHttp\Client; |
10
|
|
|
use GuzzleHttp\HandlerStack; |
11
|
|
|
use GuzzleHttp\Subscriber\Oauth\Oauth1; |
12
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\Payload; |
13
|
|
|
use LivePersonInc\LiveEngageLaravel\Exceptions\LiveEngageException; |
14
|
|
|
use LivePersonInc\LiveEngageLaravel\Exceptions\LoginFailure; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* LiveEngageRequest class. |
18
|
|
|
*/ |
19
|
|
|
class LiveEngageRequest |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* config |
23
|
|
|
* |
24
|
|
|
* @var mixed |
25
|
|
|
* @access private |
26
|
|
|
*/ |
27
|
|
|
private $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* retry_limit |
31
|
|
|
* |
32
|
|
|
* (default value: 3) |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
* @access private |
36
|
|
|
*/ |
37
|
|
|
private $retry_limit = 3; |
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* retry_counter |
40
|
|
|
* |
41
|
|
|
* (default value: 0) |
42
|
|
|
* |
43
|
|
|
* @var int |
44
|
|
|
* @access private |
45
|
|
|
*/ |
46
|
|
|
private $retry_counter = 0; |
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* bearer |
49
|
|
|
* |
50
|
|
|
* @var mixed |
51
|
|
|
* @access private |
52
|
|
|
*/ |
53
|
|
|
public $bearer; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* __construct function. |
57
|
|
|
* |
58
|
|
|
* @access public |
59
|
|
|
* @param mixed $config |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function __construct($config) |
63
|
|
|
{ |
64
|
|
|
$this->config = $config; // @codeCoverageIgnore |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* login function. |
69
|
|
|
* |
70
|
|
|
* @access public |
71
|
|
|
* @param string $user (default: null) |
72
|
|
|
* @param string $pass (default: null) |
73
|
|
|
* @return object |
74
|
|
|
*/ |
75
|
|
|
public function login($user = false, $pass = false) |
76
|
|
|
{ |
77
|
|
|
/** @scrutinizer ignore-call */ |
78
|
|
|
$le = LiveEngage::domain('agentVep'); |
79
|
|
|
$domain = $le->domain; |
80
|
|
|
$account = $le->account; |
81
|
|
|
|
82
|
|
|
$consumer_key = config("{$this->config}.key"); |
83
|
|
|
$consumer_secret = config("{$this->config}.secret"); |
84
|
|
|
$token = config("{$this->config}.token"); |
85
|
|
|
$secret = config("{$this->config}.token_secret"); |
86
|
|
|
$username = config("{$this->config}.user_name"); |
87
|
|
|
|
88
|
|
|
if ($user && $pass) { |
89
|
|
|
$auth = [ |
90
|
|
|
'username' => $user, |
91
|
|
|
'password' => $pass |
92
|
|
|
]; |
93
|
|
|
} else { |
94
|
|
|
$auth = [ |
95
|
|
|
'username' => $username, |
96
|
|
|
'appKey' => $consumer_key, |
97
|
|
|
'secret' => $consumer_secret, |
98
|
|
|
'accessToken' => $token, |
99
|
|
|
'accessTokenSecret' => $secret, |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$url = "https://{$domain}/api/account/{$account}/login?v=1.3"; |
104
|
|
|
|
105
|
|
|
try { |
106
|
|
|
$response = $this->get('V1', $url, 'POST', $auth, [], true); |
107
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
108
|
|
|
throw $e; //new LoginFailure(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->bearer = $response->body->bearer; |
112
|
|
|
|
113
|
|
|
$session = ['lptoken' => $this->bearer, 'lpcsrf' => $response->body->csrf]; |
114
|
|
|
|
115
|
|
|
session($session); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* V1 |
122
|
|
|
* |
123
|
|
|
* @access public |
124
|
|
|
* @param string $url |
125
|
|
|
* @param string $method |
126
|
|
|
* @param array $payload (default: []) |
127
|
|
|
* @param bool $noauth (default: false) |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
public function V1($url, $method, $payload = null, $headers = null, $noauth = false) |
131
|
|
|
{ |
132
|
|
|
$client = $this->requestClient($noauth); |
133
|
|
|
|
134
|
|
|
$args = [ |
135
|
|
|
'auth' => 'oauth', |
136
|
|
|
'headers' => array_merge([ |
137
|
|
|
'content-type' => 'application/json', |
138
|
|
|
'accept' => 'application/json' |
139
|
|
|
], $headers ?: []), |
140
|
|
|
'body' => json_encode($payload ?: []) |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
if ($noauth) unset($args['auth']); |
144
|
|
|
|
145
|
|
|
// @codeCoverageIgnoreStart |
146
|
|
|
try { |
147
|
|
|
$res = $client->request($method, $url, $args); |
148
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
149
|
|
|
throw $e; |
150
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
151
|
|
|
$code = $e->getResponse()->getStatusCode(); |
152
|
|
|
if ($code == 401) { |
153
|
|
|
throw new LoginFailure(); |
154
|
|
|
} else { |
155
|
|
|
throw $e; |
156
|
|
|
} |
157
|
|
|
} catch (\Exception $e) { |
158
|
|
|
throw $e; |
159
|
|
|
} |
160
|
|
|
// @codeCoverageIgnoreEnd |
161
|
|
|
|
162
|
|
|
return $res; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* V2 |
167
|
|
|
* |
168
|
|
|
* @access public |
169
|
|
|
* @param mixed $url |
170
|
|
|
* @param mixed $method |
171
|
|
|
* @param mixed $payload (default: []) |
172
|
|
|
* @param mixed $headers (default: []) |
173
|
|
|
* @return mixed |
174
|
|
|
*/ |
175
|
|
|
public function V2($url, $method, $payload = null, $headers = null, $noauth = false) |
176
|
|
|
{ |
177
|
|
|
if (!($token = session('lptoken', $this->bearer))) $this->login(); |
178
|
|
|
|
179
|
|
|
$client = new Client(); |
180
|
|
|
$args = [ |
181
|
|
|
'headers' => array_merge([ |
182
|
|
|
'content-type' => 'application/json', |
183
|
|
|
'accept' => 'application/json', |
184
|
|
|
'Authorization' => 'Bearer ' . $token |
|
|
|
|
185
|
|
|
], $headers ?: []), |
186
|
|
|
'body' => json_encode($payload ?: []) |
187
|
|
|
]; |
188
|
|
|
|
189
|
|
|
// @codeCoverageIgnoreStart |
190
|
|
|
try { |
191
|
|
|
$res = $client->request($method, $url, $args); |
192
|
|
|
} catch (\Exception $e) { |
193
|
|
|
throw $e; |
194
|
|
|
} |
195
|
|
|
// @codeCoverageIgnoreEnd |
196
|
|
|
|
197
|
|
|
return $res; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function get($version, $url, $method, $payload = null, $headers = null, $noauth = false) |
201
|
|
|
{ |
202
|
|
|
$response = $this->$version($url, $method, $payload, $headers, $noauth); |
203
|
|
|
|
204
|
|
|
$content = new \StdClass(); |
205
|
|
|
$content->body = json_decode($response->getBody()); |
206
|
|
|
$content->headers = $response->getHeaders(); |
207
|
|
|
|
208
|
|
|
return $content; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* requestClient |
213
|
|
|
* |
214
|
|
|
* @access private |
215
|
|
|
* @return \GuzzleHttp\Client |
216
|
|
|
* @param bool $noauth (default: false) |
217
|
|
|
*/ |
218
|
|
|
private function requestClient($noauth = false) |
219
|
|
|
{ |
220
|
|
|
if ($noauth) { |
221
|
|
|
return new Client([ |
222
|
|
|
'cookies' => true, |
223
|
|
|
]); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$consumer_key = config("{$this->config}.key"); |
227
|
|
|
$consumer_secret = config("{$this->config}.secret"); |
228
|
|
|
$token = config("{$this->config}.token"); |
229
|
|
|
$secret = config("{$this->config}.token_secret"); |
230
|
|
|
|
231
|
|
|
$stack = HandlerStack::create(); |
232
|
|
|
$auth = new Oauth1([ |
233
|
|
|
'consumer_key' => $consumer_key, |
234
|
|
|
'consumer_secret' => $consumer_secret, |
235
|
|
|
'token' => $token, |
236
|
|
|
'token_secret' => $secret, |
237
|
|
|
'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC, |
238
|
|
|
]); |
239
|
|
|
$stack->push($auth); |
240
|
|
|
|
241
|
|
|
$client = new Client([ |
242
|
|
|
'handler' => $stack, |
243
|
|
|
'cookies' => true |
244
|
|
|
]); |
245
|
|
|
|
246
|
|
|
return $client; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function refresh() |
250
|
|
|
{ |
251
|
|
|
$le = LiveEngage::domain('agentVep'); |
|
|
|
|
252
|
|
|
$domain = $le->domain; |
253
|
|
|
$account = $le->account; |
254
|
|
|
$csrf = session('lpcsrf'); |
255
|
|
|
$client = $this->requestClient(true); |
256
|
|
|
$client->post("https://$domain/api/account/$account/refresh", [ |
257
|
|
|
'headers' => [ |
258
|
|
|
'Content-type' => 'application/json', |
259
|
|
|
'Accept' => 'application/json', |
260
|
|
|
], |
261
|
|
|
'body' => json_encode([ |
262
|
|
|
'csrf' => $csrf |
263
|
|
|
]) |
264
|
|
|
]); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|