This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminRestClient.php
171 lines (128 loc) · 4.62 KB
/
AdminRestClient.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
<?php
/**
* Exception thrown out of AdminClient.
*/
class AdminRestClientException extends Exception {
public function __construct($message) {
parent::__construct($message);
}
}
/**
* Exception caused by internal non-REST related exception.
*/
class InternalAdminRestClientException extends AdminRestClientException {
public function __construct($message) {
parent::__construct($message);
}
}
/**
* Exception thrown out of AdminClient.
*/
class RestAdminRestClientException extends AdminRestClientException {
private $status_code;
private $body;
public function __construct($status_code, $body) {
$this->status_code = $status_code;
$this->body = $body;
parent::__construct('API returned status code ' . $this->status_code);
}
public function getStatusCode() {
return $this->status_code;
}
public function getBody() {
return $this->body;
}
}
/**
* HTTP REST client for LoginTC Admin.
*/
class AdminRestClient {
const CONTENT_TYPE = 'application/vnd.logintc.v1+json';
const DEFAULT_ACCEPT_HEADER = 'application/vnd.logintc.v1+json';
private $url;
private $api_key;
private $user_agent;
public function __construct($url, $api_key, $user_agent) {
$this->url = $url;
$this->api_key = $api_key;
$this->user_agent = $user_agent;
}
public function get($path) {
$get_url = $this->url . $path;
$ch = curl_init();
return $this->execute_curl($this->url . $path, $ch);
}
public function get_bytes($path, $content_type) {
$get_url = $this->url . $path;
$ch = curl_init();
return $this->execute_curl($this->url . $path, $ch, $content_type);
}
public function post($path, $body) {
$post_url = $this->url . $path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
if (!is_null($body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
return $this->execute_curl($post_url, $ch);
}
public function put($path, $body) {
$put_url = $this->url . $path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
if (!is_null($body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
return $this->execute_curl($put_url, $ch);
}
public function delete($path) {
$delete_url = $this->url . $path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
return $this->execute_curl($delete_url, $ch);
}
private function execute_curl($url, $ch, $accept_header = self::DEFAULT_ACCEPT_HEADER) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$http_headers = array();
array_push($http_headers, 'Accept: ' . $accept_header);
array_push($http_headers, 'User-Agent: ' . $this->user_agent);
array_push($http_headers, 'Authorization: LoginTC key="' . $this->api_key . '"');
if (!is_null(curl_getinfo($ch, CURLOPT_POSTFIELDS))) {
array_push($http_headers, 'Content-Type: ' . self::CONTENT_TYPE);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
// receive server response ...
$server_output = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// error happened with curl
if (empty($http_code)) {
$curl_error = curl_error($ch);
curl_close($ch);
throw new InternalAdminRestClientException($curl_error);
}
curl_close($ch);
switch ($http_code) {
case 200: // OK
case 201: // Created
case 202: // Accepted
break;
case 400: // Bad Request
case 401: // Unauthorized
case 403: // Forbidden
case 404: // Not Found
case 405: // Method Not Allowed
case 406: // Not Acceptable
case 410: // Gone
case 429: // Too Many Requests
case 500: // Internal Server Error
case 501: // Not Implemented
case 502: // Bad Gateway
case 503: // Service Unavailable
case 504: // Gateway Timeout
default:
throw new RestAdminRestClientException($http_code, $server_output);
}
return $server_output;
}
}