Пример #1
0
 def json(self):
     if self._data is None:
         return json.load(self._wrapped)
     else:
         return json.loads(self._data)
Пример #2
0
 def json(self):
     if self._data is None:
         return json.load(self._wrapped)
     else:
         return json.loads(self._data)
Пример #3
0
    def _doURL(self,
               url,
               method='GET',
               params=None,
               data=None,
               content_type=None,
               stream=False,
               headers=None,
               role=None):
        request_headers = self.get_default_headers()
        if headers is not None:
            request_headers.update(headers)
        if content_type:
            request_headers['Content-Type'] = content_type

        if role is not None:
            request_headers['SAM-Role'] = str(role)

        if method in ('POST', 'PUT'):
            # these always require body data
            if data is None:
                data = ''

        self._logMethod(method, url, params=params, data=data)

        if data is not None and not isinstance(data, basestring):
            data = urlencode(data, doseq=True)

        if params is not None:
            if '?' not in url: url += '?'
            else: url += '&'
            url += urlencode(params, doseq=True)
        tmout = time.time() + self.maxtimeout
        retryinterval = 1

        request = RequestWithMethod(url,
                                    method=method,
                                    headers=request_headers,
                                    logger=self._logger)
        if data is not None:
            request.add_data(data)
        kwargs = {}
        if self.socket_timeout is not None:
            kwargs['timeout'] = self.socket_timeout
        while True:
            try:
                return Response(self._opener.open(request, **kwargs),
                                stream=stream)
            except HTTPError, x:
                #python 2.4 treats 201 and up as errors instead of normal return codes
                if 201 <= x.code <= 299:
                    return Response(x)
                if x.headers.get('Content-Type') == 'application/json':
                    err = json.load(x)
                    errmsg = err['message']
                    errtype = err['error']
                else:
                    if x.code >= 500:
                        errmsg = "HTTP error: %d %s" % (x.code, x.msg)
                    else:
                        errmsg = x.read().rstrip()
                    errtype = None
                x.close(
                )  # ensure that the socket is closed (otherwise it may hang around in the traceback object)
                # retry server errors
                if x.code >= 500 and time.time() < tmout:
                    pass
                else:
                    raise makeHTTPError(method, url, x.code, errmsg, errtype)
            except URLError, x:
                if isinstance(x.reason, socket.sslerror):
                    raise self.make_ssl_error(str(x.reason))
                else:
                    errmsg = str(x.reason)
                if time.time() >= tmout:
                    raise SAMWebConnectionError(errmsg)
Пример #4
0
    def _doURL(self, url, method='GET', params=None, data=None, content_type=None, stream=False, headers=None,role=None):
        request_headers = self.get_default_headers()
        if headers is not None:
            request_headers.update(headers)
        if content_type:
            request_headers['Content-Type'] = content_type

        if role is not None:
            request_headers['SAM-Role'] = str(role)

        if method in ('POST', 'PUT'):
            # these always require body data
            if data is None:
                data = ''

        self._logMethod(method, url, params=params, data=data)

        if data is not None and not isinstance(data, basestring):
            data = urlencode(data, doseq=True)

        if params is not None:
            if '?' not in url: url += '?'
            else: url += '&'
            url += urlencode(params, doseq=True)
        tmout = time.time() + self.maxtimeout
        retryinterval = 1

        request = RequestWithMethod(url, method=method, headers=request_headers, logger=self._logger)
        if data is not None:
            request.add_data(data)
        kwargs = {}
        if self.socket_timeout is not None:
            kwargs['timeout'] = self.socket_timeout
        while True:
            try:
                return Response(self._opener.open(request, **kwargs), stream=stream)
            except HTTPError, x:
                #python 2.4 treats 201 and up as errors instead of normal return codes
                if 201 <= x.code <= 299:
                    return Response(x)
                if x.headers.get('Content-Type') == 'application/json':
                    err = json.load(x)
                    errmsg = err['message']
                    errtype = err['error']
                else:
                    if x.code >= 500:
                        errmsg = "HTTP error: %d %s" % (x.code, x.msg)
                    else:
                        errmsg = x.read().rstrip()
                    errtype = None
                x.close() # ensure that the socket is closed (otherwise it may hang around in the traceback object)
                # retry server errors
                if x.code >= 500 and time.time() < tmout:
                    pass
                else:
                    raise makeHTTPError(method, url, x.code, errmsg, errtype)
            except URLError, x:
                if isinstance(x.reason, socket.sslerror):
                    raise self.make_ssl_error(str(x.reason))
                else:
                    errmsg = str(x.reason)
                if time.time() >= tmout:
                    raise SAMWebConnectionError(errmsg)