Example #1
0
 def response(self, resp, content):
   # Error handling is TBD, for example, do we retry
   # for some operation/error combinations?
   if resp.status < 300:
     if resp.status == 204:
       # A 204: No Content response should be treated differently to all the other success states
       return simplejson.loads('{}')
     body = simplejson.loads(content)
     if isinstance(body, dict) and 'data' in body:
       body = body['data']
     return body
   else:
     logging.debug('Content from bad request was: %s' % content)
     if resp.get('content-type', '').startswith('application/json'):
       raise HttpError(resp, simplejson.loads(content)['error'])
     else:
       raise HttpError(resp, '%d %s' % (resp.status, resp.reason))
Example #2
0
def build(serviceName, version, http=None,
    discoveryServiceUrl=DISCOVERY_URI, developerKey=None, model=JsonModel()):
  params = {
      'api': serviceName,
      'apiVersion': version
      }

  if http is None:
    http = httplib2.Http()
  requested_url = uritemplate.expand(discoveryServiceUrl, params)
  logging.info('URL being requested: %s' % requested_url)
  resp, content = http.request(requested_url)
  service = simplejson.loads(content)

  fn = os.path.join(os.path.dirname(__file__), "contrib",
      serviceName, "future.json")
  try:
    f = file(fn, "r")
    d = simplejson.load(f)
    f.close()
    future = d['resources']
    auth_discovery = d['auth']
  except IOError:
    future = {}
    auth_discovery = {}

  base = urlparse.urljoin(discoveryServiceUrl, service['restBasePath'])
  resources = service['resources']

  class Service(object):
    """Top level interface for a service"""

    def __init__(self, http=http):
      self._http = http
      self._baseUrl = base
      self._model = model
      self._developerKey = developerKey

    def auth_discovery(self):
      return auth_discovery

  def createMethod(theclass, methodName, methodDesc, futureDesc):

    def method(self):
      return createResource(self._http, self._baseUrl, self._model,
          methodName, self._developerKey, methodDesc, futureDesc)

    setattr(method, '__doc__', 'A description of how to use this function')
    setattr(method, '__is_resource__', True)
    setattr(theclass, methodName, method)

  for methodName, methodDesc in resources.iteritems():
    createMethod(Service, methodName, methodDesc, future.get(methodName, {}))
  return Service()