| 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 |
/** |
| 19 |
* This class implements the experimental JSON-RPC transport for executing apiServiceRequest' |
| 20 |
* |
| 21 |
* @author Chris Chabot <chabotc@google.com> |
| 22 |
*/ |
| 23 |
class apiRPC { |
| 24 |
static public function execute($requests) { |
| 25 |
$jsonRpcRequest = array(); |
| 26 |
foreach ($requests as $request) { |
| 27 |
$parameters = array(); |
| 28 |
foreach ($request->getParameters() as $parameterName => $parameterVal) { |
| 29 |
$parameters[$parameterName] = $parameterVal['value']; |
| 30 |
} |
| 31 |
$jsonRpcRequest[] = array( |
| 32 |
'id' => $request->getBatchKey(), |
| 33 |
'method' => $request->getRpcName(), |
| 34 |
'params' => $parameters, |
| 35 |
'apiVersion' => 'v1' |
| 36 |
); |
| 37 |
} |
| 38 |
$httpRequest = new apiHttpRequest($request->getRpcPath()); |
| 39 |
$httpRequest->setHeaders(array('Content-Type: application/json')); |
| 40 |
$httpRequest->setMethod('POST'); |
| 41 |
$httpRequest->setPostBody(json_encode($jsonRpcRequest)); |
| 42 |
$httpRequest = apiClient::$io->authenticatedRequest($httpRequest); |
| 43 |
if (($decodedResponse = json_decode($httpRequest->getResponseBody(), true)) != false) { |
| 44 |
$ret = array(); |
| 45 |
foreach ($decodedResponse as $response) { |
| 46 |
$ret[$response['id']] = self::checkNextLink($response['result']); |
| 47 |
} |
| 48 |
return $ret; |
| 49 |
} else { |
| 50 |
throw new apiServiceException("Invalid json returned by the json-rpc end-point"); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
static private function checkNextLink($response) { |
| 55 |
if (isset($response['links']) && isset($response['links']['next'][0]['href'])) { |
| 56 |
parse_str($response['links']['next'][0]['href'], $params); |
| 57 |
if (isset($params['c'])) { |
| 58 |
$response['continuationToken'] = $params['c']; |
| 59 |
} |
| 60 |
} |
| 61 |
return $response; |
| 62 |
} |
| 63 |
} |