-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_utils.php
65 lines (57 loc) · 2.03 KB
/
api_utils.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
<?php
use OpenAPI\Client\Model\User;
include dirname( __FILE__ ) .'/helper.php';
/**
* @param User[] $users
* @throws Exception
*/
function validate_api_func($users)
{
$found = false;
foreach ($users as $user) {
if ($user->getUsername() === 'akadmin') {
$found = true;
break;
}
}
if ($found == false)
{
throw new Exception("Does akadmin exist?");
}}
function init_api_func()
{
if(get_option("api_token")== "")
{
wp_die( __( "You do not have provided a valid API token." ) );
}
// Configure API key authorization: authentik
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', get_option("api_token"));
// setup prefix (e.g. Bearer) for API key, if needed
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Authorization', 'Bearer');
$apiInstance = new OpenAPI\Client\Api\CoreApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$config
);
try {
// random api point for testing
$result = $apiInstance->coreUsersList()->getResults();
validate_api_func($result);
} catch (Exception $e) {
wp_die('Exception when trying to call the API.' . $e->getMessage());
}
}
function get_API_instance_func()
{
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', get_option("api_token"));
// setup prefix (e.g. Bearer) for API key, if needed
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Authorization', 'Bearer');
$apiInstance = new OpenAPI\Client\Api\CoreApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$config
);
return $apiInstance;
}