/** * 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; }
public function testStrLen() { $this->assertEquals(0, apiUtils::getStrLen(null)); $this->assertEquals(0, apiUtils::getStrLen(false)); $this->assertEquals(0, apiUtils::getStrLen("")); $this->assertEquals(1, apiUtils::getStrLen(" ")); $this->assertEquals(2, apiUtils::getStrLen(" 1")); $this->assertEquals(7, apiUtils::getStrLen("0a\\n\n\r\n")); }
/** * 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; }
private function getResumeUri(apiHttpRequest $httpRequest) { $result = null; $body = $httpRequest->getPostBody(); if ($body) { $httpRequest->setRequestHeaders(array('content-type' => 'application/json; charset=UTF-8', 'content-length' => apiUtils::getStrLen($body), 'x-upload-content-type' => $this->mimeType, 'expect' => '')); } $response = apiClient::$io->makeRequest($httpRequest); $location = $response->getResponseHeader('location'); $code = $response->getResponseHttpCode(); if (200 == $code && true == $location) { return $location; } throw new apiException("Failed to start the resumable upload"); }
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"); }
/** * @param $name * @param $arguments * @return apiHttpRequest|array * @throws apiException */ public function __call($name, $arguments) { 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'] = ''; } } $url = apiREST::createRequestUri($restBasePath, $method['path'], $parameters); $httpRequest = new apiHttpRequest($url, $method['httpMethod'], null, $postBody); if ($postBody) { $contentTypeHeader = array(); if (isset($contentType) && $contentType) { $contentTypeHeader['content-type'] = $contentType; } else { $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; $contentTypeHeader['content-length'] = apiUtils::getStrLen($postBody); } $httpRequest->setRequestHeaders($contentTypeHeader); } $httpRequest = apiClient::$auth->sign($httpRequest); if (apiClient::$useBatch) { return $httpRequest; } // Terminate immediatly if this is a resumable request. if (isset($parameters['uploadType']['value']) && 'resumable' == $parameters['uploadType']['value']) { return $httpRequest; } return apiREST::execute($httpRequest); }