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