-
Notifications
You must be signed in to change notification settings - Fork 0
/
wso2api.class.php
377 lines (353 loc) · 12.4 KB
/
wso2api.class.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* WSO2API class
*
* @author Luca Gioppo
* @created 28/11/2013
* @copyright 2013 Luca Gioppo
* @version 0.1
* @license https://github.com/gioppoluca/wso2api-php-lib/blob/master/LICENSE
* @package wso2api
*/
include_once ('easy.curl.class.php');
/**
* wso2api class
*
* This class manages all the interactions with the API Manager.
* At the moment is compatible with API manager 1.6.0 and is able to create and retrieve APIs
*/
class Wso2API {
private $curl;
private $curl_options;
private $api_server;
private $api_serverSwag;
private $api_user;
private $api_password;
private $isLoggedIn = false;
private $login_path = '/publisher/site/blocks/user/login/ajax/login.jag';
private $create_api_path = '/publisher/site/blocks/item-add/ajax/add.jag';
private $status_api_path = '/publisher/site/blocks/life-cycles/ajax/life-cycles.jag';
private $get_api_url = '/publisher/site/blocks/listing/ajax/item-list.jag';
private $get_all_api_url = '/publisher/site/blocks/listing/ajax/item-list.jag';
private $get_api_swagger_url1 = '/registry/resource/_system/governance/apimgt/applicationdata/api-docs/';
private $get_api_swagger_url2 = '/api-doc.json';
private $debug = false;
public $error_message = '';
public $error_code = 0;
public $response = null;
/**
* class constructor
*
* @param string $api_server
* @param string $user
* @param string $password
* @param string $debug
*/
function __construct($api_server, $user = 'admin', $password = 'admin', $debug = false) {
$headers [] = "Accept: */*";
$headers [] = "Connection: Keep-Alive";
$agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";
$cookie_file_path = "cookie.txt";
$this->curl_options = array (
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIEFILE => $cookie_file_path,
CURLOPT_COOKIEJAR => $cookie_file_path,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => $agent,
CURLOPT_HEADER => 0
);
$this->api_server = 'http://' . $api_server;
$this->api_serverSwag = 'http://' . $user . ':' . $password . '@' . $api_server;
$this->api_user = $user;
$this->api_password = $password;
$this->debug = $debug;
$this->curl = new cURL ( $api_server );
$this->curl->options ( $this->curl_options );
}
/**
* Function to login into the API Manager
*
* @param string $user
* @param string $password
* @return boolean
*/
public function login($user = '', $password = '') {
if ($this->debug)
error_log ( print_r ( 'user: ' . $user, TRUE ) );
if ($this->debug)
error_log ( print_r ( 'password: ' . $password, TRUE ) );
if (! empty ( $user ))
$this->api_user = $user;
if (! empty ( $password ))
$this->api_password = $password;
$login_url = $this->api_server . $this->login_path;
$login_post = array (
'action' => 'login',
'username' => $this->api_user,
'password' => $this->api_password
);
$login_ret = $this->curl->post ( $login_url, $login_post, $this->curl_options );
// two possible errors cURL and API manager
if ($this->curl->error_code) {
$this->error_message = 'Login: ' . $this->curl->error_string . ' - ' . $login_url . ' - ' . print_r ( $login_post, true );
$this->error_code = $this->curl->error_code;
} else {
// have to interpret the return code to understand if the API returned error
$response = json_decode ( $login_ret );
if ($this->debug)
error_log ( 'login response: ' . print_r ( $response, TRUE ) );
if (! empty ( $response )) {
if ($response->{'error'}) {
$this->error_message = $response->{'message'};
echo 'error in login';
return false;
}
$this->isLoggedIn = true;
}
}
$this->error_code = $this->curl->error_code;
return true;
}
/**
*
* @param array $params
* An associative array of initialization parameters
* @param array $resources
* An associative array of initialization parameters
* @param boolean $autopublish
* @return boolean
*/
public function create_api($params, $resources, $autopublish = false) {
// if not logged in log with the standard data
if (! $this->isLoggedIn) {
$login_result = $this->login ();
if (! $login_result) {
return $login_result;
}
}
if (empty ( $params ['name'] )) {
$this->error_message = 'You have to define API name';
return false;
}
if (empty ( $params ['version'] )) {
$this->error_message = 'You have to define API version';
return false;
}
if (! is_array ( $resources )) {
$this->error_message = 'You have to define API resources';
return false;
}
$create_api_url = $this->api_server . $this->create_api_path;
$create_api_post = array (
'action' => 'addAPI',
'name' => $params ['name'],
'visibility' => $params ['visibility'],
'version' => $params ['version'],
'description' => (empty ( $params ['description'] ) ? '' : $params ['description']),
'endpointType' => $params ['endpointType'],
'http_checked' => (empty ( $params ['http'] ) ? '' : 'http'),
'https_checked' => (empty ( $params ['https'] ) ? '' : 'https'),
'endpoint' => (empty ( $params ['endpoint'] ) ? '' : $params ['endpoint']),
'wsdl' => (empty ( $params ['wsdl'] ) ? '' : $params ['wsdl']),
'wadl' => (empty ( $params ['wadl'] ) ? '' : $params ['wadl']),
'tags' => (empty ( $params ['tags'] ) ? '' : $params ['tags']),
'tier' => $params ['tier'],
'bizOwner' => (empty ( $params ['bizOwner'] ) ? '' : $params ['bizOwner']),
'thumbUrl' => $params ['thumbUrl'],
'context' => $params ['context'],
'tiersCollection' => $params ['tiersCollection']
);
$create_api_post ['resourceCount'] = count ( $resources ) - 1;
foreach ( $resources as $i => $value ) {
$create_api_post ['resourceMethod-' . $i] = $value ['resourceMethod'];
$create_api_post ['resourceMethodAuthType-' . $i] = $value ['resourceMethodAuthType'];
$create_api_post ['resourceMethodThrottlingTier-' . $i] = $value ['resourceMethodThrottlingTier'];
$create_api_post ['uriTemplate-' . $i] = $value ['uriTemplate'];
}
$create_api_ret = $this->curl->post ( $create_api_url, $create_api_post, $this->curl_options );
// two possible errors cURL and API manager
if ($this->curl->error_code) {
$this->error_message = 'Create API: ' . $this->curl->error_string . ' - ' . $create_api_url . ' - ' . print_r ( $create_api_post, true );
} else {
// have to interpret the return code to understand if the API returned error
$response = json_decode ( $create_api_ret );
if ($response->{'error'}) {
$this->error_message = $response->{'message'};
return false;
}
}
// manage the autopublish option
if ($autopublish) {
$status_api_ret = $this->change_api_status ( 'PUBLISHED', $params ['provider'], $params ['name'], $params ['version'], true );
// maybe only return $status....
if (! $status_api_ret) {
return $status_api_ret;
}
}
return true;
}
public function delete_api() {
}
/**
* Function to change the lifecycle status of an API
*
* @param unknown $status
* @param string $provider
* @param string $name
* @param string $version
* @param boolean $publishtogataway
* @return boolean
*/
public function change_api_status($status, $provider, $name, $version, $publishtogataway) {
// if not logged in log with the standard data
if (! $this->isLoggedIn) {
$login_result = $this->login ();
if ($login_result > 0) {
return $login_result;
}
}
if (! is_bool ( $publishtogataway )) {
$this->error_message = 'publishtogateway has to be boolean';
return false;
}
$status_api_url = $this->api_server . $this->status_api_path;
$status_api_post = array (
'action' => 'updateStatus',
'name' => $name,
'version' => $version,
'provider' => $provider,
'status' => $status,
'publishToGateway' => $publishtogataway
);
$publish_api_ret = $this->curl->post ( $status_api_url, $status_api_post, $this->curl_options );
// two possible errors cURL and API manager
if ($this->curl->error_code) {
$this->error_message = 'Status change: ' . $this->curl->error_string;
} else {
// have to interpret the return code to understand if the API returned error
$response = json_decode ( $publish_api_ret );
if ($response->{'error'}) {
$this->error_message = $response->{'message'};
return false;
}
}
return true;
}
/**
* Function to get the info of a single API
*
* @param string $apiprovider
* @param string $apiname
* @param string $apiversion
* @return boolean
*/
public function get_api_info($apiprovider, $apiname, $apiversion) {
// if not logged in log with the standard data
if (! $this->isLoggedIn) {
$login_result = $this->login ();
if (! $login_result) {
return $login_result;
}
}
$get_api_url = $this->api_server . $this->get_api_url;
$call_api_post = array (
'action' => "getAPI",
'name' => $apiname,
'version' => $apiversion,
'provider' => $apiprovider
);
$published_api_ret = $this->curl->post ( $get_api_url, $call_api_post, $this->curl_options );
// two possible errors cURL and API manager
if ($this->curl->error_code) {
$this->error_message = 'Get API info: ' . $this->curl->error_string;
} else {
// have to interpret the return code to understand if the API returned error
$this->response = json_decode ( $published_api_ret );
if ($this->debug)
error_log ( 'Get all APi list response: : ' . print_r ( $this->response, TRUE ) );
if ($this->response->{'error'}) {
$this->error_message = $this->response->{'message'};
return false;
}
}
return true;
}
/**
* Function to return all the published API in the API Manager
*
* @return boolean
*/
public function get_all_api_list() {
// if not logged in log with the standard data
if (! $this->isLoggedIn) {
$login_result = $this->login ();
if (! $login_result) {
return $login_result;
}
}
$get_api_url = $this->api_server . $this->get_all_api_url . '?action=getAllAPIs';
if ($this->debug)
error_log ( 'get_api_url: ' . print_r ( $get_api_url, TRUE ) );
$published_api_list_ret = $this->curl->get ( $get_api_url, $this->curl_options );
// two possible errors cURL and API manager
if ($this->curl->error_code) {
$this->error_message = 'Get all API list: ' . $this->curl->error_string;
} else {
// have to interpret the return code to understand if the API returned error
$this->response = json_decode ( $published_api_list_ret );
if ($this->debug)
error_log ( 'Get all APi list response: : ' . print_r ( $this->response, TRUE ) );
if (! empty ( $this->response )) {
if (! empty ( $this->response->{'error'} )) {
$this->error_message = $this->response->{'message'};
return false;
}
}
}
return true;
}
/**
* Function to retrieve the Swagger json
*
* @param string $apiname
* @param string $apiversion
* @return boolean
*/
public function get_api_swagger($apiname, $apiversion) {
// if not logged in log with the standard data
if (! $this->isLoggedIn) {
$login_result = $this->login ();
if (! $login_result) {
return $login_result;
}
}
$get_swagger_url = $this->api_serverSwag . $this->get_api_swagger_url1 . $apiname . '-' . $apiversion . $this->get_api_swagger_url2;
$swagger_api_ret = $this->curl->get ( $get_swagger_url, $this->curl_options );
if ($this->debug)
error_log ( 'get_api_swagger_url: ' . print_r ( $get_swagger_url, TRUE ) );
if ($this->debug)
error_log ( 'get_api_swagger: ' . print_r ( $swagger_api_ret, TRUE ) );
// two possible errors cURL and API manager
if ($this->curl->error_code) {
$this->error_message = 'cURL ERROR get_api_swagger: ' . $this->curl->error_string;
if ($this->debug)
error_log ( 'cURl ERROR get_api_swagger: ' . print_r ( $this->curl->error_string, TRUE ) );
} else {
// have to interpret the return code to understand if the API returned error
$this->response = json_decode ( $swagger_api_ret );
if ($this->debug)
error_log ( 'Get all APi list response: : ' . print_r ( $this->response, TRUE ) );
if (! empty ( $this->response )) {
if (! empty ( $this->response->{'error'} )) {
$this->error_message = $this->response->{'message'};
return false;
}
}
}
return true;
}
}
?>