[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 <[email protected]> 27 * @author Chirag Shah <[email protected]> 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |