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 <?php
class APIv1 extends Module class APIv1 extends CustomModule
{ {
protected $request_method = 'GET'; protected $request_methods = 'GET';
public function __construct() public function __construct()
{ {
parent::__construct();
$response_code = 200;
$error = 'An unexcepted error has occurred.';
// Checks the request method // 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 // 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');
} }
} }

View file

@ -11,14 +11,17 @@ class CustomModule extends Module
$this->redis = new CustomRedis(); $this->redis = new CustomRedis();
if (isset($_COOKIE['__auth'])) if (substr(get_class($this), 0, 3) != 'api')
{ {
list($uid, $auth_token) = explode('|', base64_decode($_COOKIE['__auth'])); if (isset($_COOKIE['__auth']))
if ($this->redis->hget('user:' . $uid, 'auth') === $auth_token)
{ {
$this->uid = $uid; list($uid, $auth_token) = explode('|', base64_decode($_COOKIE['__auth']));
$this->return['uid'] = $uid;
if ($this->redis->hget('user:' . $uid, 'auth') === $auth_token)
{
$this->uid = $uid;
$this->return['uid'] = $uid;
}
} }
} }
} }

View file

@ -2,9 +2,13 @@
class api_v1_leaderboards extends APIv1 class api_v1_leaderboards extends APIv1
{ {
protected $request_methods = 'GET';
public function __default() public function __default()
{ {
$leaderboards = new leaderboards();
return $leaderboards->__default();
} }
} }

View file

@ -6,27 +6,37 @@ class leaderboards extends UserModule
{ {
// Grabs the user's leaderboards // Grabs the user's leaderboards
$leaderboards = $this->redis->zrevrange('user:' . $this->uid . ':leaderboards:updated', 0, -1, 'WITHSCORES'); $leaderboards = $this->redis->zrevrange('user:' . $this->uid . ':leaderboards:updated', 0, -1, 'WITHSCORES');
$data = array();
if ($leaderboards) if ($leaderboards)
{ {
$this->redis->multi(); $this->redis->multi();
$leaderboard_uids = array(); // Grabs the additional fields
foreach ($leaderboards as $uid => $updated_at) foreach ($leaderboards as $uid => $updated_at)
{ {
$leaderboard_uids[] = $uid; unset($leaderboards[$uid]);
$leaderboards[(int)$uid] = $updated_at;
$this->redis->hmget('leaderboard:' . $uid, array('name', 'created_at')); $this->redis->hmget('leaderboard:' . $uid, array('name', 'created_at'));
} }
$data = array_combine($leaderboard_uids, $this->redis->exec()); $fields = $this->redis->exec();
foreach ($leaderboards as $uid => $updated_at)
{
$data = current($fields);
$leaderboards[$uid] = array(
'name' => $data['name'],
'created_at' => (int)$data['created_at'],
'updated_at' => (int)$updated_at,
);
next($fields);
}
} }
return array( return array('leaderboards' => $leaderboards);
'leaderboards' => $leaderboards,
'data' => $data,
);
} }
} }

View file

@ -22,15 +22,14 @@ if ($this->module['leaderboards'])
</tr></thead> </tr></thead>
<tbody> <tbody>
<?php <?php
foreach ($this->module['leaderboards'] as $uid => $updated_at) foreach ($this->module['leaderboards'] as $uid => $data)
{ {
$data = $this->module['data'][$uid];
?> ?>
<tr class="lead"> <tr class="lead">
<td><?php echo $uid; ?></td> <td><?php echo $uid; ?></td>
<td><a href="/leaderboard/<?php echo $uid; ?>"><?php echo $data['name']; ?></a></td> <td><a href="/leaderboard/<?php echo $uid; ?>"><?php echo $data['name']; ?></a></td>
<td><?php echo Time::ago($data['created_at']); ?></td> <td><?php echo Time::ago($data['created_at']); ?></td>
<td><?php echo Time::ago($updated_at); ?></td> <td><?php echo Time::ago($data['updated_at']); ?></td>
<td class="text-right"> <td class="text-right">
<div class="btn-group"> <div class="btn-group">
<a class="btn btn-danger" href="/leaderboard/delete/<?php echo $uid; ?>">Delete</a> <a class="btn btn-danger" href="/leaderboard/delete/<?php echo $uid; ?>">Delete</a>