コード例 #1
0
 def do_request(self, method, action, body=None, headers=None, params=None):
     # Add format and tenant_id
     action += ".%s" % self.format
     action = self.action_prefix + action
     if type(params) is dict and params:
         params = utils.safe_encode_dict(params)
         action += '?' + urllib.urlencode(params, doseq=1)
     # Ensure client always has correct uri - do not guesstimate anything
     self.httpclient.authenticate_and_fetch_endpoint_url()
     uri_len = len(self.httpclient.endpoint_url) + len(action)
     if uri_len > self.MAX_URI_LEN:
         raise exceptions.RequestURITooLong(
             excess=uri_len - self.MAX_URI_LEN)
     if body:
         body = self.serialize(body)
     self.httpclient.content_type = self.content_type()
     resp, replybody = self.httpclient.do_request(action, method, body=body)
     status_code = self.get_status_code(resp)
     if status_code in (httplib.OK,
                        httplib.CREATED,
                        httplib.ACCEPTED,
                        httplib.NO_CONTENT):
         return self.deserialize(replybody, status_code)
     else:
         self._handle_fault_response(status_code, replybody)
コード例 #2
0
    def _cs_request(self, *args, **kwargs):
        kargs = {}
        kargs.setdefault('headers', kwargs.get('headers', {}))
        kargs['headers']['User-Agent'] = self.USER_AGENT

        if 'content_type' in kwargs:
            kargs['headers']['Content-Type'] = kwargs['content_type']
            kargs['headers']['Accept'] = kwargs['content_type']
        else:
            kargs['headers']['Content-Type'] = self.content_type
            kargs['headers']['Accept'] = self.content_type

        if 'body' in kwargs:
            kargs['body'] = kwargs['body']
        args = utils.safe_encode_list(args)
        kargs = utils.safe_encode_dict(kargs)
        utils.http_log_req(_logger, args, kargs)
        resp, body = self.request(*args, **kargs)
        utils.http_log_resp(_logger, resp, body)
        status_code = self.get_status_code(resp)
        if status_code == 401:
            raise exceptions.Unauthorized(message=body)
        elif status_code == 403:
            raise exceptions.Forbidden(message=body)
        return resp, body
コード例 #3
0
 def test_safe_encode_dict(self):
     o = object()
     unicode_text = u'\u7f51\u7edc'
     d = {'test1': unicode_text,
          'test2': [unicode_text, o],
          'test3': o,
          'test4': {'test5': unicode_text},
          'test6': unicode_text.encode('utf-8')}
     expected = {'test1': unicode_text.encode('utf-8'),
                 'test2': [unicode_text.encode('utf-8'), o],
                 'test3': o,
                 'test4': {'test5': unicode_text.encode('utf-8')},
                 'test6': unicode_text.encode('utf-8')}
     self.assertEqual(utils.safe_encode_dict(d), expected)