Got that first API call done

Had to work out the API parent class a bit. Also closing #8 as the API docs are about done. Time to knock out the rest of the API calls so I can start using this shit.
This commit is contained in:
Josh Sherman 2013-12-18 16:38:21 -05:00
parent 7b18541a61
commit 1006cbd436
5 changed files with 100 additions and 21 deletions

View file

@ -1,14 +1,77 @@
<?php
class APIv1 extends Module
class APIv1 extends CustomModule
{
protected $request_method = 'GET';
protected $request_methods = 'GET';
public function __construct()
{
parent::__construct();
$response_code = 200;
$error = 'An unexcepted error has occurred.';
// Checks the request method
if ((is_array($this->request_methods) && !in_array($this->request_methods, $_SERVER['REQUEST_METHOD']))
|| $this->request_methods != $_SERVER['REQUEST_METHOD'])
{
$response_code = 400;
$error = 'Invalid request method.';
}
// Checks the key
try
{
if (isset($_REQUEST['key']))
{
if (strlen($_REQUEST['key']) == 40)
{
$uid = $this->redis->get('user:api:' . $_REQUEST['key']);
if ($uid)
{
$this->uid = $uid;
}
else
{
throw new Exception('Invalid key.');
}
}
else
{
throw new Exception('Invalid key length.');
}
}
else
{
throw new Exception('Missing key.');
}
}
catch (Exception $e)
{
$response_code = 401;
$error = $e->getMessage();
}
if (isset($_REQUEST['suppress_response_codes']))
{
$response_code = 200;
}
$this->response_code = $response_code;
if ($response_code != 200)
{
$this->error = $error;
}
Browser::status($response_code);
}
public function __destruct()
{
// Unsure why I had to put this here, I guess it's being overridden somewhere in PICKLES
header('Content-type: application/json');
}
}