-
Notifications
You must be signed in to change notification settings - Fork 2
/
Client.php
executable file
·268 lines (235 loc) · 8.28 KB
/
Client.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/
namespace Nexmo;
use Http\Client\HttpClient;
use Nexmo\Client\Credentials\Basic;
use Nexmo\Client\Credentials\CredentialsInterface;
use Nexmo\Client\Credentials\OAuth;
use Nexmo\Client\Credentials\SharedSecret;
use Nexmo\Client\Factory\FactoryInterface;
use Nexmo\Client\Factory\MapFactory;
use Nexmo\Client\Response\Response;
use Nexmo\Client\Signature;
use Nexmo\Entity\EntityInterface;
use Nexmo\Verify\Verification;
use Psr\Http\Message\RequestInterface;
use Zend\Diactoros\Uri;
/**
* Nexmo API Client, allows access to the API from PHP.
*
* @property \Nexmo\Message\Client $message
* @method \Nexmo\Message\Client message()
* @method \Nexmo\Verify\Client verify()
*/
class Client
{
const VERSION = '1.0.0-beta1';
const BASE_API = 'https://api.nexmo.com';
const BASE_REST = 'https://rest.nexmo.com';
/**
* API Credentials
* @var CredentialsInterface
*/
protected $credentials;
/**
* Http Client
* @var HttpClient
*/
protected $client;
/**
* @var FactoryInterface
*/
protected $factory;
/**
* @var array
*/
protected $options = [];
/**
* Create a new API client using the provided credentials.
*/
public function __construct(CredentialsInterface $credentials, $options = array(), HttpClient $client = null)
{
if(is_null($client)){
$client = new \Http\Adapter\Guzzle6\Client();
}
$this->setHttpClient($client);
//make sure we know how to use the credentials
if(!($credentials instanceof Basic) && !($credentials instanceof SharedSecret) && !($credentials instanceof OAuth)){
throw new \RuntimeException('unknown credentials type: ' . get_class($credentials));
}
$this->credentials = $credentials;
$this->options = $options;
$this->setFactory(new MapFactory([
'message' => 'Nexmo\Message\Client',
'verify' => 'Nexmo\Verify\Client'
], $this));
}
/**
* Set the Http Client to used to make API requests.
*
* This allows the default http client to be swapped out for a HTTPlug compatible
* replacement.
*
* @param HttpClient $client
* @return $this
*/
public function setHttpClient(HttpClient $client)
{
$this->client = $client;
return $this;
}
/**
* Get the Http Client used to make API requests.
*
* @return HttpClient
*/
public function getHttpClient()
{
return $this->client;
}
/**
* Set the factory used to create API specific clients.
*
* @param FactoryInterface $factory
* @return $this
*/
public function setFactory(FactoryInterface $factory)
{
$this->factory = $factory;
return $this;
}
/**
* @param RequestInterface $request
* @param Signature $signature
* @return RequestInterface
*/
public static function signRequest(RequestInterface $request, SharedSecret $credentials)
{
switch($request->getHeaderLine('content-type')){
case 'application/json':
$body = $request->getBody();
$body->rewind();
$content = $body->getContents();
$params = json_decode($content, true);
$params['api_key'] = $credentials['api_key'];
$signature = new Signature($params, $credentials['shared_secret']);
$body->rewind();
$body->write(json_encode($signature->getSignedParams()));
break;
case 'application/x-www-form-urlencoded':
$body = $request->getBody();
$body->rewind();
$content = $body->getContents();
$params = [];
parse_str($content, $params);
$params['api_key'] = $credentials['api_key'];
$signature = new Signature($params, $credentials['shared_secret']);
$params = $signature->getSignedParams();
$body->rewind();
$body->write(http_build_query($params, null, '&'));
break;
default:
$query = [];
parse_str($request->getUri()->getQuery(), $query);
$query['api_key'] = $credentials['api_key'];
$signature = new Signature($query, $credentials['shared_secret']);
$request = $request->withUri($request->getUri()->withQuery(http_build_query($signature->getSignedParams())));
break;
}
return $request;
}
public static function authRequest(RequestInterface $request, Basic $credentials)
{
switch($request->getHeaderLine('content-type')){
case 'application/json':
$body = $request->getBody();
$body->rewind();
$content = $body->getContents();
$params = json_decode($content, true);
$params = array_merge($params, $credentials->asArray());
$body->rewind();
$body->write(json_encode($params));
break;
case 'application/x-www-form-urlencoded':
$body = $request->getBody();
$body->rewind();
$content = $body->getContents();
$params = [];
parse_str($content, $params);
$params = array_merge($params, $credentials->asArray());
$body->rewind();
$body->write(http_build_query($params, null, '&'));
break;
default:
$query = [];
parse_str($request->getUri()->getQuery(), $query);
$query = array_merge($query, $credentials->asArray());
$request = $request->withUri($request->getUri()->withQuery(http_build_query($query)));
break;
}
return $request;
}
/**
* Wraps the HTTP Client, creates a new PSR-7 request adding authentication, signatures, etc.
*
* @param \Psr\Http\Message\RequestInterface $request
* @return \Psr\Http\Message\ResponseInterface
*/
public function send(\Psr\Http\Message\RequestInterface $request)
{
//also an instance of Basic, so checked first
if($this->credentials instanceof SharedSecret){
$request = self::signRequest($request, $this->credentials);
} elseif($this->credentials instanceof Basic){
$request = self::authRequest($request, $this->credentials);
}
//todo: add oauth support
//allow any part of the URI to be replaced with a simple search
if(isset($this->options['url'])){
foreach($this->options['url'] as $search => $replace){
$uri = (string) $request->getUri();
$new = str_replace($search, $replace, $uri);
if($uri !== $new){
$request = $request->withUri(new Uri($new));
}
}
}
//set the user-agent
$request = $request->withHeader('user-agent', implode('/', [
'nexmo-php',
self::VERSION,
'PHP-' . implode('.', [PHP_MAJOR_VERSION, PHP_MINOR_VERSION])
]));
$response = $this->client->sendRequest($request);
return $response;
}
public function serialize(EntityInterface $entity)
{
if($entity instanceof Verification){
return $this->verify()->serialize($entity);
}
throw new \RuntimeException('unknown class `' . get_class($entity) . '``');
}
public function unserialize($entity)
{
if(is_string($entity)){
$entity = unserialize($entity);
}
if($entity instanceof Verification){
return $this->verify()->unserialize($entity);
}
throw new \RuntimeException('unknown class `' . get_class($entity) . '``');
}
public function __call($name, $args)
{
if(!$this->factory->hasApi($name)){
throw new \RuntimeException('no api namespace found: ' . $name);
}
return $this->factory->getApi($name);
}
}