-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApiClient.php
More file actions
99 lines (83 loc) · 3.52 KB
/
ApiClient.php
File metadata and controls
99 lines (83 loc) · 3.52 KB
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
<?php
namespace Shirtigo\ApiClient;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
class ApiClient {
/** @var Client */
protected $client;
public function __construct(string $api_key, string $base_url="https://cockpit.shirtigo.com/api/")
{
// Validate base URL
if (substr($base_url, -1) !== '/' ) {
throw new \InvalidArgumentException('Invalid API base URL. The URL has to end with a backslash.');
}
// initialize GuzzleHttp client
$this->client = new Client([
'verify' => false,
'base_uri' => $base_url,
'headers' => [
'User-Agent' => 'Shirtigo Cockpit Guzzle-PHP REST API Client',
'Authorization' => 'Bearer ' . $api_key,
]
]);
}
protected function request(string $url, string $method="GET", array $data=[], array $params=[], array $files=[]) : ?\stdClass
{
try {
$options = [
'json' => $data,
'query' => $params
];
if (count($files) > 0) {
unset($options['json']);
$options['multipart'] = [];
foreach ($files as $key => $value) {
$options['multipart'][] = [
'name' => $key,
'contents' => $value,
];
}
}
$response = $this->client->request($method, $url, $options);
} catch (BadResponseException $e) {
// convert exception into PHP RuntimeException with the correct error message
$status = $e->getResponse()->getStatusCode();
$body = $e->getResponse()->getBody();
if ($status == 401) {
throw new \RuntimeException('Authentication error. Check whether the provided API key is valid.');
} else if ($status == 405) {
throw new \RuntimeException("{$method} is not valid a valid HTTP verb for the '{$url}' endpoint.");
} else if ($status == 422) {
$json = json_decode($body, true);
$message = isset($json['errors']) ? json_encode($json['errors']) : $json['message'];
throw new \RuntimeException("Input validation failed: {$message}");
} else {
$json = json_decode($body, true);
if (!is_null($json) && isset($json['message'])) {
$message = $json['message'];
throw new \RuntimeException("Endpoint returned HTTP status {$status}: {$message}");
} else {
throw new \RuntimeException("Endpoint returned unhandled HTTP status {$status}");
}
}
}
$response_data = json_decode($response->getBody(),false);
return $response_data;
}
public function get(string $url, array $params=[]) : ?\stdClass
{
return $this->request($url, 'GET', [], $params);
}
public function post(string $url, array $data=[], array $params=[], array $files=[]) : ?\stdClass
{
return $this->request($url, 'POST', $data, $params, $files);
}
public function put(string $url, array $data=[], array $params=[], array $files=[]) : ?\stdClass
{
return $this->request($url, 'PUT', $data, $params, $files);
}
public function delete(string $url, array $params=[]) : ?\stdClass
{
return $this->request($url, 'DELETE', [], $params);
}
}