-
Notifications
You must be signed in to change notification settings - Fork 3
/
rest.php
47 lines (39 loc) · 1.25 KB
/
rest.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
<?php
// TODO: use wp_remote_get when the backend fix query params
// TODO: recursive get until no more data
function request_merchants($env) {
$base_url = $env === 'staging'
? 'https://api.staging.bitfinex.com'
: 'https://api.bitfinex.com';
$url = $base_url . '/v2/ext/merchant/map/locations/list?page=1&pageSize=500';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function list_merchants(WP_REST_Request $request) {
$env = $request->get_param('env');
$cache_key = 'bfx-crypto-map.merchant_list.' . $env;
$exp_seconds = 60 * 10; // 10 mins
try {
$result = get_transient($cache_key);
if ($result === false) {
$response = request_merchants($env);
$result = json_decode($response);
set_transient($cache_key, $result, $exp_seconds);
}
return new WP_REST_Response($result, 200);
} catch (\Throwable $th) {
error_log($th);
return new WP_REST_Response(array("message" => "error"), 400);
}
}
add_action('rest_api_init', function () {
register_rest_route( 'bfx-crypto-map/v1', '/merchants', array(
'methods' => 'POST',
'callback' => 'list_merchants',
));
});
?>