-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-api-client.php
More file actions
237 lines (192 loc) · 7.67 KB
/
Copy pathclass-api-client.php
File metadata and controls
237 lines (192 loc) · 7.67 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
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
<?php
defined( 'ABSPATH' ) || exit;
class Filecheck_API_Client {
protected static $_instance = null;
protected $api_url = 'https://api.filecheck.io';
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct() {
$override = get_option( 'filecheck_api_url' );
if ( ! empty( $override ) ) {
$this->api_url = esc_url_raw( $override );
}
}
public function get_api_url() {
return $this->api_url;
}
protected function get_headers( $secret_key ) {
return array(
'Authorization' => 'Bearer ' . $secret_key,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
);
}
public function verify_keys( $publishable_key, $secret_key ) {
if ( empty( $secret_key ) ) {
return new WP_Error( 'missing_key', __( 'Secret key is required.', 'filecheck' ) );
}
$url = $this->api_url . '/workflows/';
$response = wp_remote_get( $url, array(
'headers' => $this->get_headers( $secret_key ),
'timeout' => 15,
) );
if ( is_wp_error( $response ) ) {
return $response;
}
$code = wp_remote_retrieve_response_code( $response );
if ( $code === 200 ) {
return true;
}
if ( $code === 401 || $code === 403 ) {
return new WP_Error( 'auth_failed', __( 'Authentication failed. Please check your keys.', 'filecheck' ) );
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
// translators: %d: HTTP status code returned by the Filecheck API.
$message = isset( $body['message'] ) ? $body['message'] : sprintf( __( 'API returned HTTP code %d', 'filecheck' ), $code );
return new WP_Error( 'auth_failed', $message );
}
public function get_workflows( $secret_key = '' ) {
if ( empty( $secret_key ) ) {
$secret_key = get_option( 'filecheck_secret_key' );
}
if ( empty( $secret_key ) ) {
return array();
}
$url = $this->api_url . '/workflows/';
$response = wp_remote_get( $url, array(
'headers' => $this->get_headers( $secret_key ),
'timeout' => 15,
) );
if ( is_wp_error( $response ) ) {
return array();
}
$code = wp_remote_retrieve_response_code( $response );
if ( $code !== 200 ) {
return array();
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( isset( $body['workflows'] ) && is_array( $body['workflows'] ) ) {
return $body['workflows'];
}
if ( isset( $body['rules'] ) && is_array( $body['rules'] ) ) {
return $body['rules'];
}
return is_array( $body ) ? $body : array();
}
public function get_connectors( $secret_key = '' ) {
if ( empty( $secret_key ) ) {
$secret_key = get_option( 'filecheck_secret_key' );
}
if ( empty( $secret_key ) ) {
return array();
}
$url = $this->api_url . '/connectors/';
$response = wp_remote_get( $url, array(
'headers' => $this->get_headers( $secret_key ),
'timeout' => 15,
) );
if ( is_wp_error( $response ) ) {
return array();
}
$code = wp_remote_retrieve_response_code( $response );
if ( $code !== 200 ) {
return array();
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( isset( $body['connectors'] ) && is_array( $body['connectors'] ) ) {
return $body['connectors'];
}
return is_array( $body ) ? $body : array();
}
public function sync_order( $order_id, $payload, $secret_key = '' ) {
if ( empty( $secret_key ) ) {
$secret_key = get_option( 'filecheck_secret_key' );
}
if ( empty( $secret_key ) ) {
return new WP_Error( 'missing_key', __( 'Secret key is missing.', 'filecheck' ) );
}
$url = $this->api_url . '/orders/' . urlencode( $order_id );
$response = wp_remote_post( $url, array(
'headers' => $this->get_headers( $secret_key ),
'body' => wp_json_encode( $payload ),
'timeout' => 15,
) );
if ( is_wp_error( $response ) ) {
return $response;
}
$code = wp_remote_retrieve_response_code( $response );
if ( $code < 200 || $code >= 300 ) {
$body = json_decode( wp_remote_retrieve_body( $response ), true );
// translators: %d: HTTP status code returned by the Filecheck API.
$message = isset( $body['message'] ) ? $body['message'] : sprintf( __( 'API returned HTTP code %d', 'filecheck' ), $code );
return new WP_Error( 'sync_failed', $message );
}
return true;
}
public function get_job( $job_id, $secret_key = '' ) {
if ( empty( $secret_key ) ) {
$secret_key = get_option( 'filecheck_secret_key' );
}
if ( empty( $secret_key ) ) {
return new WP_Error( 'missing_key', __( 'Secret key is missing.', 'filecheck' ) );
}
$url = $this->api_url . '/jobs/' . urlencode( $job_id ) . '?expand=runs';
$response = wp_remote_get( $url, array(
'headers' => $this->get_headers( $secret_key ),
'timeout' => 15,
) );
if ( is_wp_error( $response ) ) {
return $response;
}
$code = wp_remote_retrieve_response_code( $response );
if ( $code !== 200 ) {
// translators: %d: HTTP status code returned by the Filecheck API.
return new WP_Error( 'http_error', sprintf( __( 'API returned HTTP code %d', 'filecheck' ), $code ) );
}
$job = json_decode( wp_remote_retrieve_body( $response ), true );
return $job;
}
public function get_job_summary( $job_id, $secret_key = '' ) {
$job = $this->get_job( $job_id, $secret_key );
if ( is_wp_error( $job ) ) {
return $job;
}
$files = array();
$runs = isset( $job['runs'] ) && is_array( $job['runs'] ) ? $job['runs'] : array();
foreach ( $runs as $run ) {
$run_id = isset( $run['id'] ) ? $run['id'] : '';
if ( empty( $run_id ) ) {
continue;
}
$name = isset( $run['name'] ) ? $run['name'] : $run_id;
$proofs = array();
if ( isset( $run['proofs'] ) && is_array( $run['proofs'] ) ) {
foreach ( $run['proofs'] as $proof ) {
if ( ! empty( $proof['url'] ) ) {
$proofs[] = array(
'url' => esc_url_raw( $proof['url'] ),
);
}
}
}
$files[] = array(
'runId' => $run_id,
'name' => $name,
'outcome' => isset( $run['outcome'] ) ? $run['outcome'] : null,
'status' => isset( $run['status'] ) ? $run['status'] : '',
'hasOutput' => ! empty( $run['hasOutput'] ),
'downloadUrl' => isset( $run['downloadUrl'] ) ? esc_url_raw( $run['downloadUrl'] ) : '',
'proofs' => $proofs,
);
}
return array(
'jobId' => $job_id,
'status' => isset( $job['status'] ) ? $job['status'] : '',
'files' => $files,
);
}
}