Skip to content

Instantly share code, notes, and snippets.

@Nub
Created September 30, 2011 03:29
Show Gist options
  • Select an option

  • Save Nub/1252567 to your computer and use it in GitHub Desktop.

Select an option

Save Nub/1252567 to your computer and use it in GitHub Desktop.
<?php
/**
*
*
* @author Jack Perry, Zach Thayer
* @version 2.0
* @copyright Yoshimi Robotics. All rights reserved.
* @package YRAPI
**/
error_reporting(E_ALL);
require 'YRAPIMethod.php';
class YRAPIManager {
private $_plugins = array();
private $_data = "";
private $_apiCall = "";
private $_apiKey = "";
private $_database = "";
private $_error = "";
const ERROR_INVALID_APIKEY = "APIKey invalid";
const ERROR_PLUGIN_NOT_FOUND = "Method not immplemented";
const ERROR_NO_DATA_PROVIDED = "No data provided";
const ERROR_JSON_ECODE_FAILED = "Unable to encode JSON data";
const ERROR_JSON_DECODE_FAILED = "Unable to decode JSON data";
const ERROR_API_CALL_FAILED = "API plugin returned null value";
public function addPlugin(&$plugin) {
$this->_plugins[$plugin->name()] = $plugin;
}
public function setDatabaseHandler($databaseHandler) {
$this->_database = $databaseHandler;
}
public function setErrorHandler($errorHandler) {
$this->_error = $errorHandler;
}
public function run() {
$this->prepareAPIData();
if (!$this->validAPIKey($this->_apiKey))
$this->_error->raiseException('com.frequenciesapp.api', '406,0', YRAPIManager::ERROR_INVALID_APIKEY);
$plugin = $this->_apiCall['Action'];
if (!isset($this->_plugins[$plugin]))
$this->_error->raiseException('com.frequenciesapp.api', '406,1', YRAPIManager::ERROR_PLUGIN_NOT_FOUND);
$this->_data = $this->_apiCall['Data'];
if ($this->_plugins[$plugin]->requiresData())
if (!isset($this->_data) || empty($this->_data))
$this->_error->raiseException('com.frequenciesapp.api', '406,2', YRAPIManager::ERROR_NO_DATA_PROVIDED);
$output = $this->_plugins[$plugin]->invoke($this->_data);
if (!isset($output))
$this->_error->raiseException('com.frequenciesapp.api', '406,3', YRAPIManager::ERROR_API_CALL_FAILED);
$JSONOutput = json_encode($output);
if (!isset($JSONOutput))
$this->_error->raiseException('com.frequenciesapp.api', '406,4', YRAPIManager::ERROR_JSON_ENCODE_FAILED);
print($JSONOutput);
}
private function validAPIKey($APIKey) {
$validAPIKey = $this->_database->APIKeyIsValid($APIKey);
return $validAPIKey;
}
private function prepareAPIData() {
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$HTTPBody = @file_get_contents('php://input');
if (!isset($HTTPBody))
$this->_error->raiseException('com.frequenciesapp.api', '406,5', YRAPIManager::ERROR_NO_DATA_PROVIDED);
$this->_apiCall = json_decode($HTTPBody, true);
if (!isset($this->_apiCall))
$this->_error->raiseException('com.frequenciesapp.api', '409,6', YRAPIManager::ERROR_JSON_DECODE_FAILED);
$this->_apiKey = $this->_apiCall['APIKey'];
if (!isset($this->_apiKey))
$this->_error->raiseException('com.frequenciesapp.api', '406,7', YRAPIManager::ERROR_INVALID_APIKEY);
} else if ($_SERVER['REQUEST_METHOD'] == "GET") {
echo 'Using $_GET';
$arguments = split(';', $_GET['Args']);
$this->_data = array();
if (strlen($arguments[0])) {
foreach ($arguments as $key => $value) {
$argumentSplit = split('=', $value);
$this->_data[$argumentSplit[0]] = $argumentSplit[1];
}
} else
$this->_data = null;
$this->_apiCall = array(
'Action' => $_GET['Action'],
'APIKey' => $_GET['APIKey'],
'Data' => $this->_data
);
} else
$this->_error->raiseException('com.frequenciesapp.api', '406,8', YRAPIManager::ERROR_NO_DATA_PROVIDED);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment