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
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* LiveEngageRequest class. |
16
|
|
|
*/ |
17
|
|
|
class LiveEngageRequest |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* config |
21
|
|
|
* |
22
|
|
|
* @var mixed |
23
|
|
|
* @access private |
24
|
|
|
*/ |
25
|
|
|
private $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* retry_limit |
29
|
|
|
* |
30
|
|
|
* (default value: 3) |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
* @access private |
34
|
|
|
*/ |
35
|
|
|
private $retry_limit = 3; |
36
|
|
|
/** |
37
|
|
|
* retry_counter |
38
|
|
|
* |
39
|
|
|
* (default value: 0) |
40
|
|
|
* |
41
|
|
|
* @var int |
42
|
|
|
* @access private |
43
|
|
|
*/ |
44
|
|
|
private $retry_counter = 0; |
45
|
|
|
/** |
46
|
|
|
* bearer |
47
|
|
|
* |
48
|
|
|
* @var mixed |
49
|
|
|
* @access private |
50
|
|
|
*/ |
51
|
|
|
private $bearer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* __construct function. |
55
|
|
|
* |
56
|
|
|
* @access public |
57
|
|
|
* @param mixed $config |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function __construct($config) |
61
|
|
|
{ |
62
|
|
|
$this->config = $config; // @codeCoverageIgnore |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* login |
67
|
|
|
* |
68
|
|
|
* @access public |
69
|
|
|
* @return this |
70
|
|
|
*/ |
71
|
1 |
|
public function login() |
72
|
|
|
{ |
73
|
|
|
/** @scrutinizer ignore-call */ |
74
|
1 |
|
$le = LiveEngage::domain('agentVep'); |
75
|
1 |
|
$domain = $le->domain; |
76
|
1 |
|
$account = $le->account; |
77
|
|
|
|
78
|
1 |
|
$consumer_key = config("{$this->config}.key"); |
79
|
1 |
|
$consumer_secret = config("{$this->config}.secret"); |
80
|
1 |
|
$token = config("{$this->config}.token"); |
81
|
1 |
|
$secret = config("{$this->config}.token_secret"); |
82
|
1 |
|
$username = config("{$this->config}.user_name"); |
83
|
|
|
|
84
|
|
|
$auth = [ |
85
|
1 |
|
'username' => $username, |
86
|
1 |
|
'appKey' => $consumer_key, |
87
|
1 |
|
'secret' => $consumer_secret, |
88
|
1 |
|
'accessToken' => $token, |
89
|
1 |
|
'accessTokenSecret' => $secret, |
90
|
|
|
]; |
91
|
|
|
|
92
|
1 |
|
$url = "https://{$domain}/api/account/{$account}/login?v=1.3"; |
93
|
|
|
|
94
|
1 |
|
$response = $this->V1($url, 'POST', $auth); |
95
|
|
|
|
96
|
1 |
|
$this->bearer = $response->bearer; |
97
|
|
|
|
98
|
1 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* V1 |
103
|
|
|
* |
104
|
|
|
* @access public |
105
|
|
|
* @param string $url |
106
|
|
|
* @param string $method |
107
|
|
|
* @param array $payload (default: []) |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
1 |
|
public function V1($url, $method, $payload = null) |
111
|
|
|
{ |
112
|
1 |
|
$client = $this->requestClient(); |
113
|
|
|
|
114
|
|
|
$args = [ |
115
|
1 |
|
'auth' => 'oauth', |
116
|
|
|
'headers' => [ |
117
|
|
|
'content-type' => 'application/json', |
118
|
|
|
], |
119
|
1 |
|
'body' => json_encode($payload ?: []) |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
// @codeCoverageIgnoreStart |
123
|
|
|
try { |
124
|
|
|
$res = $client->request($method, $url, $args); |
125
|
|
|
$response = json_decode($res->getBody()); |
126
|
|
|
} catch (\Exception $e) { |
127
|
|
|
if ($this->retry_counter < $this->retry_limit || $this->retry_limit == -1) { |
128
|
|
|
usleep(1500); |
129
|
|
|
$this->retry_counter++; |
130
|
|
|
$response = $this->V1($url, $payload ?: []); |
|
|
|
|
131
|
|
|
} else { |
132
|
|
|
throw $e; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
// @codeCoverageIgnoreEnd |
136
|
|
|
|
137
|
1 |
|
return $response; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* V2 |
142
|
|
|
* |
143
|
|
|
* @access public |
144
|
|
|
* @param mixed $url |
145
|
|
|
* @param mixed $method |
146
|
|
|
* @param mixed $payload (default: []) |
147
|
|
|
* @param mixed $headers (default: []) |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
1 |
|
public function V2($url, $method, $payload = null, $headers = null) |
151
|
|
|
{ |
152
|
1 |
|
$this->login(); |
153
|
|
|
|
154
|
1 |
|
$client = new Client(); |
155
|
|
|
$args = [ |
156
|
1 |
|
'headers' => array_merge([ |
157
|
1 |
|
'content-type' => 'application/json', |
158
|
1 |
|
'Authorization' => 'Bearer ' . $this->bearer |
159
|
1 |
|
], $headers ?: []), |
160
|
1 |
|
'body' => json_encode($payload ?: []) |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
// @codeCoverageIgnoreStart |
164
|
|
|
try { |
165
|
|
|
$res = $client->request($method, $url, $args); |
166
|
|
|
} catch (\Exception $e) { |
167
|
|
|
throw $e; |
168
|
|
|
} |
169
|
|
|
// @codeCoverageIgnoreEnd |
170
|
|
|
|
171
|
1 |
|
return json_decode($res->getBody()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* requestClient |
176
|
|
|
* |
177
|
|
|
* @access private |
178
|
|
|
* @return \GuzzleHttp\Client |
179
|
|
|
*/ |
180
|
1 |
|
private function requestClient() |
181
|
|
|
{ |
182
|
1 |
|
$consumer_key = config("{$this->config}.key"); |
183
|
1 |
|
$consumer_secret = config("{$this->config}.secret"); |
184
|
1 |
|
$token = config("{$this->config}.token"); |
185
|
1 |
|
$secret = config("{$this->config}.token_secret"); |
186
|
|
|
|
187
|
1 |
|
$stack = HandlerStack::create(); |
188
|
1 |
|
$auth = new Oauth1([ |
189
|
1 |
|
'consumer_key' => $consumer_key, |
190
|
1 |
|
'consumer_secret' => $consumer_secret, |
191
|
1 |
|
'token' => $token, |
192
|
1 |
|
'token_secret' => $secret, |
193
|
1 |
|
'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC, |
194
|
|
|
]); |
195
|
1 |
|
$stack->push($auth); |
196
|
|
|
|
197
|
1 |
|
$client = new Client([ |
198
|
1 |
|
'handler' => $stack, |
199
|
|
|
]); |
200
|
|
|
|
201
|
1 |
|
return $client; |
202
|
|
|
} |
203
|
|
|
} |