/src/plugins/orangehrmOpenidAuthenticationPlugin/lib/vendor/GoogleAPIClient/Http/REST.php

https://github.com/orangehrm/OrangeHRM · PHP · 134 lines · 73 code · 11 blank · 50 comment · 22 complexity · fadd43507a0f79b588d6f05158812bdb 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_Http_REST
  24. {
  25. /**
  26. * Executes a Google_Http_Request
  27. *
  28. * @param Google_Client $client
  29. * @param Google_Http_Request $req
  30. * @return array decoded result
  31. * @throws Google_Service_Exception on server side error (ie: not authenticated,
  32. * invalid or malformed post body, invalid url)
  33. */
  34. public static function execute(Google_Client $client, Google_Http_Request $req)
  35. {
  36. $httpRequest = $client->getIo()->makeRequest($req);
  37. $httpRequest->setExpectedClass($req->getExpectedClass());
  38. return self::decodeHttpResponse($httpRequest);
  39. }
  40. /**
  41. * Decode an HTTP Response.
  42. * @static
  43. * @throws Google_Service_Exception
  44. * @param Google_Http_Request $response The http response to be decoded.
  45. * @return mixed|null
  46. */
  47. public static function decodeHttpResponse($response)
  48. {
  49. $code = $response->getResponseHttpCode();
  50. $body = $response->getResponseBody();
  51. $decoded = null;
  52. if ((intVal($code)) >= 300) {
  53. $decoded = json_decode($body, true);
  54. $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
  55. if (isset($decoded['error']) &&
  56. isset($decoded['error']['message']) &&
  57. isset($decoded['error']['code'])) {
  58. // if we're getting a json encoded error definition, use that instead of the raw response
  59. // body for improved readability
  60. $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
  61. } else {
  62. $err .= ": ($code) $body";
  63. }
  64. $errors = null;
  65. // Specific check for APIs which don't return error details, such as Blogger.
  66. if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
  67. $errors = $decoded['error']['errors'];
  68. }
  69. throw new Google_Service_Exception($err, $code, null, $errors);
  70. }
  71. // Only attempt to decode the response, if the response code wasn't (204) 'no content'
  72. if ($code != '204') {
  73. $decoded = json_decode($body, true);
  74. if ($decoded === null || $decoded === "") {
  75. throw new Google_Service_Exception("Invalid json in service response: $body");
  76. }
  77. if ($response->getExpectedClass()) {
  78. $class = $response->getExpectedClass();
  79. $decoded = new $class($decoded);
  80. }
  81. }
  82. return $decoded;
  83. }
  84. /**
  85. * Parse/expand request parameters and create a fully qualified
  86. * request uri.
  87. * @static
  88. * @param string $servicePath
  89. * @param string $restPath
  90. * @param array $params
  91. * @return string $requestUrl
  92. */
  93. public static function createRequestUri($servicePath, $restPath, $params)
  94. {
  95. $requestUrl = $servicePath . $restPath;
  96. $uriTemplateVars = array();
  97. $queryVars = array();
  98. foreach ($params as $paramName => $paramSpec) {
  99. if ($paramSpec['type'] == 'boolean') {
  100. $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false';
  101. }
  102. if ($paramSpec['location'] == 'path') {
  103. $uriTemplateVars[$paramName] = $paramSpec['value'];
  104. } else if ($paramSpec['location'] == 'query') {
  105. if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) {
  106. foreach ($paramSpec['value'] as $value) {
  107. $queryVars[] = $paramName . '=' . rawurlencode($value);
  108. }
  109. } else {
  110. $queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']);
  111. }
  112. }
  113. }
  114. if (count($uriTemplateVars)) {
  115. $uriTemplateParser = new Google_Utils_URITemplate();
  116. $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars);
  117. }
  118. if (count($queryVars)) {
  119. $requestUrl .= '?' . implode($queryVars, '&');
  120. }
  121. return $requestUrl;
  122. }
  123. }