PluginProbe
Authorizer / 2.2
Authorizer v2.2
3.15.3 3.15.2 3.15.1 3.15.0 3.14.3 3.14.4 3.14.2 3.14.1 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.9.0 2.9.1 2.9.10 2.9.11 2.9.12 2.9.13 2.9.2 2.9.3 2.9.6 All 126 releases
authorizer / inc / google-api-php-client / src / Google / Http / REST.php

REST.php in Authorizer 2.2, at inc/google-api-php-client/src/Google/Http/REST.php

140 lines 4.5 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 'Google/Client.php';
19 require_once 'Google/Http/Request.php';
20 require_once 'Google/Service/Exception.php';
21 require_once '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 Google_Http_REST
30 {
31 /**
32 * Executes a Google_Http_Request
33 *
34 * @param Google_Client $client
35 * @param Google_Http_Request $req
36 * @return array decoded result
37 * @throws Google_Service_Exception on server side error (ie: not authenticated,
38 * invalid or malformed post body, invalid url)
39 */
40 public static function execute(Google_Client $client, 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 Google_Service_Exception
51 * @param 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 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 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 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