Newer
Older
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
<?php
namespace Ingress\Services\smartdataapi;
use Exception;
/**
* API client for SmartDataContext API
*/
class SmartDataContextApiClient
{
private string $baseUrl;
private string $certificatePath;
private string $certificateKeyPath;
public function __construct(string $baseUrl, string $certificatePath, string $certificateKeyPath)
{
$this->baseUrl = $baseUrl;
$this->certificatePath = $certificatePath;
$this->certificateKeyPath = $certificateKeyPath;
}
private function postRequest(string $endpoint, array $data): array
{
$url = $this->baseUrl . $endpoint;
$ch = curl_init($url);
$jsonData = json_encode($data);
$headers = [
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData),
];
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSLCERT, $this->certificatePath);
curl_setopt($ch, CURLOPT_SSLKEY, $this->certificateKeyPath);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
return ['errors' => [$error]];
}
return json_decode($response, true);
}
public function createSmartDataContext(array $request): array
{
$data = [
'command' => '/create',
'request' => $request,
];
return $this->postRequest('/api/context.php#create', $data);
}
public function associateSmartDataContext(array $request): array
{
$data = [
'command' => '/associate',
'request' => $request,
];
return $this->postRequest('/api/context.php#associate', $data);
}
public function unassociateSmartDataContext(array $request): array
{
$data = [
'command' => '/unassociate',
'request' => $request,
];
return $this->postRequest('/api/context.php#unassociate', $data);
}
public function getSmartDataContext(string $id): array
{
$data = [
'command' => '/get',
'request' => ['smartDataContextId' => $id]
];
return $this->postRequest('/api/context.php#get', $data);
}
public function getIdByIdentifier(string $signature): mixed
{
$data = [
'command' => '/query',
'request' => ['features.identifier' => $signature]
];
$result = $this->postRequest('/api/context.php#query', $data);
if (array_key_exists('result', $result) && count($result['result']) > 0) {
return $result['result'][0]['id'];
} else if (array_key_exists('errors', $result)) {
throw new Exception(json_encode($result['errors']));
} else {
return null;
}
}
public function updateSmartDataContext(string $id, array $request): array
{
$request['smartDataContextId'] = $id;
$data = [
'command' => '/update',
'request' => $request,
];
return $this->postRequest('/api/context.php#update', $data);
}
public function querySmartDataContext(array $request): array
{
$data = [
'command' => '/query',
'request' => $request,
];
return $this->postRequest('/api/context.php#query', $data);
}
public function retrieveContext(array $request): array
{
$data = [
'command' => '/contexts',
'request' => $request,
];
return $this->postRequest('/api/context.php#context', $data);
}
}