/** * Executes a apiServiceRequest using a RESTful call by transforming it into a apiHttpRequest, * execute it via apiIO::authenticatedRequest() and returning the json decoded result * * @param apiServiceRequest $req * @return array decoded result * @throws apiServiceException on server side error (ie: not authenticated, invalid or * malformed post body, invalid url) */ public static function execute(apiServiceRequest $req) { $result = null; $postBody = $req->getPostBody(); $url = self::createRequestUri($req->getRestBasePath(), $req->getRestPath(), $req->getParameters()); //var_dump($url); $httpRequest = new apiHttpRequest($url, $req->getHttpMethod(), null, $postBody); // Add a content-type: application/json header so the server knows how to interpret the post body if ($postBody) { $contentTypeHeader = array('Content-Type: application/json; charset=UTF-8', 'Content-Length: ' . apiUtils::getStrLen($postBody)); if ($httpRequest->getHeaders()) { $contentTypeHeader = array_merge($httpRequest->getHeaders(), $contentTypeHeader); } $httpRequest->setHeaders($contentTypeHeader); } // TODO : ajout didier pour passage adresse IP client // $newHeader = array( // //'X-Forwarded-For: ' . $_SERVER['SERVER_ADDR'] // 'X-Forwarded-For: 88.176.177.141' // ); // if ($httpRequest->getHeaders()) { // $newHeader = array_merge($httpRequest->getHeaders(), $newHeader); // } // $httpRequest->setHeaders($newHeader); // // var_dump($httpRequest); $httpRequest = apiClient::$io->authenticatedRequest($httpRequest); $decodedResponse = self::decodeHttpResponse($httpRequest); //FIXME currently everything is wrapped in a data envelope, but hopefully this might change some day $ret = isset($decodedResponse['data']) ? $decodedResponse['data'] : $decodedResponse; return $ret; }
/** * Executes a apiServiceRequest using a RESTful call by transforming it into a apiHttpRequest, * execute it via apiIO::authenticatedRequest() and returning the json decoded result * * @param apiServiceRequest $req * @return array decoded result * @throws apiServiceException on server side error (ie: not authenticated, invalid or * malformed post body, invalid url) */ public static function execute(apiServiceRequest $req) { $result = null; $postBody = $req->getPostBody(); $url = self::createRequestUri($req->getRestBasePath(), $req->getRestPath(), $req->getParameters()); $httpRequest = new apiHttpRequest($url, $req->getHttpMethod(), null, $postBody); // Add a content-type: application/json header so the server knows how to interpret the post body if ($postBody) { $contentTypeHeader = array('Content-Type: application/json; charset=UTF-8', 'Content-Length: ' . apiUtils::getStrLen($postBody)); if ($httpRequest->getHeaders()) { $contentTypeHeader = array_merge($httpRequest->getHeaders(), $contentTypeHeader); } $httpRequest->setHeaders($contentTypeHeader); } $httpRequest = $req->getIo()->authenticatedRequest($httpRequest); $decodedResponse = self::decodeHttpResponse($httpRequest); //FIXME currently everything is wrapped in a data envelope, but hopefully this might change some day $ret = isset($decodedResponse['data']) ? $decodedResponse['data'] : $decodedResponse; return $ret; }
/** * Executes a apiServiceRequest using a RESTful call by transforming it into * an apiHttpRequest, and executed via apiIO::authenticatedRequest(). * * @param apiServiceRequest $req * @return array decoded result * @throws apiServiceException on server side error (ie: not authenticated, invalid or * malformed post body, invalid url) */ public static function execute(apiServiceRequest $req) { $result = null; $postBody = $req->getPostBody(); $url = self::createRequestUri($req->getRestBasePath(), $req->getRestPath(), $req->getParameters()); $httpRequest = new apiHttpRequest($url, $req->getHttpMethod(), null, $postBody); if ($postBody) { $contentTypeHeader = array(); if (isset($req->contentType) && $req->contentType) { $contentTypeHeader['content-type'] = $req->contentType; } else { $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; $contentTypeHeader['content-length'] = apiUtils::getStrLen($postBody); } $httpRequest->setRequestHeaders($contentTypeHeader); } $httpRequest = apiClient::$io->authenticatedRequest($httpRequest); $decodedResponse = self::decodeHttpResponse($httpRequest); //FIXME currently everything is wrapped in a data envelope, but hopefully this might change some day $ret = isset($decodedResponse['data']) ? $decodedResponse['data'] : $decodedResponse; return $ret; }
/** * @param $name * @param $arguments * @return apiServiceRequest|array * @throws apiException */ public function __call($name, $arguments) { if (count($arguments) != 1 && count($arguments) != 2) { throw new apiException("apiClient method calls expect 1 or 2 parameter (\$client->plus->activities->list(array('userId' => 'me'))"); } if (!is_array($arguments[0])) { throw new apiException("apiClient method parameter should be an array (\$client->plus->activities->list(array('userId' => 'me'))"); } $batchKey = false; if (isset($arguments[1])) { if (!is_string($arguments[1])) { throw new apiException("The batch key parameter should be a string (\$client->plus->activities->list( array('userId' => '@me'), 'batchKey'))"); } $batchKey = $arguments[1]; } if (!isset($this->methods[$name])) { throw new apiException("Unknown function: {$this->serviceName}->{$this->resourceName}->{$name}()"); } $method = $this->methods[$name]; $parameters = $arguments[0]; // postBody is a special case since it's not defined in the discovery document as parameter, but we abuse the param entry for storing it $postBody = null; if (isset($parameters['postBody'])) { if (is_object($parameters['postBody'])) { $this->stripNull($parameters['postBody']); } // Some APIs require the postBody to be set under the data key. if (is_array($parameters['postBody']) && 'buzz' == $this->serviceName) { if (!isset($parameters['postBody']['data'])) { $rawBody = $parameters['postBody']; unset($parameters['postBody']); $parameters['postBody']['data'] = $rawBody; } } $postBody = is_array($parameters['postBody']) || is_object($parameters['postBody']) ? json_encode($parameters['postBody']) : $parameters['postBody']; // remove from the parameter list so not to trip up the param entry checking & make sure it doesn't end up on the query unset($parameters['postBody']); if (isset($parameters['optParams'])) { $optParams = $parameters['optParams']; unset($parameters['optParams']); $parameters = array_merge($parameters, $optParams); } } if (!isset($method['parameters'])) { $method['parameters'] = array(); } $method['parameters'] = array_merge($method['parameters'], $this->stackParameters); foreach ($parameters as $key => $val) { if ($key != 'postBody' && !isset($method['parameters'][$key])) { throw new apiException("({$name}) unknown parameter: '{$key}'"); } } if (isset($method['parameters'])) { foreach ($method['parameters'] as $paramName => $paramSpec) { if (isset($paramSpec['required']) && $paramSpec['required'] && !isset($parameters[$paramName])) { throw new apiException("({$name}) missing required param: '{$paramName}'"); } if (isset($parameters[$paramName])) { $value = $parameters[$paramName]; $parameters[$paramName] = $paramSpec; $parameters[$paramName]['value'] = $value; unset($parameters[$paramName]['required']); } else { unset($parameters[$paramName]); } } } // Discovery v1.0 puts the canonical method id under the 'id' field. if (!isset($method['id'])) { $method['id'] = $method['rpcMethod']; } // Discovery v1.0 puts the canonical path under the 'path' field. if (!isset($method['path'])) { $method['path'] = $method['restPath']; } $request = new apiServiceRequest($this->service->restBasePath, $this->service->rpcPath, $method['path'], $method['id'], $method['httpMethod'], $parameters, $postBody); if ($batchKey) { $request->setBatchKey($batchKey); return $request; } else { return apiREST::execute($request); } }
/** * Executes a apiServiceRequest using a RESTful call by transforming it into a apiHttpRequest, execute it via apiIO::authenticatedRequest() * and returning the json decoded result * * @param apiServiceRequest $request * @return array decoded result * @throws apiServiceException on server side error (ie: not authenticated, invalid or malformed post body, invalid url, etc) */ public static function execute(apiServiceRequest $request) { global $apiTypeHandlers; $result = null; $requestUrl = $request->getRestBasePath() . $request->getRestPath(); $uriTemplateVars = array(); $queryVars = array(); foreach ($request->getParameters() as $paramName => $paramSpec) { // Discovery v1.0 puts the canonical location under the 'location' field. if (!isset($paramSpec['location'])) { $paramSpec['location'] = $paramSpec['restParameterType']; } if ($paramSpec['location'] == 'path') { $uriTemplateVars[$paramName] = $paramSpec['value']; } else { if ($paramSpec['type'] == 'boolean') { $paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false'; } if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { foreach ($paramSpec['value'] as $value) { $queryVars[] = $paramName . '=' . rawurlencode($value); } } else { $queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']); } } } $queryVars[] = 'alt=json'; if (count($uriTemplateVars)) { $uriTemplateParser = new URI_Template_Parser($requestUrl); $requestUrl = $uriTemplateParser->expand($uriTemplateVars); } //FIXME work around for the the uri template lib which url encodes the @'s & confuses our servers $requestUrl = str_replace('%40', '@', $requestUrl); //EOFIX //FIXME temp work around to make @groups/{@following,@followers} work (something which we should really be fixing in our API) if (strpos($requestUrl, '/@groups') && (strpos($requestUrl, '/@following') || strpos($requestUrl, '/@followers'))) { $requestUrl = str_replace('/@self', '', $requestUrl); } //EOFIX if (count($queryVars)) { $requestUrl .= '?' . implode($queryVars, '&'); } $httpRequest = new apiHttpRequest($requestUrl, $request->getHttpMethod(), null, $request->getPostBody()); // Add a content-type: application/json header so the server knows how to interpret the post body if ($request->getPostBody()) { $contentTypeHeader = array('Content-Type: application/json; charset=UTF-8', 'Content-Length: ' . self::getStrLen($request->getPostBody())); if ($httpRequest->getHeaders()) { $contentTypeHeader = array_merge($httpRequest->getHeaders(), $contentTypeHeader); } $httpRequest->setHeaders($contentTypeHeader); } $httpRequest = $request->getIo()->authenticatedRequest($httpRequest); if ($httpRequest->getResponseHttpCode() != '200' && $httpRequest->getResponseHttpCode() != '201' && $httpRequest->getResponseHttpCode() != '204') { $responseBody = $httpRequest->getResponseBody(); if (($responseBody = json_decode($responseBody, true)) != null && isset($responseBody['error']['message']) && isset($responseBody['error']['code'])) { // if we're getting a json encoded error definition, use that instead of the raw response body for improved readability $errorMessage = "Error calling " . $httpRequest->getUrl() . ": ({$responseBody['error']['code']}) {$responseBody['error']['message']}"; } else { $errorMessage = "Error calling " . $httpRequest->getMethod() . " " . $httpRequest->getUrl() . ": (" . $httpRequest->getResponseHttpCode() . ") " . $httpRequest->getResponseBody(); } throw new apiServiceException($errorMessage); } $decodedResponse = null; if ($httpRequest->getResponseHttpCode() != '204') { // Only attempt to decode the response, if the response code wasn't (204) 'no content' if (($decodedResponse = json_decode($httpRequest->getResponseBody(), true)) == null) { throw new apiServiceException("Invalid json in service response: " . $httpRequest->getResponseBody()); } } //FIXME currently everything is wrapped in a data envelope, but hopefully this might change some day $ret = isset($decodedResponse['data']) ? $decodedResponse['data'] : $decodedResponse; // Add a 'continuationToken' element to the response if the response contains a next link (so you can call it using the 'c' param) $ret = self::checkNextLink($ret); // if the response type has a registered type handler, call & return it instead of the raw response array if (isset($ret['kind']) && isset($apiTypeHandlers[$ret['kind']])) { $ret = new $apiTypeHandlers[$ret['kind']]($ret); } return $ret; }
public function __call($name, $arguments) { if (count($arguments) != 1 && count($arguments) != 2) { throw new apiException("apiClient method calls expect one or two parameter (for example: \$apiClient->buzz->activities->list( array('userId' => '@me')) or when executing a batch request: \$apiClient->buzz->activities->list( array('userId' => '@me'), 'batchKey')"); } if (!is_array($arguments[0])) { throw new apiException("apiClient method parameter should be an array (for example: \$apiClient->buzz->activities->list( array('userId' => '@me'))"); } $batchKey = false; if (isset($arguments[1])) { if (!is_string($arguments[1])) { throw new apiException("The batch key parameter should be a string, for example: \$apiClient->buzz->activities->list( array('userId' => '@me'), 'batchKey')"); } $batchKey = $arguments[1]; } if (!isset($this->methods[$name])) { throw new apiException("Unknown function: {$this->serviceName}->{$this->resourceName}->{$name}()"); } $method = $this->methods[$name]; $parameters = $arguments[0]; // postBody is a special case since it's not defined in the discovery document as parameter, but we abuse the param entry for storing it $postBody = null; if (isset($parameters['postBody'])) { if (is_object($parameters['postBody'])) { $this->stripNull($parameters['postBody']); } // Some APIs require the postBody to be set under the data key. if (is_array($parameters['postBody']) && 'buzz' == $this->serviceName) { if (!isset($parameters['postBody']['data'])) { $rawBody = $parameters['postBody']; unset($parameters['postBody']); $parameters['postBody']['data'] = $rawBody; } } $postBody = is_array($parameters['postBody']) || is_object($parameters['postBody']) ? json_encode($parameters['postBody']) : $parameters['postBody']; // remove from the parameter list so not to trip up the param entry checking & make sure it doesn't end up on the query unset($parameters['postBody']); if (isset($parameters['optParams'])) { $optParams = $parameters['optParams']; unset($parameters['optParams']); $parameters = array_merge($parameters, $optParams); } } if (!isset($method['parameters'])) { $method['parameters'] = array(); } $method['parameters'] = array_merge($method['parameters'], $this->stackParameters); foreach ($parameters as $key => $val) { if ($key != 'postBody' && !isset($method['parameters'][$key])) { throw new apiException("({$name}) unknown parameter: '{$key}'"); } } if (isset($method['parameters'])) { foreach ($method['parameters'] as $paramName => $paramSpec) { if (isset($paramSpec['required']) && $paramSpec['required'] && !isset($parameters[$paramName])) { throw new apiException("({$name}) missing required param: '{$paramName}'"); } if (isset($parameters[$paramName])) { $value = $parameters[$paramName]; // check to see if the param value matches the required pattern if (isset($parameters[$paramName]['pattern']) && !empty($parameters[$paramName]['pattern'])) { if (preg_match('|' . $parameters[$paramName]['pattern'] . '|', $value) == 0) { throw new apiException("({$name}) invalid parameter format for {$paramName}: {$value} doesn't match \"{$parameters[$paramName]['pattern']}\""); } } $parameters[$paramName] = $paramSpec; $parameters[$paramName]['value'] = $value; // remove all the bits that were already validated in this function & are no longer relevant within the execution chain unset($parameters[$paramName]['pattern']); unset($parameters[$paramName]['required']); } else { unset($parameters[$paramName]); } } } // Discovery v1.0 puts the canonical method id under the 'id' field. if (!isset($method['id'])) { $method['id'] = $method['rpcMethod']; } // Discovery v1.0 puts the canonical path under the 'path' field. if (!isset($method['path'])) { $method['path'] = $method['restPath']; } $request = new apiServiceRequest($this->service->getIo(), $this->service->getRestBasePath(), $this->service->getRpcPath(), $method['path'], $method['id'], $method['httpMethod'], $parameters, $postBody); if ($batchKey) { $request->setBatchKey($batchKey); return $request; } else { return apiREST::execute($request); } }
/** * @param $name * @param $arguments * @return apiServiceRequest|array * @throws apiException */ public function __call($name, $arguments) { if (count($arguments) != 1 && count($arguments) != 2) { throw new apiException("client method calls expect 1 or 2 parameter (\$client->plus->activities->list(array('userId' => 'me'))"); } if (!is_array($arguments[0])) { throw new apiException("client method parameter should be an array (\$client->plus->activities->list(array('userId' => 'me'))"); } $batchKey = false; if (isset($arguments[1])) { if (!is_string($arguments[1])) { throw new apiException("The batch key parameter should be a string (\$client->plus->activities->list( array('userId' => 'me'), 'batchKey'))"); } $batchKey = $arguments[1]; } if (!isset($this->methods[$name])) { throw new apiException("Unknown function: {$this->serviceName}->{$this->resourceName}->{$name}()"); } $method = $this->methods[$name]; $parameters = $arguments[0]; // postBody is a special case since it's not defined in the discovery document as parameter, but we abuse the param entry for storing it $postBody = null; if (isset($parameters['postBody'])) { if (is_object($parameters['postBody'])) { $this->stripNull($parameters['postBody']); } // Some APIs require the postBody to be set under the data key. if (is_array($parameters['postBody']) && 'latitude' == $this->serviceName) { if (!isset($parameters['postBody']['data'])) { $rawBody = $parameters['postBody']; unset($parameters['postBody']); $parameters['postBody']['data'] = $rawBody; } } $postBody = is_array($parameters['postBody']) || is_object($parameters['postBody']) ? json_encode($parameters['postBody']) : $parameters['postBody']; unset($parameters['postBody']); if (isset($parameters['optParams'])) { $optParams = $parameters['optParams']; unset($parameters['optParams']); $parameters = array_merge($parameters, $optParams); } } if (!isset($method['parameters'])) { $method['parameters'] = array(); } $method['parameters'] = array_merge($method['parameters'], $this->stackParameters); foreach ($parameters as $key => $val) { if ($key != 'postBody' && !isset($method['parameters'][$key])) { throw new apiException("({$name}) unknown parameter: '{$key}'"); } } if (isset($method['parameters'])) { foreach ($method['parameters'] as $paramName => $paramSpec) { if (isset($paramSpec['required']) && $paramSpec['required'] && !isset($parameters[$paramName])) { throw new apiException("({$name}) missing required param: '{$paramName}'"); } if (isset($parameters[$paramName])) { $value = $parameters[$paramName]; $parameters[$paramName] = $paramSpec; $parameters[$paramName]['value'] = $value; unset($parameters[$paramName]['required']); } else { unset($parameters[$paramName]); } } } // Discovery v1.0 puts the canonical method id under the 'id' field. if (!isset($method['id'])) { $method['id'] = $method['rpcMethod']; } // Discovery v1.0 puts the canonical path under the 'path' field. if (!isset($method['path'])) { $method['path'] = $method['restPath']; } $restBasePath = $this->service->restBasePath; // Process Media Request $contentType = false; if (isset($method['mediaUpload'])) { $media = apiMediaFileUpload::process($postBody, $parameters); if ($media) { $contentType = isset($media['content-type']) ? $media['content-type'] : null; $postBody = isset($media['postBody']) ? $media['postBody'] : null; $restBasePath = $method['mediaUpload']['protocols']['simple']['path']; $method['path'] = ''; } } $request = new apiServiceRequest($restBasePath, $this->service->rpcPath, $method['path'], $method['id'], $method['httpMethod'], $parameters, $postBody); $request->setContentType($contentType); // Terminate immediatly if this is a resumable request. if (isset($parameters['uploadType']['value']) && 'resumable' == $parameters['uploadType']['value']) { return $request; } if ($batchKey) { $request->setBatchKey($batchKey); return $request; } else { return apiREST::execute($request); } }
private function getResumeUri(apiServiceRequest $req) { $result = null; $postBody = $req->getPostBody(); $url = apiREST::createRequestUri($req->getRestBasePath(), $req->getRestPath(), $req->getParameters()); $httpRequest = new apiHttpRequest($url, $req->getHttpMethod(), null, $postBody); if ($postBody) { $httpRequest->setRequestHeaders(array('content-type' => 'application/json; charset=UTF-8', 'content-length' => apiUtils::getStrLen($postBody), 'x-upload-content-type' => $this->mimeType, 'expect' => '')); } $response = apiClient::$io->authenticatedRequest($httpRequest); $location = $response->getResponseHeader('location'); $code = $response->getResponseHttpCode(); if (200 == $code && true == $location) { return $location; } throw new apiException("Failed to start the resumable upload"); }
public function __call($name, $arguments) { if (count($arguments) != 1 && count($arguments) != 2) { throw new apiException("apiClient method calls expect one or two parameter (for example: \$apiClient->buzz->activities->list( array('userId' => '@me')) or when executing a batch request: \$apiClient->buzz->activities->list( array('userId' => '@me'), 'batchKey')"); } if (!is_array($arguments[0])) { throw new apiException("apiClient method parameter should me an array (for example: \$apiClient->buzz->activities->list( array('userId' => '@me'))"); } $batchKey = false; if (isset($arguments[1])) { if (!is_string($arguments[1])) { throw new apiException("The batch key parameter should be a string, for example: \$apiClient->buzz->activities->list( array('userId' => '@me'), 'batchKey')"); } $batchKey = $arguments[1]; } if (!isset($this->methods[$name])) { throw new apiException("Unknown function: {$this->serviceName}->{$this->resourceName}->{$name}()"); } $method = $this->methods[$name]; $parameters = $arguments[0]; // postBody is a special case since it's not defined in the discovery document as parameter, but we abuse the param entry for storing it $postBody = null; if (isset($parameters['postBody'])) { // If the postBody is not a raw json string, json encode it for transport $postBody = is_array($parameters['postBody']) || is_object($parameters['postBody']) ? json_encode($parameters['postBody']) : $parameters['postBody']; // remove from the parameter list so not to trip up the param entry checking & make sure it doesn't end up on the query unset($parameters['postBody']); //FIXME Should really add the magic array('data' => ..real stuff ..) wrapper around the request as to not bother developers with it } foreach ($parameters as $key => $val) { if ($key != 'postBody' && !isset($method['parameters'][$key])) { throw new apiException("({$name}) unknown parameter: '{$key}'"); } } if (isset($method['parameters'])) { foreach ($method['parameters'] as $paramName => $paramSpec) { if ($paramSpec['required'] && !isset($parameters[$paramName])) { throw new apiException("({$name}) missing required param: '{$paramName}'"); } if (isset($parameters[$paramName])) { $value = $parameters[$paramName]; // check to see if the param value matches the required pattern if (isset($parameters[$paramName]['pattern']) && !empty($parameters[$paramName]['pattern'])) { if (preg_match('|' . $parameters[$paramName]['pattern'] . '|', $value) == 0) { throw new apiException("({$name}) invalid parameter format for {$paramName}: {$value} doesn't match \"{$parameters[$paramName]['pattern']}\""); } } $parameters[$paramName] = $paramSpec; $parameters[$paramName]['value'] = $value; // remove all the bits that were already validated in this function & are no longer relevant within the execution chain unset($parameters[$paramName]['pattern']); unset($parameters[$paramName]['required']); } else { unset($parameters[$paramName]); } } } $request = new apiServiceRequest($this->service->getIo(), $this->service->getBaseUrl(), $method['pathUrl'], $method['rpcName'], $method['httpMethod'], $parameters, $postBody); if ($batchKey) { $request->setBatchKey($batchKey); return $request; } else { return apiREST::execute($request); } }