Permalink
Browse files

Returning to the ol' API!

  • Loading branch information...
1 parent 4cef9dd commit 5bcd63e28922698bbb3eca38bba1d00e1c021937 @benkeen committed Jan 5, 2015
@@ -2,5 +2,5 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
-RewriteRule api/v1/(.*)$ api/process.php?v=1request=$1 [QSA,NC,L]
-</IfModule>
+RewriteRule ^api/v1/(.*)$ /generatedata/api/process.php?v=1&request=$1 [L]
+</IfModule>
View
@@ -0,0 +1,57 @@
+### generatedata REST API
+
+The purpose of the API is to allow you to programmatically generate data sets, rather than you to create them
+manually via the user interface. This would mean a big leap forward in letting you use the Data Generator in your
+automatic build tools. Useful for some. :)
+
+Right now it's a work in progress, but here's what I have planned.
+
+## How it'll work
+
+The Data Generator provides a TON of control over what you generate. The Data Types and Export Types all allow
+different settings unique to themselves. Some are very basic, like the Email Data Type which offers no additional
+settings at all, whereas others like the PAN Data Typs let you choose the credit card type, the possible formats and
+more.
+
+So... the API has to allow for all the same functionality offered by the UI. I figure it'll work like this:
+
+- All Data Types and Export Types have their own unique JSON schema file. That defines what settings are available
+for the plugin, what fields are required and what are optional.
+- To call the API to request some data, you'll need to *POST* (no, not GET) a JSON file to a /api/v1 URL. The payload
+of the POST would be a JSON file containing whatever you want, for example:
+
+```javascript
+{
+ "numRows": 100,
+ "countries": [],
+ "rows": [
+ {
+ "type": "Names",
+ "settings": {
+ "placeholder": "Name Initial. Surname"
+ }
+ }
+ ],
+ "export": {
+ "type": "JSON",
+ "settings": {
+ "stripWhitespace": false,
+ "dataStructureFormat": "simple"
+ }
+ }
+}
+```
+
+The "settings" properties in the "rows" and "export" sections would contain whatever unique settings are specific
+to the Data Type / Export Type.
+
+Because all the components (Core, Data Types & Export Types) are all schema'd out, any API call that passes invalid
+or incomplete JSON will be easy to identify. I think I'll be able to provide some pretty decent error handling and
+error feedback here.
+
+### Why not keep it RESTful and use GET?
+
+GET doesn't allow payloads. There's simply too much information needing to be passed for each API call to cram it all
+into a URL, so that leaves us with POST. It's un-RESTful I know, but there are bigger sins in the world. I don't think
+I'll lose any sleep over this.
+
View
@@ -1,17 +1,22 @@
<?php
require_once("../library.php");
+Core::init();
-print_r($_SERVER);
+if (!Core::isApiEnabled()) {
+ echo "Sorry, the API is not enabled.";
+ return;
+}
// requests from the same server don't have a HTTP_ORIGIN header
if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) {
$_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME'];
}
+
try {
$API = new MyAPI($_REQUEST['request'], $_SERVER['HTTP_ORIGIN']);
echo $API->processAPI();
} catch (Exception $e) {
echo json_encode(Array('error' => $e->getMessage()));
-}
+}
@@ -6,9 +6,9 @@
"example": {
"type": "string"
},
- "option": {
+ "placeholder": {
"type": "string"
}
},
- "required": ["option"]
-}
+ "required": ["placeholder"]
+}
@@ -3,7 +3,7 @@
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
- "stripWhitespace": { // TODO 1/0
+ "stripWhitespace": {
"type": "boolean"
},
"dataStructureFormat": {
@@ -31,6 +31,7 @@ class Core {
private static $useMinifiedResources = false;
private static $pluginSettings = array();
private static $timeout = 300; // 5 minutes
+ private static $apiEnabled = false;
// non-overridable settings
private static $version = "3.1.4";
@@ -195,6 +196,9 @@ private static function loadSettingsFile() {
if (isset($pluginSettings)) {
self::$pluginSettings = $pluginSettings;
}
+ if (isset($apiEnabled)) {
+ self::$apiEnabled = $apiEnabled;
+ }
// TODO temporary, during alpha dev
if (isset($allowThemes)) {
@@ -393,6 +397,14 @@ public static function getDefaultTheme() {
}
/**
+ * Determines whether the REST API functionality is available or not.
+ * @return bool
+ */
+ public static function isApiEnabled() {
+ return self::$apiEnabled;
+ }
+
+ /**
* Added in 3.1.4. This allows any plugins to have custom settings defined in $pluginSettings. This
* function returns null if no settings exist for the plugin, or whatever settings have been provided.
* @param $pluginType
View
@@ -5,9 +5,10 @@
* in most cases automatically create this file, but worst-case scenario (no permissions, for example) it will
* at least generate the file content so you can create it yourself.
*/
-$dbHostname = '';
-$dbName = '';
-$dbUsername = '';
-$dbPassword = '';
-$dbTablePrefix = '';
-$encryptionSalt = 'ABC'; // any 3 A-Z chars
+$dbHostname = '';
+$dbName = '';
+$dbUsername = '';
+$dbPassword = '';
+$dbTablePrefix = '';
+$encryptionSalt = 'ABC'; // any 3 A-Z chars
+$apiEnabled = false;
@@ -0,0 +1,19 @@
+{
+ "numRows": 100,
+ "countries": [],
+ "rows": [
+ {
+ "type": "Names",
+ "settings": {
+ "placeholder": "Name Initial. Surname"
+ }
+ }
+ ],
+ "export": {
+ "type": "JSON",
+ "settings": {
+ "stripWhitespace": false,
+ "dataStructureFormat": "simple"
+ }
+ }
+}

0 comments on commit 5bcd63e

Please sign in to comment.