Skip to content

Instantly share code, notes, and snippets.

@uniacid
Created March 30, 2016 19:04
Show Gist options
  • Save uniacid/2597efb10ce5886e5de602e182e94b52 to your computer and use it in GitHub Desktop.
Save uniacid/2597efb10ce5886e5de602e182e94b52 to your computer and use it in GitHub Desktop.
raml test
<?php
namespace JukeboxApi;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Client as HttpClient;
const TEMPLATE_REGEXP = '/\{([^\{\}]+)\}/';
/**
* @param string $string
* @param mixed $interpolate
* @param mixed $defaults
* @return string
*/
function template($string, $interpolate = [], $defaults = []) {
$defaults = !is_null($defaults) ? $defaults : [];
$interpolate = !is_null($interpolate) ? $interpolate : [];
return preg_replace_callback(TEMPLATE_REGEXP, function ($matches) use ($defaults, $interpolate) {
$key = $matches[1];
if (isset($interpolate[$key]) && $interpolate[$key] != null) {
return urlencode($interpolate[$key]);
}
if (isset($defaults[$key]) && $defaults[$key] != null) {
return urlencode($defaults[$key]);
}
return '';
}, $string);
}
class Parameter
{
protected $key;
protected $value;
public function __construct($key, $value = null)
{
if (empty($key)) {
throw new \InvalidArgumentException('no key given');
}
$this->key = $key;
$this->value = $value;
}
public function getId()
{
return $this->key;
}
public function getValue()
{
return $this->value;
}
public function __toString()
{
$value = $this->getValue();
if (is_null($value)) {
$paramStr = $this->key;
} elseif (is_bool($value)) {
$paramStr = $this->key . '=' . ($value ? 'true' : 'false');
} else {
$paramStr = $this->key . '=' . urlencode((string)$value);
}
return $paramStr;
}
}
class MultiParameter extends Parameter
{
public function getId()
{
return $this->__toString();
}
}
class Resource
{
protected $client;
protected $uri;
public function __construct($uri, Client $client)
{
$this->uri = $uri;
$this->client = $client;
}
}
class Resource0 extends Resource
{
public $songs;
public $artists;
public $albums;
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
$this->songs = new Resource1($this->uri . '/songs', $client);
$this->artists = new Resource4($this->uri . '/artists', $client);
$this->albums = new Resource7($this->uri . '/albums', $client);
}
}
class Resource1 extends Resource
{
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
}
public function songId($songId)
{
$uri = $this->uri . template('/{0}', [$songId]);
return new Resource2($uri, $this->client);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
public function post($body = null, $options = [])
{
return $this->client->request($this->uri, 'POST', $body, $options);
}
}
class Resource2 extends Resource
{
public $fileContent;
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
$this->fileContent = new Resource3($this->uri . '/file-content', $client);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
}
class Resource3 extends Resource
{
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
public function post($body = null, $options = [])
{
return $this->client->request($this->uri, 'POST', $body, $options);
}
}
class Resource4 extends Resource
{
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
}
public function artistId($artistId)
{
$uri = $this->uri . template('/{0}', [$artistId]);
return new Resource5($uri, $this->client);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
public function post($body = null, $options = [])
{
return $this->client->request($this->uri, 'POST', $body, $options);
}
}
class Resource5 extends Resource
{
public $albums;
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
$this->albums = new Resource6($this->uri . '/albums', $client);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
}
class Resource6 extends Resource
{
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
}
class Resource7 extends Resource
{
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
}
public function albumId($albumId)
{
$uri = $this->uri . template('/{0}', [$albumId]);
return new Resource8($uri, $this->client);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
public function post($body = null, $options = [])
{
return $this->client->request($this->uri, 'POST', $body, $options);
}
}
class Resource8 extends Resource
{
public $songs;
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
$this->songs = new Resource9($this->uri . '/songs', $client);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
}
class Resource9 extends Resource
{
public function __construct($uri, $client)
{
parent::__construct($uri, $client);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
}
class CustomResource extends Resource
{
public function acl($body = null, $options = [])
{
return $this->client->request($this->uri, 'ACL', $body, $options);
}
public function bind($body = null, $options = [])
{
return $this->client->request($this->uri, 'BIND', $body, $options);
}
public function checkout($body = null, $options = [])
{
return $this->client->request($this->uri, 'CHECKOUT', $body, $options);
}
public function connect($body = null, $options = [])
{
return $this->client->request($this->uri, 'CONNECT', $body, $options);
}
public function copy($body = null, $options = [])
{
return $this->client->request($this->uri, 'COPY', $body, $options);
}
public function delete($body = null, $options = [])
{
return $this->client->request($this->uri, 'DELETE', $body, $options);
}
public function get($body = null, $options = [])
{
return $this->client->request($this->uri, 'GET', $body, $options);
}
public function head($body = null, $options = [])
{
return $this->client->request($this->uri, 'HEAD', $body, $options);
}
public function link($body = null, $options = [])
{
return $this->client->request($this->uri, 'LINK', $body, $options);
}
public function lock($body = null, $options = [])
{
return $this->client->request($this->uri, 'LOCK', $body, $options);
}
public function mSearch($body = null, $options = [])
{
return $this->client->request($this->uri, 'M-SEARCH', $body, $options);
}
public function merge($body = null, $options = [])
{
return $this->client->request($this->uri, 'MERGE', $body, $options);
}
public function mkactivity($body = null, $options = [])
{
return $this->client->request($this->uri, 'MKACTIVITY', $body, $options);
}
public function mkcalendar($body = null, $options = [])
{
return $this->client->request($this->uri, 'MKCALENDAR', $body, $options);
}
public function mkcol($body = null, $options = [])
{
return $this->client->request($this->uri, 'MKCOL', $body, $options);
}
public function move($body = null, $options = [])
{
return $this->client->request($this->uri, 'MOVE', $body, $options);
}
public function notify($body = null, $options = [])
{
return $this->client->request($this->uri, 'NOTIFY', $body, $options);
}
public function options($body = null, $options = [])
{
return $this->client->request($this->uri, 'OPTIONS', $body, $options);
}
public function patch($body = null, $options = [])
{
return $this->client->request($this->uri, 'PATCH', $body, $options);
}
public function post($body = null, $options = [])
{
return $this->client->request($this->uri, 'POST', $body, $options);
}
public function propfind($body = null, $options = [])
{
return $this->client->request($this->uri, 'PROPFIND', $body, $options);
}
public function proppatch($body = null, $options = [])
{
return $this->client->request($this->uri, 'PROPPATCH', $body, $options);
}
public function purge($body = null, $options = [])
{
return $this->client->request($this->uri, 'PURGE', $body, $options);
}
public function put($body = null, $options = [])
{
return $this->client->request($this->uri, 'PUT', $body, $options);
}
public function rebind($body = null, $options = [])
{
return $this->client->request($this->uri, 'REBIND', $body, $options);
}
public function report($body = null, $options = [])
{
return $this->client->request($this->uri, 'REPORT', $body, $options);
}
public function search($body = null, $options = [])
{
return $this->client->request($this->uri, 'SEARCH', $body, $options);
}
public function subscribe($body = null, $options = [])
{
return $this->client->request($this->uri, 'SUBSCRIBE', $body, $options);
}
public function trace($body = null, $options = [])
{
return $this->client->request($this->uri, 'TRACE', $body, $options);
}
public function unbind($body = null, $options = [])
{
return $this->client->request($this->uri, 'UNBIND', $body, $options);
}
public function unlink($body = null, $options = [])
{
return $this->client->request($this->uri, 'UNLINK', $body, $options);
}
public function unlock($body = null, $options = [])
{
return $this->client->request($this->uri, 'UNLOCK', $body, $options);
}
public function unsubscribe($body = null, $options = [])
{
return $this->client->request($this->uri, 'UNSUBSCRIBE', $body, $options);
}
}
class Client
{
private $httpClient;
private $options;
public $resources;
public $resource;
public function __construct($options = [])
{
$baseUriParameters = [];
if (isset($options['baseUriParameters'])) {
$baseUriParameters = $options['baseUriParameters'];
unset($options['baseUriParameters']);
}
if (isset($options['baseUri'])) {
$options['base_uri'] = template($options['baseUri'], $baseUriParameters);
unset($options['baseUri']);
} else {
$options['base_uri'] = template('http://jukebox.api.com', []);
}
$this->options = $options;
$this->httpClient = new HttpClient($this->options);
$this->resources = new Resource0('.', $this);
}
protected function buildRequest($path, $method, $body = null, $options = [])
{
$options = array_merge($this->options, $options);
$hasBody = $method !== 'GET' && $method !== 'HEAD';
$reqQuery = [];
if (isset($options['query'])) {
$reqQuery = $options['query'];
unset($options['query']);
}
$headers = isset($options['headers']) ? $options['headers'] : [];
$request = new Request($method, $path, $headers);
if ($hasBody && !is_null($body)) {
$request = $request->withBody($body);
}
if (!$hasBody && !is_null($body)) {
$reqQuery = array_merge($reqQuery, $body);
}
if (count($reqQuery) > 0) {
$params = [];
foreach ($reqQuery as $key => $value) {
if (!is_array($value)) {
$param = new Parameter($key, $value);
$params[$param->getId()] = $param;
} else {
foreach ($value as $data) {
$param = new MultiParameter($key, $data);
$params[$param->getId()] = $param;
}
}
}
$params = array_map(
function ($param) {
return (string)$param;
},
$params
);
sort($params);
$query = implode('&', $params);
$uri = $request->getUri()->withQuery($query);
$request = $request->withUri($uri);
}
return $request;
}
public function request($path, $method, $body = null, $options = [])
{
return $this->send($this->buildRequest($path, $method, $body, $options));
}
public function resource($route, $parameters)
{
$path = '/' . ltrim(template($route, $parameters),'/');
return new CustomResource($path, $this);
}
protected function send(Request $request)
{
return $this->getHttpClient()->send($request);
}
public function getHttpClient()
{
return $this->httpClient;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment