Exemplo n.º 1
0
 def get(self, path, params={}):
     request = Request(self._build_url(path, params), method='GET')
     connection = self._opener.open(request)
     try:
         if 'json' in connection.info().dict['content-type']:
             return json.load(connection)
         else:
             return connection.read()
     finally:
         connection.close()
Exemplo n.º 2
0
 def delete(self, path, *args, **kwargs):
     params = kwargs.pop('params', {})
     request = Request(self._build_url(path, params), method='DELETE')
     connection = self._opener.open(request)
     try:
         if 'json' in connection.info().dict['content-type']:
             return json.load(connection)
         else:
             return connection.read()
     finally:
         connection.close()
Exemplo n.º 3
0
 def post(self, path, params={}, data={}, content_type=''):
     # Deal with content type and POST data.
     if hasattr(data, '__iter__') and not isinstance(data, basestring):
         data = urllib.urlencode(data)
         content_type = 'application/x-www-form-urlencoded'
     headers = {}
     if content_type:
         headers['Content-Type'] = content_type
     # Using custom Request object.
     request = Request(self._build_url(path, params), data=data,
         headers=headers, method='POST')
     try:
         connection = self._opener.open(request)
         content_type, params = parse_content_type(
             connection.info().dict.get('content-type', ''))
         charset = params.get('charset', 'utf-8')
         if content_type.endswith('json'):
             return json.load(connection, encoding=charset)
         else:
             return connection.read().decode(charset)
     finally:
         connection.close()