Skip to content

Instantly share code, notes, and snippets.

@Mizore
Last active December 19, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mizore/5939745 to your computer and use it in GitHub Desktop.
Save Mizore/5939745 to your computer and use it in GitHub Desktop.
Hexide api helper.
<?php
namespace Hexide;
class RequestType
{
const Json = 1;
const Binary = 2;
}
class ApiRequest
{
/**
* @var string $Structure Request structure.
*/
public $Structure;
public $Type;
/**
* Checks if parameter list is correct for that request.
*
* @param array $parameters parameter list.
* @return bool
*/
public function Verify(array $parameters = null)
{
return false;
}
public function BuildRequest(array $parameters = null)
{
return vsprintf($this->Structure, $parameters);
}
}
class BeatmapList extends ApiRequest
{
public $Structure = "beatmaps";
public $Type = RequestType::Json;
public function Verify(array $p = null)
{
return $p == null || count($p) == 0;
}
}
class BeatmapDetails extends ApiRequest
{
public $Structure = "beatmaps/%s";
public $Type = RequestType::Json;
public function Verify(array $p = null)
{
return $p !== null && count($p) == 1 && ctype_digit((string)$p[0]);
}
}
class BeatmapDownload extends ApiRequest
{
public $Structure = "beatmaps/%s/download";
public $Type = RequestType::Binary;
public function Verify(array $p = null)
{
return $p !== null && count($p) == 1 && ctype_digit((string)$p[0]);
}
}
class BeatmapMetadata extends ApiRequest
{
public $Structure = "metadata/%s";
public $Type = RequestType::Json;
public function Verify(array $p = null)
{
return $p !== null && count($p) == 1 && ctype_digit((string)$p[0]);
}
}
class BeatmapMetadataRaw extends ApiRequest
{
public $Structure = "metadata/%s/raw";
public $Type = RequestType::Binary;
public function Verify(array $p = null)
{
return $p !== null && count($p) == 1 && ctype_digit((string)$p[0]);
}
}
/**
* show off @method
*
* @method array BeatmapList()
* @method array BeatmapDetails(\int $RankedBeatmapID)
* @method binary BeatmapDownload(\int $RankedBeatmapID)
* @method array BeatmapMetadata(\int $BeatmapID)
* @method string BeatmapMetadataRaw(\int $MetadataID)
*/
class Api
{
private $location = "http://api.osu.miz.hexide.com/";
public function __construct()
{
}
public function __call($name, array $args)
{
$class_name = "\\Hexide\\{$name}";
if(!$this->IsValidApiRequest($name))
throw new \ErrorException("Invalid api request.");
/** @var ApiRequest $api_request */
$api_request = new $class_name();
if(!$api_request->Verify($args))
throw new \ErrorException("Invalid parameters.");
$ch = curl_init(sprintf("%s%s", $this->location, $api_request->BuildRequest($args)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$status = curl_getinfo($ch);
curl_close($ch);
if($status['http_code'] == 404)
return null;
if($status['http_code'] != 404 && $status['http_code'] != 200)
throw new \ErrorException("Invalid response received from api.");
switch($api_request->Type)
{
case RequestType::Json: return json_decode($data, true);
case RequestType::Binary:
default:
return $data;
}
}
/**
* @param string $request Api request name
* @return bool
*/
private function IsValidApiRequest($request)
{
switch($request)
{
case "BeatmapList":
case "BeatmapDetails":
case "BeatmapDownload":
case "BeatmapMetadata":
case "BeatmapMetadataRaw":
return true;
default: return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment