PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/google/io/Google_REST.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 128 lines | 65 code | 11 blank | 52 comment | 26 complexity | e0b7abd2292576c5b087b8ddbc5c66b6 MD5 | raw file
  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. * This class implements the RESTful transport of apiServiceRequest()'s
  19. *
  20. * @author Chris Chabot <chabotc@google.com>
  21. * @author Chirag Shah <chirags@google.com>
  22. */
  23. class Google_REST {
  24. /**
  25. * Executes a apiServiceRequest using a RESTful call by transforming it into
  26. * an apiHttpRequest, and executed via apiIO::authenticatedRequest().
  27. *
  28. * @param Google_HttpRequest $req
  29. * @return array decoded result
  30. * @throws Google_ServiceException on server side error (ie: not authenticated,
  31. * invalid or malformed post body, invalid url)
  32. */
  33. static public function execute(Google_HttpRequest $req) {
  34. $httpRequest = Google_Client::$io->makeRequest($req);
  35. $decodedResponse = self::decodeHttpResponse($httpRequest);
  36. $ret = isset($decodedResponse['data'])
  37. ? $decodedResponse['data'] : $decodedResponse;
  38. return $ret;
  39. }
  40. /**
  41. * Decode an HTTP Response.
  42. * @static
  43. * @throws Google_ServiceException
  44. * @param Google_HttpRequest $response The http response to be decoded.
  45. * @return mixed|null
  46. */
  47. public static function decodeHttpResponse($response) {
  48. $code = $response->getResponseHttpCode();
  49. $body = $response->getResponseBody();
  50. $decoded = null;
  51. if ($code != '200' && $code != '201' && $code != '204') {
  52. $decoded = json_decode($body, true);
  53. $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
  54. if ($decoded != null && isset($decoded['error']['message']) && isset($decoded['error']['code'])) {
  55. // if we're getting a json encoded error definition, use that instead of the raw response
  56. // body for improved readability
  57. $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
  58. } else {
  59. $err .= ": ($code) $body";
  60. }
  61. throw new Google_ServiceException($err, $code, null, $decoded['error']['errors']);
  62. }
  63. // Only attempt to decode the response, if the response code wasn't (204) 'no content'
  64. if ($code != '204') {
  65. $decoded = json_decode($body, true);
  66. if ($decoded === null || $decoded === "") {
  67. throw new Google_ServiceException("Invalid json in service response: $body");
  68. }
  69. }
  70. return $decoded;
  71. }
  72. /**
  73. * Parse/expand request parameters and create a fully qualified
  74. * request uri.
  75. * @static
  76. * @param string $servicePath
  77. * @param string $restPath
  78. * @param array $params
  79. * @return string $requestUrl
  80. */
  81. static function createRequestUri($servicePath, $restPath, $params) {
  82. $requestUrl = $servicePath . $restPath;
  83. $uriTemplateVars = array();
  84. $queryVars = array();
  85. foreach ($params as $paramName => $paramSpec) {
  86. // Discovery v1.0 puts the canonical location under the 'location' field.
  87. if (! isset($paramSpec['location'])) {
  88. $paramSpec['location'] = $paramSpec['restParameterType'];
  89. }
  90. if ($paramSpec['type'] == 'boolean') {
  91. $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false';
  92. }
  93. if ($paramSpec['location'] == 'path') {
  94. $uriTemplateVars[$paramName] = $paramSpec['value'];
  95. } else {
  96. if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) {
  97. foreach ($paramSpec['value'] as $value) {
  98. $queryVars[] = $paramName . '=' . rawurlencode($value);
  99. }
  100. } else {
  101. $queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']);
  102. }
  103. }
  104. }
  105. if (count($uriTemplateVars)) {
  106. $uriTemplateParser = new URI_Template_Parser($requestUrl);
  107. $requestUrl = $uriTemplateParser->expand($uriTemplateVars);
  108. }
  109. //FIXME work around for the the uri template lib which url encodes
  110. // the @'s & confuses our servers.
  111. $requestUrl = str_replace('%40', '@', $requestUrl);
  112. if (count($queryVars)) {
  113. $requestUrl .= '?' . implode($queryVars, '&');
  114. }
  115. return $requestUrl;
  116. }
  117. }