public function testProcess()
 {
     // Test data *only* uploads.
     $params = array('data' => array('value' => 'foo'));
     $val = Google_MediaFileUpload::process(null, $params);
     $this->assertTrue(array_key_exists('postBody', $val));
     $this->assertEquals('foo', $val['postBody']);
     // Test only metadata.
     $params = array();
     $val = Google_MediaFileUpload::process(null, $params);
     $this->assertEquals(false, $val);
     // Test multipart (metadata & upload data).
     $params = array('data' => array('value' => 'foo'), 'boundary' => array('value' => 'a'));
     $val = Google_MediaFileUpload::process(array('a'), $params);
     $this->assertEquals('multipart/related; boundary=a', $val['content-type']);
     $expected = '--aContent-Type: application/json; charset=UTF-8' . '["a"]' . '--aContent-Type: Content-Transfer-Encoding: base64Zm9v--a--';
     $this->assertEquals($expected, str_replace("\r\n", '', $val['postBody']));
     // Multipart
     $params = array('data' => array('value' => 'foo'), 'boundary' => array('value' => 'a""'), 'mimeType' => array('value' => 'image/png'));
     $val = Google_MediaFileUpload::process(array('a'), $params);
     $this->assertEquals('multipart/related; boundary=a', $val['content-type']);
     $expected = '--aContent-Type: application/json; charset=UTF-8' . '["a"]' . '--aContent-Type: image/pngContent-Transfer-Encoding: base64Zm9v--a--';
     $this->assertEquals($expected, str_replace("\r\n", '', $val['postBody']));
 }
Пример #2
0
 /**
  * @param $name
  * @param $arguments
  * @return Google_HttpRequest|array
  * @throws Google_Exception
  */
 public function __call($name, $arguments)
 {
     if (!isset($this->methods[$name])) {
         throw new Google_Exception("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 Google_Exception("({$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 Google_Exception("({$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'];
     }
     $servicePath = $this->service->servicePath;
     // Process Media Request
     $contentType = false;
     if (isset($method['mediaUpload'])) {
         $media = Google_MediaFileUpload::process($postBody, $parameters);
         if ($media) {
             $contentType = isset($media['content-type']) ? $media['content-type'] : null;
             $postBody = isset($media['postBody']) ? $media['postBody'] : null;
             $servicePath = $method['mediaUpload']['protocols']['simple']['path'];
             $method['path'] = '';
         }
     }
     $url = Google_REST::createRequestUri($servicePath, $method['path'], $parameters);
     $httpRequest = new Google_HttpRequest($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'] = Google_Utils::getStrLen($postBody);
         }
         $httpRequest->setRequestHeaders($contentTypeHeader);
     }
     $httpRequest = Google_Client::$auth->sign($httpRequest);
     if (Google_Client::$useBatch) {
         return $httpRequest;
     }
     // Terminate immediatly if this is a resumable request.
     if (isset($parameters['uploadType']['value']) && 'resumable' == $parameters['uploadType']['value']) {
         return $httpRequest;
     }
     return Google_REST::execute($httpRequest);
 }