Skip to content

Instantly share code, notes, and snippets.

@pschultz
Created June 20, 2012 13:09
Show Gist options
  • Save pschultz/2959819 to your computer and use it in GitHub Desktop.
Save pschultz/2959819 to your computer and use it in GitHub Desktop.
o2p REST API draft
<?php
function query($action, $params = array()) {
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => "Content-type: application/json\r\n",
'ignore_errors' => true,
)
));
$url = http_build_url("http://apihost/adcounter/", array(
'path' => $action,
'query' => http_build_query($params),
), HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY);
return json_decode(file_get_contents($url, false, $context));
}
function submit($action, $data, $params = array()) {
$context = stream_context_create(array(
'http' => array(
'method' => 'PUT',
'content' => $data,
'header' => "Content-type: application/json\r\n",
'ignore_errors' => true,
)
));
$url = http_build_url("http://apihost/adcounter/", array(
'path' => $action,
'query' => http_build_query($params),
), HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY);
return json_decode(file_get_contents($url, false, $context));
}
<?php
$issue_list = query('private_issue_list');
list($issue_id, $issue_name) = each($issue_list);
# OR
# $issue_list = query('commercial_issue_list');
# list($issue_id, $issue_name) = each($issue_list);
$category_tree = query('category_tree', compact('issue_id'));
$category_path = $category_tree[0]['_children'][1]['path'];
$release_date_combination_list = query('release_date_combination_list', compact('issue_id', 'category_path'));
list($release_date_combination_id, $date_combination_name) = each($release_date_combination_list);
/**
* Note that $release_date_combination_id and $date_combination_name may
* very well be an empty string. This list might also be empty. In that
* case use an empty string as $release_date_combination_id in successive calls.
* Please send hate mail to info@alfamedia.com.
*/
$release_dates = query('release_dates', compact('issue_id', 'category_path', 'release_date_combination_id'));
$templates = query('private_templates', compact('issue_id', 'category_path'));
# OR:
# $templates = query('commercial_templates', compact('issue_id', 'category_path'));
$template_path = $templates[0]['path'];
$schema = query('schema', compact('template_path'));
$job = submit('job_details', $form_content, compact('template_path'));
$job_id = $job['id'];
$price = submit('get_price', $order_data, compact('issue_id', 'category_path', 'template_path', 'release_date', 'release_date_combination_id'));
$order = submit('commit', $order_data, compact('issue_id', 'category_path', 'template_path', 'release_date', 'release_date_combination_id'));
$order_id = $order['id'];
/**
* Make sure to check for '409 Validation failed' for all PUT requests
*/
// issue_list.json
[
{ "012": "Ausgabe A" },
{ "018": "Ausgabe B" }
]
// category_tree.json
[
{
"path": "1900541",
"name": "Vermietungen",
"_isLeaf": false,
"_children": [
{
"path": "1900541/1900578",
"name": "Vermietungen - 2-Zimmer",
"_isLeaf": true
"_children": []
},
{
"path": "1900541/1900578",
"name": "Vermietungen - 3-Zimmer",
"_isLeaf": true
"_children": []
}
]
},
{
"path": "{$issue_id}/1900641",
"name": "Verkäufe",
"_isLeaf": false,
"_children": [
{
"path": "{$issue_id}/1900641/1900678",
"name": "Verkäufe - 3-Zimmer",
"_isLeaf": true,
"_children": []
}
]
}
]
// release_date_combination_list.json
{
"": "",
"012MiSa": "+als_Mi_Sa_Doppelpack_mit_50%_Rabatt_f\u00fcr_die_Samstagstermine",
"012SaMi": "+als_Sa_Mi_Doppelpack_mit_50%_Rabatt_f\u00fcr_die_Mittwochstermine"
}
// release_dates.json
[
{
"id": "2012-06-26T22:00:00.000Z",
"deadline": "Tue, 26 Jun 2012 11:00:00 +0200",
"date": "Wed, 27 Jun 2012 00:00:00 +0200",
"_dependants": [
{
"id": "2012-06-29T22:00:00.000Z",
"deadline": "Thu, 28 Jun 2012 14:45:00 +0200",
"date": "Sat, 30 Jun 2012 00:00:00 +0200",
"_dependants": []
}
]
},
{
"id": "2012-07-03T22:00:00.000Z",
"deadline": "Tue, 03 Jul 2012 11:00:00 +0200",
"date": "Wed, 04 Jul 2012 00:00:00 +0200",
"_dependants": [
{
"id": "2012-07-06T22:00:00.000Z",
"deadline": "Thu, 05 Jul 2012 14:45:00 +0200",
"date": "Sat, 07 Jul 2012 00:00:00 +0200",
"_dependants": []
}
]
}
]
// templates.json
[
{
"id": "2_fl_ww_ZiWo_Chiffre_P",
"name": "2_fl_ww_ZiWo_Chiffre_P",
"path": "\/files\/config\/samples\/2_fl_ww_ZiWo_Chiffre_P",
"thumbnail": "http:\/\/apihost\/\/files\/adcounter\/templates\/0e2d6de00588d2ad48323702b1ca3b9a.jpg"
},
{
"id": "2_fl_ww_ZiWo_P",
"name": "2_fl_ww_ZiWo_P",
"path": "\/files\/config\/samples\/2_fl_ww_ZiWo_P",
"thumbnail": "http:\/\/apihost\/files\/adcounter\/templates\/24896f7d663cbb15152b16603e07ceb8.jpg"
},
]
// schema.json
{
"Ort": {
"label": "Ort",
"options": {
"Alfhausen": "Alfhausen",
"Ankum": "Ankum",
"Bad Essen": "Bad Essen",
"Bad Iburg": "Bad Iburg",
"Bad Laer": "Bad Laer",
"Bad Rothenfelde": "Bad Rothenfelde",
},
"type": "select"
},
"Haustyp": {
"label": "Haustyp",
"options": {
"EFH": "EFH",
"ZFH": "ZFH",
"MFH": "MFH",
"DHH": "DHH",
},
"type": "select"
},
"Wohnflaeche": {
"label": "Wohnfläche in m\u00b2",
"type": "text",
"required": "required",
"pattern": "^(\\d){1,4}$",
"title": "Die Wohnfläche muss aus 1 bis 4 Ziffern bestehen."
},
"warm_kalt": {
"label": "Auswahlmöglichkeit von WM, KM, VS und VB",
"options": {
"": "",
"WM": "WM",
"KM": "KM",
"VS": "VS",
"VB": "VB"
},
"type": "select"
},
"Preis": {
"label": "Preis in Euro",
"type": "text",
"pattern": "^(\\d){0,7}$",
"title": "Der Preis darf nur aus ZIFFERN bestehen (max. 7)"
},
"Beschreibung": {
"label": "Beschreibung",
"rows": "7",
"type": "textarea"
},
"Ansprechpartner": {
"label": "Ansprechpartner",
"rows": "2",
"type": "textarea"
},
"Firma": {
"label": "Firma",
"rows": "2",
"type": "textarea"
},
"Adresse": {
"label": "Adresse",
"rows": "3",
"type": "textarea"
},
"Datei": {
"label": "Datei",
"type": "file",
"required": "required",
"pattern": "^.{1,}$",
"title": "Bitte EIGENES BILD oder Grafik laden"
}
}
// validate.json
// if all goes well
{}
// else
{
"Wohnflaeche": "Die Wohnfläche muss aus 1 bis 4 Ziffern bestehen."
}
// job_details.json
/**
* Make sure to check the response code for '412 Validation failed'
*/
{
"id": "JOB_13403581002.pom",
"preview_url": "http://apihost/files/adcounter/preview/4f23/d457/a766/91ee/992b/4edc/72e2/312f.png",
"validation_errors": {}, // same as validate.json response
"overflowing": false
}
// price.json
{
"net": 10.0,
"vat_amount": 1.9,
"vat_percent": 19,
"total": 11.9,
"currency": "EUR"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment