Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
25 - 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
/**
19
 * Implements the actual methods/resources of the discovered Google API using magic function
20
 * calling overloading (__call()), which on call will see if the method name (plus.activities.list)
21
 * is available in this service, and if so construct an apiHttpRequest representing it.
22
 *
23
 * @author Chris Chabot <chabotc@google.com>
24
 * @author Chirag Shah <chirags@google.com>
25
 *
26
 */
27
class Google_ServiceResource {
28
  // Valid query parameters that work, but don't appear in discovery.
29
  private $stackParameters = array(
30
      'alt' => array('type' => 'string', 'location' => 'query'),
31
      'boundary' => array('type' => 'string', 'location' => 'query'),
32
      'fields' => array('type' => 'string', 'location' => 'query'),
33
      'trace' => array('type' => 'string', 'location' => 'query'),
34
      'userIp' => array('type' => 'string', 'location' => 'query'),
35
      'userip' => array('type' => 'string', 'location' => 'query'),
36
      'file' => array('type' => 'complex', 'location' => 'body'),
37
      'data' => array('type' => 'string', 'location' => 'body'),
38
      'mimeType' => array('type' => 'string', 'location' => 'header'),
39
      'uploadType' => array('type' => 'string', 'location' => 'query'),
40
      'mediaUpload' => array('type' => 'complex', 'location' => 'query'),
41
  );
42
 
43
  /** @var Google_Service $service */
44
  private $service;
45
 
46
  /** @var string $serviceName */
47
  private $serviceName;
48
 
49
  /** @var string $resourceName */
50
  private $resourceName;
51
 
52
  /** @var array $methods */
53
  private $methods;
54
 
55
  public function __construct($service, $serviceName, $resourceName, $resource) {
56
    $this->service = $service;
57
    $this->serviceName = $serviceName;
58
    $this->resourceName = $resourceName;
59
    $this->methods = isset($resource['methods']) ? $resource['methods'] : array($resourceName => $resource);
60
  }
61
 
62
  /**
63
   * @param $name
64
   * @param $arguments
65
   * @return Google_HttpRequest|array
66
   * @throws Google_Exception
67
   */
68
  public function __call($name, $arguments) {
69
    if (! isset($this->methods[$name])) {
70
      throw new Google_Exception("Unknown function: {$this->serviceName}->{$this->resourceName}->{$name}()");
71
    }
72
    $method = $this->methods[$name];
73
    $parameters = $arguments[0];
74
 
75
    // 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
76
    $postBody = null;
77
    if (isset($parameters['postBody'])) {
78
      if (is_object($parameters['postBody'])) {
79
        $this->stripNull($parameters['postBody']);
80
      }
81
 
82
      // Some APIs require the postBody to be set under the data key.
83
      if (is_array($parameters['postBody']) && 'latitude' == $this->serviceName) {
84
        if (!isset($parameters['postBody']['data'])) {
85
          $rawBody = $parameters['postBody'];
86
          unset($parameters['postBody']);
87
          $parameters['postBody']['data'] = $rawBody;
88
        }
89
      }
90
 
91
      $postBody = is_array($parameters['postBody']) || is_object($parameters['postBody'])
92
          ? json_encode($parameters['postBody'])
93
          : $parameters['postBody'];
94
      unset($parameters['postBody']);
95
 
96
      if (isset($parameters['optParams'])) {
97
        $optParams = $parameters['optParams'];
98
        unset($parameters['optParams']);
99
        $parameters = array_merge($parameters, $optParams);
100
      }
101
    }
102
 
103
    if (!isset($method['parameters'])) {
104
      $method['parameters'] = array();
105
    }
106
 
107
    $method['parameters'] = array_merge($method['parameters'], $this->stackParameters);
108
    foreach ($parameters as $key => $val) {
109
      if ($key != 'postBody' && ! isset($method['parameters'][$key])) {
110
        throw new Google_Exception("($name) unknown parameter: '$key'");
111
      }
112
    }
113
    if (isset($method['parameters'])) {
114
      foreach ($method['parameters'] as $paramName => $paramSpec) {
115
        if (isset($paramSpec['required']) && $paramSpec['required'] && ! isset($parameters[$paramName])) {
116
          throw new Google_Exception("($name) missing required param: '$paramName'");
117
        }
118
        if (isset($parameters[$paramName])) {
119
          $value = $parameters[$paramName];
120
          $parameters[$paramName] = $paramSpec;
121
          $parameters[$paramName]['value'] = $value;
122
          unset($parameters[$paramName]['required']);
123
        } else {
124
          unset($parameters[$paramName]);
125
        }
126
      }
127
    }
128
 
129
    // Discovery v1.0 puts the canonical method id under the 'id' field.
130
    if (! isset($method['id'])) {
131
      $method['id'] = $method['rpcMethod'];
132
    }
133
 
134
    // Discovery v1.0 puts the canonical path under the 'path' field.
135
    if (! isset($method['path'])) {
136
      $method['path'] = $method['restPath'];
137
    }
138
 
139
    $servicePath = $this->service->servicePath;
140
 
141
    // Process Media Request
142
    $contentType = false;
143
    if (isset($method['mediaUpload'])) {
144
      $media = Google_MediaFileUpload::process($postBody, $parameters);
145
      if ($media) {
146
        $contentType = isset($media['content-type']) ? $media['content-type']: null;
147
        $postBody = isset($media['postBody']) ? $media['postBody'] : null;
148
        $servicePath = $method['mediaUpload']['protocols']['simple']['path'];
149
        $method['path'] = '';
150
      }
151
    }
152
 
153
    $url = Google_REST::createRequestUri($servicePath, $method['path'], $parameters);
154
    $httpRequest = new Google_HttpRequest($url, $method['httpMethod'], null, $postBody);
155
    if ($postBody) {
156
      $contentTypeHeader = array();
157
      if (isset($contentType) && $contentType) {
158
        $contentTypeHeader['content-type'] = $contentType;
159
      } else {
160
        $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8';
161
        $contentTypeHeader['content-length'] = Google_Utils::getStrLen($postBody);
162
      }
163
      $httpRequest->setRequestHeaders($contentTypeHeader);
164
    }
165
 
166
    $httpRequest = Google_Client::$auth->sign($httpRequest);
167
    if (Google_Client::$useBatch) {
168
      return $httpRequest;
169
    }
170
 
171
    // Terminate immediatly if this is a resumable request.
172
    if (isset($parameters['uploadType']['value'])
173
        && 'resumable' == $parameters['uploadType']['value']) {
174
      return $httpRequest;
175
    }
176
 
177
    return Google_REST::execute($httpRequest);
178
  }
179
 
180
  public  function useObjects() {
181
    global $apiConfig;
182
    return (isset($apiConfig['use_objects']) && $apiConfig['use_objects']);
183
  }
184
 
185
  protected function stripNull(&$o) {
186
    $o = (array) $o;
187
    foreach ($o as $k => $v) {
188
      if ($v === null || strstr($k, "\0*\0__")) {
189
        unset($o[$k]);
190
      }
191
      elseif (is_object($v) || is_array($v)) {
192
        $this->stripNull($o[$k]);
193
      }
194
    }
195
  }
196
}