forked from benkeen/generatedata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.html
395 lines (341 loc) · 15 KB
/
api.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data Generator</title>
<link href="assets/css/bootstrap.css" rel="stylesheet">
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
<link href="assets/css/docs.css" rel="stylesheet">
<link href="assets/js/google-code-prettify/prettify.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#pagenav">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<ul class="nav">
<li id="mainTitle">
<button id="mobileNavLink" data-target=".nav-collapse" data-toggle="collapse" class="btn btn-navbar" type="button">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="index.html"></a>
</li>
<li>
<ul id="mainNav" class="nav">
<li><a href="index.html">User Doc</a></li>
<li class="dropdown">
<a href="developer.html" class="dropdown-toggle" data-toggle="dropdown">Developer Doc <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="developer.html">Introduction</a></li>
<li class="divider"></li>
<li class="nav-header">Module Types</li>
<li><a href="dataTypes.html">Data Types</a></li>
<li><a href="exportTypes.html">Export Types</a></li>
<li><a href="countryPlugins.html">Country Plugins</a></li>
<li class="divider"></li>
<li><a href="translations.html">Translations</a></li>
</ul>
</li>
<li class="active"><a href="api.html">API</a></li>
<li><a href="contribute.html">Contributing</a></li>
<li><a href="scriptsUsed.html">Scripts Used</a></li>
</ul>
<ul id="tabletNav" class="nav">
<li class="dropdown">
<a href="developer.html" class="dropdown-toggle" data-toggle="dropdown">Menu <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="index.html">User Documentation</a></li>
<li><a href="install.html">How to Install</a></li>
<li class="divider"></li>
<li class="nav-header">Developer Documentation</li>
<li><a href="developer.html">Introduction</a></li>
<li class="divider"></li>
<li class="nav-header">Module Types</li>
<li><a href="dataTypes.html">Data Types</a></li>
<li><a href="exportTypes.html">Export Types</a></li>
<li><a href="countryPlugins.html">Country Plugins</a></li>
<li class="divider"></li>
<li><a href="translations.html">Translations</a></li>
<li class="divider"></li>
<li><a href="contribute.html">How to Contribute / Contact Me</a></li>
<li><a href="scriptsUsed.html">Scripts Used</a></li>
</ul>
</li>
</ul>
<ul id="mobileNav" class="nav">
<li class="dropdown">
<div class="nav-collapse collapse">
<ul class="dropdown-menu">
<li><a href="index.html">User Documentation</a></li>
<li><a href="install.html">How to Install</a></li>
<li class="divider"></li>
<li class="nav-header">Developer Documentation</li>
<li><a href="developer.html">Introduction</a></li>
<li class="divider"></li>
<li class="nav-header">Module Types</li>
<li><a href="dataTypes.html">Data Types</a></li>
<li><a href="exportTypes.html">Export Types</a></li>
<li><a href="countryPlugins.html">Country Plugins</a></li>
<li class="divider"></li>
<li><a href="translations.html">Translations</a></li>
<li class="divider"></li>
<li><a href="contribute.html">How to Contribute / Contact Me</a></li>
<li><a href="scriptsUsed.html">Scripts Used</a></li>
</ul>
</div>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="span3 bs-docs-sidebar" id="pagenav">
<ul class="nav nav-list bs-docs-sidenav" data-spy="affix">
<li class="active"><a href="#intro"><i class="icon-chevron-right"></i> Introduction</a></li>
<li><a href="#enable"><i class="icon-chevron-right"></i> How to Enable</a></li>
<li><a href="#how_it_works"><i class="icon-chevron-right"></i> How the API Works</a></li>
<li><a href="#readme_files"><i class="icon-chevron-right"></i> Examples / README files</a></li>
<li><a href="#error_handling"><i class="icon-chevron-right"></i> Error Handling</a></li>
<li><a href="#questions_bugs"><i class="icon-chevron-right"></i> Questions / Bugs?</a></li>
</ul>
</div>
<div class="span9">
<section id="intro">
<div class="hero-unit">
<h1>API</h1>
<p>
This page explains how to use the REST API to programmatically generate data without having to use
the UI.
</p>
</div>
<h2>Introduction</h2>
<p>
The API was added in 3.2.0. All Data Types and Export Types were updated for compatibility, so you can
now generate custom data by calling a REST endpoint. At this stage the API is pretty basic: there's no
user authentication and the API is either enabled or not. That's pretty much it. This page explains how it
all works: how to enable the API and how you can construct your requests to retrieve precisely what data
you need.
</p>
<p>
Please note: the API is only available for your own installations. It's not available via the main
generatedata.com website mainly due to bandwidth constraints.
</p>
</section>
<hr size="1" />
<section id="enable">
<h2>How to Enable</h2>
<p>
The API is disabled by default. To enable it, first <a href="install.html" target="_blank">install the
script</a> then edit your <code>settings.php</code> file (found in the root folder of where you installed it)
and add the following line:
</p>
<pre class="prettyprint linenums">
$apiEnabled = true;
</pre>
<p>
Once this is done, open up the following URL in your browser: <code>http://yoursite/api/v1/data</code> (you'll
need to replace the <code>yoursite</code> bit with whatever URL is appropriate. If it's working properly,
you should see the following:
</p>
<pre class="prettyprint linenums">
{
"error": "Sorry, this endpoint only accepts POST requests. Please see the help documentation."
}
</pre>
<p>
So far so good!
</p>
</section>
<hr size="1" />
<section id="how_it_works">
<h2>How the API Works</h2>
<p>
Now for the fun stuff! The API works by allowing you to <b>POST JSON content to the /api/v1/data endpoint.</b>
The JSON content contains all the settings needed to generate the content you want. It's the JSON equivalent
of using the UI.
</p>
<h4>POST, to GET something?! But this isn't RESTful!</h4>
<p>
Quite honestly, I couldn't care less. I've always found REST an <i>adequate-at-best</i> set of guidelines,
so believe me I'm not losing any sleep over this. The problem was that the range of settings offered by the
generatedata UI is <b>vast</b>. Offering the same functionality via REST ruled out passing all the info
via the query string since the URLs would become far too long. So instead, "REST Best Practices"
(i.e. "REST Least Awful practices") says you should use a POST. Sure.
</p>
<h4>Now back to regular scheduled programming</h4>
<p>
So! As I was saying, you need to POST some JSON content in the body of the request to the <b>/api/v1/data</b>
endpoint. The top-level JSON properties are of the following form:
</p>
<pre class="prettyprint linenums">
{
"numRows": N,
"countries": [],
"rows": [
// list of Data Types hear, each
],
"export": {
// export settings here
}
}</pre>
<ul>
<li><code>numRows</code>: (<b>required</b>). The number of rows to generate.</li>
<li><code>countries</code>: (<b>optional</b>). An array of country slugs. These correspond to the Country
plugins section within the UI. To find the available country slugs, take a look at the folders in the
<b>/plugins/countries</b>. In those folders, each Country plugin has a <code>[Country].class.php</code>
file. At the top of those files, there's a <code>countrySlug</code> setting - that's the string value
you'll want to use.</li>
<li><code>rows</code>: (<b>required</b>). This is where you define what data you actually want to generate.
This is an array of objects, each object needs to be of the following form:
<pre class="prettyprint linenums">
{
"type": "DataTypeFolderName",
"title": "My title",
"settings": {
// ...
}
}
</pre>
<ul>
<li><code>type</code>: (<b>required</b>). The folder name of the Data Type you want to use.</li>
<li><code>title</code>: (<b>required</b>). the label for the field. Depending on your Export Type, this will be used
for things like the XML node name, database column name, JSON property name, etc. </li>
<li><code>settings</code>: (<b>optional</b>). This object contains any arbitrary settings that belong to the
Data Type. Each Data Type is unique and has 0-N settings. For details about the available settings for each
Data Type, check out the <a href="#readme_files">README files</a> section below.</li>
</ul>
</li>
<li>
<code>export</code>: (<b>required</b>). This object specifies what format you want your generated data to be in.
In other words, it targets a particular Export Type. Structurally it looks very much like the Data Type object:
<pre class="prettyprint linenums">
{
"type": "ExportTypeFolderName",
"settings": {
// ...
}
}
</pre>
<ul>
<li><code>type</code>: (<b>required</b>). The folder name of the Export Type you want to use.</li>
<li><code>settings</code>: (<b>optional</b>). This object contains any arbitrary settings that belong to the
Export Type. Like with Data Types, every Export Type is unique and has 0-N settings. For details about
the available settings for each Data Type, check out the <a href="#readme_files">README files</a> section below.</li>
</ul>
</li>
</ul>
<p>
Since generatedata.com is really just an <i>engine</i> designed to be extended, the vast bulk of the work being
done to generate the data is done by plugins: Data Types and Export Types. The API is made possible by
getting all plugins to be <b>well defined</b>. Any plugin that offers one or more custom settings defines a
<code>schema.json</code> file containing a schema of those settings.
</p>
</section>
<hr size="1" />
<section id="readme_files">
<h2>Examples / README files</h2>
<p>
All Data Types and Export Types have their own <code>README.md</code> files containing documentation about
how they can be called via the API. They all contain example JSON that you can POST to your API endpoint. This
should greatly help you get up to speed quickly! To find the READMEs, just browse on github to the appropriate
plugin folder. For example:
</p>
<ul>
<li><a href="https://github.com/benkeen/generatedata/tree/master/plugins/dataTypes/AlphaNumeric">AutoIncrement Data Type README.md</a></li>
<li><a href="https://github.com/benkeen/generatedata/tree/master/plugins/exportTypes/JSON">JSON Export Type README.md</a></li>
</ul>
<p>
If something isn't clear, or you feel it's incomplete either create a github issue, or just fork the project
and fix the doc yourself. At the time of writing (Jan 2015), the doc is pretty complete, and I'll endeavor
to keep it up to date.
</p>
</section>
<hr size="1" />
<section id="error_handling">
<h2>Error Handling</h2>
<p>
I've included a lot of (hopefully useful) error handling for the API to provide human-friendly feedback on
what's gone wrong. I'll just jot down a few notes here.
</p>
<h4>Syntax error</h4>
<p>
The most basic error is a JSON syntax error. That'll yield the following error:
</p>
<pre class="prettyprint linenums">
{"error":"API_INVALID_JSON","error_details":"Syntax error (JSON_ERROR_SYNTAX)"}
</pre>
<p>
Unfortunately the JS validator being used doesn't contain any more info than that. So if you run into this problem
go online and find a JS validator like <a href="http://jsonlint.com/" target="_blank">JSONLint.com</a>.
</p>
<h4>Missing / Invalid Data Type / Export Type settings</h4>
<p>
The errors here are super clear. It tells you exactly where the error occurs and what type of error is
occurring. For example:
</p>
<pre class="prettyprint linenums">
{
"error": "API_INVALID_DATA_TYPE_JSON",
"error_details": "Invalid Data Type JSON `settings` content passed",
"validation_error": "Missing required property: placeholder",
"location": "index 1 of the `rows` array",
"data_type":"AlphaNumeric"
}
</pre>
<p>
Hopefully these sort of errors will be easily located based on the supplied errors. If not, let me know!
</p>
</section>
<hr size="1" />
<section id="questions_bugs">
<h2>Questions / Bugs?</h2>
<p>
Create an issue on github. That's by far the best way to reach me - and others - who may be able to respond.
</p>
</section>
</div>
</div>
</div>
<script src="assets/js/jquery.js"></script>
<script src="assets/js/google-code-prettify/prettify.js"></script>
<script src="assets/js/bootstrap-transition.js"></script>
<script src="assets/js/bootstrap-alert.js"></script>
<script src="assets/js/bootstrap-modal.js"></script>
<script src="assets/js/bootstrap-dropdown.js"></script>
<script src="assets/js/bootstrap-scrollspy.js"></script>
<script src="assets/js/bootstrap-tab.js"></script>
<script src="assets/js/bootstrap-tooltip.js"></script>
<script src="assets/js/bootstrap-popover.js"></script>
<script src="assets/js/bootstrap-button.js"></script>
<script src="assets/js/bootstrap-collapse.js"></script>
<script src="assets/js/bootstrap-carousel.js"></script>
<script src="assets/js/bootstrap-typeahead.js"></script>
<script src="assets/js/bootstrap-affix.js"></script>
<script>$(function() { prettyPrint(); });</script>
<footer>
<div class="container">
<div class="row">
<div class="span12">
</div>
</div>
</div>
</footer>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-41026944-1', 'benkeen.github.io');
ga('send', 'pageview');
</script>
</body>
</html>