forked from moxiworks-platform/moxiworks-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResource.php
91 lines (73 loc) · 2.89 KB
/
Resource.php
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
<?php
namespace MoxiworksPlatform;
use MoxiworksPlatform\Exception\RemoteRequestFailureException;
class Resource {
public static function headers() {
$headers = array (
'Authorization' => static::authHeader(),
'Accept' => static::acceptHeader(),
'Content-Type' => static::contentTypeHeader(),
'User-Agent' => static::userAgentHeader()
);
if(isset(Session::$cookie)) {
$headers['Cookie'] = Session::$cookie;
}
return $headers;
}
public static function authHeader() {
if (!Credentials::ready())
throw new Exception\AuthorizationException('MoxiworksPlatform\Credentials must be set before using');
$identifier = Credentials::identifier();
$secret = Credentials::secret();
$auth_string = base64_encode("$identifier:$secret");
return "Basic $auth_string";
}
public static function acceptHeader() {
return 'application/vnd.moxi-platform+json;version=1';
}
public static function contentTypeHeader() {
return 'application/x-www-form-urlencoded';
}
public static function userAgentHeader() {
return 'moxiworks_platform php client';
}
public static function apiConnection($method, $url, $attributes) {
$client = new \GuzzleHttp\Client();
$json = null;
$type = ($method == 'GET') ? 'query' : 'form_params';
$query = [
$type => $attributes,
'headers' => Resource::headers(),
'debug' => Config::getDebug()
];
$res = $client->request($method, $url, $query);
$body = $res->getBody();
if(!isset(Session::$cookie)) {
Session::$cookie = $res->getHeader('set-cookie');
}
try {
$json = json_decode($body, true);
} catch (\Exception $e) {
throw new RemoteRequestFailureException("unable to parse remote response $e\n response:\n $body");
}
Resource::checkForErrorInResponse($json);
return $json;
}
public static function checkForErrorInResponse($json) {
$message = (is_array($json) && key_exists('messages', $json) && is_array($json['messages'])) ?
implode(',', $json['messages']) :
"unable to perform remote action on Moxi Works platform\n";
if (!is_array($json) || (key_exists('status', $json) && ($json['status'] == 'fail' || $json['status'] == 'error' ))) {
throw new RemoteRequestFailureException($message);
}
return true;
}
public static function underscore($attr) {
$out = preg_replace( '/::/', '/', $attr);
$out = preg_replace('/([A-Z]+)([A-Z][a-z])/','\1_\2', $out);
$out = preg_replace('/([a-z\d])([A-Z])/','\1_\2', $out);
$out = str_replace( "-", "_", $out);
$out = ltrim(strtolower($out), '_');
return $out;
}
}