From 35cef4dbaf1e373304e51f400b0d399de67e2a69 Mon Sep 17 00:00:00 2001 From: nikkel Date: Fri, 20 Mar 2020 01:46:16 +0700 Subject: [PATCH] Upload files to 'src' --- src/Client.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/Client.php diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..8694d3a --- /dev/null +++ b/src/Client.php @@ -0,0 +1,45 @@ +host = $host; + $this->serverKey = $serverKey; + } + + public function makeRequest($controller, $action, $parameters) + { + $url = sprintf('%s/api/v1/%s/%s', $this->host, $controller, $action); + + // Headers + $headers = [ + 'x-server-api-key' => $this->serverKey, + 'content-type' => 'application/json', + ]; + + // Make the body + $json = json_encode($parameters); + + // Make the request + $response = \Requests::post($url, $headers, $json); + + if ($response->status_code === 200) { + $json = json_decode($response->body); + + if ($json->status == 'success') { + return $json->data; + } else { + if (isset($json->data->code)) { + throw new Error(sprintf('[%s] %s', $json->data->code, $json->data->message)); + } else { + throw new Error($json->data->message); + } + } + } + + throw new Error('Couldn’t send message to API'); + } +}