Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 55 additions & 21 deletions src/BasicApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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 = [])
{
Expand All @@ -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 = [])
{
Expand All @@ -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 {
Expand All @@ -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.');
}
}
}
3 changes: 1 addition & 2 deletions tests/BasicApiClientTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

use PHPUnit\Framework\TestCase;
use InvalidArgumentException;
use Yuca\BasicApiClient;
use PHPUnit\Framework\TestCase;

/**
* @group unit
Expand Down