forked from quaderno/quaderno-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquaderno_base.php
More file actions
90 lines (72 loc) · 2.9 KB
/
quaderno_base.php
File metadata and controls
90 lines (72 loc) · 2.9 KB
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
<?php
/* General interface that implements the calls
to the message coding and transport library */
abstract class QuadernoBase {
const DEBUG_URL = 'http://localhost:3000/';
const PRODUCTION_URL = "https://quadernoapp.com/";
protected static $API_KEY = null;
protected static $ACCOUNT_ID = null;
protected static $URL = null;
static function init($key, $account_id, $debug=false) {
self::$API_KEY = $key;
self::$ACCOUNT_ID = $account_id;
self::$URL = $debug ? self::DEBUG_URL : self::PRODUCTION_URL;
}
static function authorization($key, $debug=false){
$url = $debug ? self::DEBUG_URL : self::PRODUCTION_URL;
$url = $url . 'subdomain' . '/api/v1/authorization.json';
$response = QuadernoJSON::exec($url, "GET", $key, "foo", null);
return $response['data'];
}
static function ping() {
$url = self::$URL . self::$ACCOUNT_ID . '/api/v1/ping.json';
$response = QuadernoJSON::exec($url, "GET", self::$API_KEY, "foo", null);
return self::responseIsValid($response);
}
static function delete($model, $id) {
$url = self::$URL . self::$ACCOUNT_ID . "/api/v1/" . $model . "/" . $id . ".json";
return QuadernoJSON::exec($url, "DELETE", self::$API_KEY, "foo", null);
}
static function deleteNested($parentmodel, $parentid, $model, $id) {
return self::delete($parentmodel . "/" . $parentid . "/" . $model, $id);
}
static function deliver($model, $id) {
$url = self::$URL . self::$ACCOUNT_ID . "/api/v1/" . $model . "/" . $id . "/deliver.json";
return QuadernoJSON::exec($url, "GET", self::$API_KEY, "foo", null);
}
static function find($model, $params=null) {
$url = self::$URL . self::$ACCOUNT_ID . "/api/v1/" . $model . ".json";
if (isset($params)) {
$encodeQuery = '';
foreach ($params as $key => $value) {
$encodeQuery.= urlencode($key) . '=' . urlencode($value) . '&';
}
$url .= "?" . $encodeQuery;
}
return QuadernoJSON::exec($url, "GET", self::$API_KEY, "foo", null);
}
static function findByID($model, $id) {
$url = self::$URL . self::$ACCOUNT_ID . "/api/v1/" . $model . "/" . $id . ".json";
return QuadernoJSON::exec($url, "GET", self::$API_KEY, "foo", null);
}
static function save($model, $data, $id) {
$url = self::$URL . self::$ACCOUNT_ID . "/api/v1/" . $model;
if ($id) {
$url .= "/" . $id . ".json";
$return = QuadernoJSON::exec($url, "PUT", self::$API_KEY, "foo", $data);
} else {
$url .= ".json";
$return = QuadernoJSON::exec($url, "POST", self::$API_KEY, "foo", $data);
}
return $return;
}
static function saveNested($parentmodel, $parentid, $model, $data) {
return self::save($parentmodel . "/" . $parentid . "/" . $model, $data, null);
}
static function responseIsValid($response) {
return isset($response) &&
!$response['error'] &&
intval($response['http_code']/100) == 2;
}
}
?>