From 5d8c727f940746e96186c00b9f53971e37f037e8 Mon Sep 17 00:00:00 2001 From: Bagaskara Wisnu Gunawan Date: Sat, 13 Apr 2019 19:45:50 +0700 Subject: [PATCH] Added More RESTful Endpoints (PUT, PATCH, DELETE) --- README.md | 11 ++++++ src/BasicApiClient.php | 76 ++++++++++++++++++++++++++---------- tests/BasicApiClientTest.php | 3 +- 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index f9dd145..5338824 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,17 @@ $client = new Yuca\BasicApiClient([ $client->get('articles', ['limit' => 10]); $client->post('article/create', ['title' => 'Awesome!']); + +$client->put('article/1', [ + 'title' => 'New Title', + 'body' => 'New Body' +]); + +$client->patch('article/1', [ + 'title' => 'New Title' +]); + +$client->delete('article/1'); ``` License diff --git a/src/BasicApiClient.php b/src/BasicApiClient.php index a254028..e054dba 100644 --- a/src/BasicApiClient.php +++ b/src/BasicApiClient.php @@ -6,25 +6,25 @@ class BasicApiClient { - + /** * The base uri of api url * @var string */ protected $baseUrl; - + /** * The username of basic http authentication * @var string */ protected $username = ''; - + /** * The password of basic http authentication * @var string */ protected $password = ''; - + /** * Return result data as 'array' or 'object' * @var string @@ -42,7 +42,7 @@ public function __construct(array $options) $this->$key = $value; } } - + // Validate the attributes $vars = get_object_vars($this); foreach ($vars as $key => $val) { @@ -52,11 +52,44 @@ public function __construct(array $options) } } + /** + * HTTP DELETE request call + * @param striing $function + * @param array $parameters + * @return object/array + */ + public function delete($function, array $parameters = []) + { + return $this->request('delete', $function, $parameters); + } + + /** + * HTTP PATCH request call + * @param striing $function + * @param array $parameters + * @return object/array + */ + public function patch($function, array $parameters = []) + { + return $this->request('patch', $function, $parameters); + } + + /** + * HTTP PUT request call + * @param striing $function + * @param array $parameters + * @return object/array + */ + public function put($function, array $parameters = []) + { + return $this->request('put', $function, $parameters); + } + /** * HTTP POST request call - * @param striing $function - * @param array $parameters - * @return object/array + * @param striing $function + * @param array $parameters + * @return object/array */ public function post($function, array $parameters = []) { @@ -65,9 +98,9 @@ public function post($function, array $parameters = []) /** * HTTP GET request call - * @param striing $function - * @param array $parameters - * @return object/array + * @param striing $function + * @param array $parameters + * @return object/array */ public function get($function, array $parameters = []) { @@ -77,24 +110,25 @@ public function get($function, array $parameters = []) /** * Perform HTTP request call * @param string $method POST|GET - * @param string $function - * @param array $parameters - * @return object/array + * @param string $function + * @param array $parameters + * @return object/array */ public function request($method, $function, array $parameters = []) { - $url = trim($this->baseUrl, '/') . '/' . trim($function, '/'); + $method = strtoupper($method); $params = http_build_query($parameters); + $url = trim($this->baseUrl, '/') . '/' . trim($function, '/'); $context = []; $headers = []; if ($this->username && $this->password) { - $headers[] = 'Authorization: Basic ' . base64_encode($this->username . ':' . $this->password); - } + $headers[] = 'Authorization: Basic ' . base64_encode($this->username . ':' . $this->password); + } - if (strtoupper($method) == 'POST') { - $context['http']['method'] = 'POST'; + if (in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE'])) { + $context['http']['method'] = $method; $context['http']['content'] = $params; $headers[] = 'Content-Type: application/x-www-form-urlencoded'; } else { @@ -107,9 +141,9 @@ public function request($method, $function, array $parameters = []) $contents = file_get_contents($url, false, stream_context_create($context)); if ($contents !== false) { - return json_decode($contents, $this->return == 'array'); + return json_decode($contents, $this->return == 'array'); } else { - throw new Exception('Error processing request.'); + throw new Exception('Error processing request.'); } } } diff --git a/tests/BasicApiClientTest.php b/tests/BasicApiClientTest.php index 98d5787..e90a2bc 100644 --- a/tests/BasicApiClientTest.php +++ b/tests/BasicApiClientTest.php @@ -1,8 +1,7 @@