-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApi.php
211 lines (184 loc) · 5.44 KB
/
Api.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
<?php
namespace formcorp\sdk;
use GuzzleHttp\Client;
/**
* @constant string The url to access the API
*/
defined(__NAMESPACE__ . '\API_URL') or define(__NAMESPACE__ . '\API_URL', 'https://api.formcorp.com.au');
//defined(__NAMESPACE__ . '\API_URL') or define(__NAMESPACE__ . '\API_URL', 'http://192.168.247.129:9001/');
/**
* Class FCHelper
* @package fishvision\formcorpsdk
* @author Alex Berriman <[email protected]>
*/
class Api
{
/**
* @var string The public key for the application
*/
private $publicKey;
/**
* @var string The application secret
*/
private $secret;
/**
* Initialise the object instance.
* @param $publicKey
* @param $secret
*/
public function __construct($publicKey, $secret)
{
$this->publicKey = $publicKey;
$this->secret = $secret;
// Instantiate the guzzle client
$this->client = new Client([
'base_url' => API_URL,
]);
// Set the identifying token
$this->client->setDefaultOption('headers/Authorization', sprintf('%s %s', Constants::TOKEN_TYPE, $publicKey));
}
/**
* Send off an API call.
* @param $uri
* @param string $requestMethod
* @param array $data
* @return null|void
*/
public function call($uri, $requestMethod = Constants::METHOD_GET, $data = [])
{
// Prepend with a slash
if (substr($uri, 0, 1) !== '/') {
$uri = '/' . $uri;
}
// Send the request
switch (strtoupper($requestMethod)) {
// Send a GET HTTP request
case Constants::METHOD_GET:
$this->result = $this->get($uri, $data);
break;
// Send a POST HTTP request
case Constants::METHOD_POST:
$this->result = $this->post($uri, $data);
break;
}
// Return a json result
try {
return $this->result->json();
} catch (\Exception $e) {
return [
'error' => true,
'type' => 'exception',
'message' => 'Unable to decode JSON data',
];
}
}
/**
* @return array|bool
*/
public function error()
{
if (!isset($this->result)) {
return [
'error' => true,
'type' => 'undefined',
'message' => 'No result defined'
];
}
// Try to decode the json result
try {
$result = $this->result->json();
if (isset($result['error'])) {
return $result;
}
} catch (\Exception $e) {
return [
'error' => true,
'type' => 'exception',
'message' => 'Unable to decode JSON data',
];
}
return false;
}
/**
* @param $uri
* @param $data
* @return \GuzzleHttp\Message\FutureResponse|\GuzzleHttp\Message\ResponseInterface|\GuzzleHttp\Ring\Future\FutureInterface|mixed|null
*/
public function get($uri, $data)
{
return $this->client->get($uri, [
'headers' => [
'Signature' => $this->calculateSignature(Constants::METHOD_GET, $uri),
],
]);
}
/**
* @param $uri
* @param array $data
* @return \GuzzleHttp\Message\FutureResponse|\GuzzleHttp\Message\ResponseInterface|\GuzzleHttp\Ring\Future\FutureInterface|mixed|null
*/
public function post($uri, $data = [])
{
// PHP is loosely typed. Attempt to convert non string elements to string
foreach ($data as &$val) {
if (!is_string($val)) {
$val = strval($val);
}
}
return $this->client->post($uri, [
'body' => $data,
'headers' => [
'Signature' => $this->calculateSignature(Constants::METHOD_POST, $uri, $data),
]
]);
}
/**
* Generate an access token to the API
* @return string|bool
*/
public function generateToken()
{
$request = $this->call('v1/auth/token', Constants::METHOD_POST, [
'timestamp' => time(),
'nonce' => $this->generateNonce(),
]);
return isset($request['token']) ? $request['token'] : false;
}
/**
* Calculates the signature to send through with the API request.
* @param $requestMethod
* @param $uri
* @param array $data
* @return string
*/
private function calculateSignature($requestMethod, $uri, $data = [])
{
// PHP is loosely typed. Attempt to convert non string elements to string
foreach ($data as &$val) {
if (!is_string($val)) {
$val = strval($val);
}
}
// Calculate the encoded hash using the request
$plaintext = json_encode([
'method' => $requestMethod,
'uri' => $uri,
'data' => $data,
]);
$plaintext = mb_convert_encoding($plaintext, Constants::SIGNATURE_ENCODING);
$hash = base64_encode(hash_hmac('sha1', $plaintext, $this->secret));
return $hash;
}
/**
* @param int $bytes
* @return string
*/
private function generateNonce($bytes = 32)
{
$nonce = '';
for ($i = 0; $i < $bytes; ++$i) {
$nonce .= chr(mt_rand(0, 255));
}
return base64_encode($nonce);
}
}