| 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 "external/URITemplateParser.php"; |
| 19 |
require_once "service/apiUtils.php"; |
| 20 |
|
| 21 |
/** |
| 22 |
* This class implements the RESTful transport of apiServiceRequest()'s |
| 23 |
* |
| 24 |
* @author Chris Chabot <chabotc@google.com> |
| 25 |
* @author Chirag Shah <chirags@google.com> |
| 26 |
*/ |
| 27 |
class apiREST { |
| 28 |
/** |
| 29 |
* Executes a apiServiceRequest using a RESTful call by transforming it into a apiHttpRequest, |
| 30 |
* execute it via apiIO::authenticatedRequest() and returning the json decoded result |
| 31 |
* |
| 32 |
* @param apiServiceRequest $req |
| 33 |
* @return array decoded result |
| 34 |
* @throws apiServiceException on server side error (ie: not authenticated, invalid or |
| 35 |
* malformed post body, invalid url) |
| 36 |
*/ |
| 37 |
static public function execute(apiServiceRequest $req) { |
| 38 |
$result = null; |
| 39 |
$postBody = $req->getPostBody(); |
| 40 |
$url = self::createRequestUri($req->getRestBasePath(), $req->getRestPath(), $req->getParameters()); |
| 41 |
|
| 42 |
$httpRequest = new apiHttpRequest($url, $req->getHttpMethod(), null, $postBody); |
| 43 |
// Add a content-type: application/json header so the server knows how to interpret the post body |
| 44 |
if ($postBody) { |
| 45 |
$contentTypeHeader = array( |
| 46 |
'Content-Type: application/json; charset=UTF-8', |
| 47 |
'Content-Length: ' . apiUtils::getStrLen($postBody) |
| 48 |
); |
| 49 |
if ($httpRequest->getHeaders()) { |
| 50 |
$contentTypeHeader = array_merge($httpRequest->getHeaders(), $contentTypeHeader); |
| 51 |
} |
| 52 |
$httpRequest->setHeaders($contentTypeHeader); |
| 53 |
} |
| 54 |
|
| 55 |
$httpRequest = apiClient::$io->authenticatedRequest($httpRequest); |
| 56 |
$decodedResponse = self::decodeHttpResponse($httpRequest); |
| 57 |
|
| 58 |
//FIXME currently everything is wrapped in a data envelope, but hopefully this might change some day |
| 59 |
$ret = isset($decodedResponse['data']) ? $decodedResponse['data'] : $decodedResponse; |
| 60 |
return $ret; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Decode an HTTP Response. |
| 66 |
* @static |
| 67 |
* @throws apiServiceException |
| 68 |
* @param apiHttpRequest $response The http response to be decoded. |
| 69 |
* @return mixed|null |
| 70 |
*/ |
| 71 |
static function decodeHttpResponse($response) { |
| 72 |
$code = $response->getResponseHttpCode(); |
| 73 |
$body = $response->getResponseBody(); |
| 74 |
$decoded = null; |
| 75 |
|
| 76 |
if ($code != '200' && $code != '201' && $code != '204') { |
| 77 |
$decoded = json_decode($body, true); |
| 78 |
$err = 'Error calling ' . $response->getMethod() . ' ' . $response->getUrl(); |
| 79 |
if ($decoded != null && isset($decoded['error']['message']) && isset($decoded['error']['code'])) { |
| 80 |
// if we're getting a json encoded error definition, use that instead of the raw response |
| 81 |
// body for improved readability |
| 82 |
$err .= ": ({$decoded['error']['code']}) " . $decoded['error']['message']; |
| 83 |
} else { |
| 84 |
$err .= ": ($code) $body"; |
| 85 |
} |
| 86 |
throw new apiServiceException($err); |
| 87 |
} |
| 88 |
|
| 89 |
// Only attempt to decode the response, if the response code wasn't (204) 'no content' |
| 90 |
if ($code != '204') { |
| 91 |
$decoded = json_decode($body, true); |
| 92 |
if ($decoded == null) { |
| 93 |
throw new apiServiceException("Invalid json in service response: $body"); |
| 94 |
} |
| 95 |
} |
| 96 |
return $decoded; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Parse/expand request parameters and create a fully qualified |
| 102 |
* request uri. |
| 103 |
* @static |
| 104 |
* @param string $basePath |
| 105 |
* @param string $restPath |
| 106 |
* @param array $params |
| 107 |
* @return string $requestUrl |
| 108 |
*/ |
| 109 |
static function createRequestUri($basePath, $restPath, $params) { |
| 110 |
$requestUrl = $basePath . $restPath; |
| 111 |
$uriTemplateVars = array(); |
| 112 |
$queryVars = array(); |
| 113 |
foreach ($params as $paramName => $paramSpec) { |
| 114 |
// Discovery v1.0 puts the canonical location under the 'location' field. |
| 115 |
if (! isset($paramSpec['location'])) { |
| 116 |
$paramSpec['location'] = $paramSpec['restParameterType']; |
| 117 |
} |
| 118 |
|
| 119 |
if ($paramSpec['type'] == 'boolean') { |
| 120 |
$paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; |
| 121 |
} |
| 122 |
if ($paramSpec['location'] == 'path') { |
| 123 |
$uriTemplateVars[$paramName] = $paramSpec['value']; |
| 124 |
} else { |
| 125 |
if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { |
| 126 |
foreach ($paramSpec['value'] as $value) { |
| 127 |
$queryVars[] = $paramName . '=' . rawurlencode($value); |
| 128 |
} |
| 129 |
} else { |
| 130 |
$queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
$queryVars[] = 'alt=json'; |
| 135 |
if (count($uriTemplateVars)) { |
| 136 |
$uriTemplateParser = new URI_Template_Parser($requestUrl); |
| 137 |
$requestUrl = $uriTemplateParser->expand($uriTemplateVars); |
| 138 |
} |
| 139 |
//FIXME work around for the the uri template lib which url encodes |
| 140 |
// the @'s & confuses our servers. |
| 141 |
$requestUrl = str_replace('%40', '@', $requestUrl); |
| 142 |
//EOFIX |
| 143 |
|
| 144 |
if (count($queryVars)) { |
| 145 |
$requestUrl .= '?' . implode($queryVars, '&'); |
| 146 |
} |
| 147 |
|
| 148 |
return $requestUrl; |
| 149 |
} |
| 150 |
} |
| 151 |
|