PluginProbe
InfiniteWP Client / trunk
InfiniteWP Client vtrunk
1.13.10 1.13.7 trunk 0.1.4 0.1.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.11.0 1.11.1 1.12.1 1.12.3 All 92 releases
iwp-client / lib / Google / Http / REST.php

REST.php in InfiniteWP Client trunk, at lib/Google/Http/REST.php

140 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2010 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Client.php';
19 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Http/Request.php';
20 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Service/Exception.php';
21 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Utils/URITemplate.php';
22
23 /**
24 * This class implements the RESTful transport of apiServiceRequest()'s
25 *
26 * @author Chris Chabot <chabotc@google.com>
27 * @author Chirag Shah <chirags@google.com>
28 */
29 class IWP_google_Http_REST
30 {
31 /**
32 * Executes a IWP_google_Http_Request
33 *
34 * @param IWP_google_Client $client
35 * @param IWP_google_Http_Request $req
36 * @return array decoded result
37 * @throws IWP_google_Service_Exception on server side error (ie: not authenticated,
38 * invalid or malformed post body, invalid url)
39 */
40 public static function execute(IWP_google_Client $client, IWP_google_Http_Request $req)
41 {
42 $httpRequest = $client->getIo()->makeRequest($req);
43 $httpRequest->setExpectedClass($req->getExpectedClass());
44 return self::decodeHttpResponse($httpRequest);
45 }
46
47 /**
48 * Decode an HTTP Response.
49 * @static
50 * @throws IWP_google_Service_Exception
51 * @param IWP_google_Http_Request $response The http response to be decoded.
52 * @return mixed|null
53 */
54 public static function decodeHttpResponse($response)
55 {
56 $code = $response->getResponseHttpCode();
57 $body = $response->getResponseBody();
58 $decoded = null;
59
60 if ((intVal($code)) >= 300) {
61 $decoded = json_decode($body, true);
62 $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
63 if (isset($decoded['error']) &&
64 isset($decoded['error']['message']) &&
65 isset($decoded['error']['code'])) {
66 // if we're getting a json encoded error definition, use that instead of the raw response
67 // body for improved readability
68 $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
69 } else {
70 $err .= ": ($code) $body";
71 }
72
73 $errors = null;
74 // Specific check for APIs which don't return error details, such as Blogger.
75 if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
76 $errors = $decoded['error']['errors'];
77 }
78
79 throw new IWP_google_Service_Exception($err, $code, null, $errors);
80 }
81
82 // Only attempt to decode the response, if the response code wasn't (204) 'no content'
83 if ($code != '204') {
84 $decoded = json_decode($body, true);
85 if ($decoded === null || $decoded === "") {
86 throw new IWP_google_Service_Exception("Invalid json in service response: $body");
87 }
88
89 if ($response->getExpectedClass()) {
90 $class = $response->getExpectedClass();
91 $decoded = new $class($decoded);
92 }
93 }
94 return $decoded;
95 }
96
97 /**
98 * Parse/expand request parameters and create a fully qualified
99 * request uri.
100 * @static
101 * @param string $servicePath
102 * @param string $restPath
103 * @param array $params
104 * @return string $requestUrl
105 */
106 public static function createRequestUri($servicePath, $restPath, $params)
107 {
108 $requestUrl = $servicePath . $restPath;
109 $uriTemplateVars = array();
110 $queryVars = array();
111 foreach ($params as $paramName => $paramSpec) {
112 if ($paramSpec['type'] == 'boolean') {
113 $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false';
114 }
115 if ($paramSpec['location'] == 'path') {
116 $uriTemplateVars[$paramName] = $paramSpec['value'];
117 } else if ($paramSpec['location'] == 'query') {
118 if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) {
119 foreach ($paramSpec['value'] as $value) {
120 $queryVars[] = $paramName . '=' . rawurlencode($value);
121 }
122 } else {
123 $queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']);
124 }
125 }
126 }
127
128 if (count($uriTemplateVars)) {
129 $uriTemplateParser = new IWP_google_Utils_URITemplate();
130 $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars);
131 }
132
133 if (count($queryVars)) {
134 $requestUrl .= '?' . implode('&', $queryVars);
135 }
136
137 return $requestUrl;
138 }
139 }
140