Skip to content

Commit ad686ba

Browse files
committed
regactor: update the APIClient
1 parent 2c3c8d0 commit ad686ba

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

server/classes/headstart/library/APIClient.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
<?php
22

33
namespace headstart\library;
4+
5+
use Exception;
6+
47
require_once dirname(__FILE__) . '/CommUtils.php';
58

69
class APIClient {
10+
private array $ini_array;
11+
private string $database;
12+
private string $base_route;
13+
private array $settings;
714

815
public function __construct($ini_array) {
9-
1016
$this->load_configs($ini_array);
1117
}
1218

13-
public function load_configs($ini_array) {
19+
public function load_configs(array $ini_array): void {
1420
$this->ini_array = $ini_array;
1521
$this->settings = $this->ini_array["general"];
1622
$this->database = $this->ini_array["connection"]["database"];
@@ -21,46 +27,44 @@ public function load_configs($ini_array) {
2127

2228
public function call_api($endpoint, $payload) {
2329
$route = $this->base_route . $endpoint;
30+
2431
try {
2532
$res = CommUtils::call_api($route, $payload);
33+
2634
if ($res["httpcode"] != 200) {
2735
$res["route"] = $route;
2836
$res = $this->handle_api_errors($res);
2937
}
38+
3039
return $res;
3140
}
3241
catch (Exception $e) {
3342
error_log("Error in APIClient: " . $e);
34-
$res = array("status"=>"error",
35-
"httpcode"=>500,
36-
"reason"=>array("unexpected data processing error"));
43+
$res = array("status"=>"error", "httpcode"=>500, "reason"=>array("unexpected data processing error"));
3744
return $res;
3845
}
39-
finally {
40-
}
4146
}
4247

4348
public function call_persistence($endpoint, $payload) {
4449
$route = $this->base_route . "persistence/" . $endpoint . "/" . $this->database;
50+
4551
try {
4652
$res = CommUtils::call_api($route, $payload);
53+
4754
if ($res["httpcode"] != 200) {
4855
$res["route"] = $route;
4956
$res = $this->handle_api_errors($res);
5057
}
58+
5159
return $res;
5260
}
5361
catch (Exception $e) {
5462
// what happens here is instead of bubbling the error up,
55-
// it is caught and we fake a response that looks like an error
63+
// fake a response that looks like an error
5664
// because of the hardcoded reason we loose the original error information
57-
$res = array("status"=>"error",
58-
"httpcode"=>500,
59-
"reason"=>array("unexpected data processing error"));
65+
$res = array("status"=>"error", "httpcode"=>500, "reason"=>array("unexpected data processing error"));
6066
return $res;
6167
}
62-
finally {
63-
}
6468
}
6569

6670
public function handle_api_errors($res) {
@@ -85,5 +89,4 @@ public function handle_api_errors($res) {
8589
error_log(("Trying to handle API errors: " . print_r($res, true)));
8690
return $res;
8791
}
88-
8992
}

0 commit comments

Comments
 (0)