Пример #1
0
    def request(self, method, url, headers, post_data=None):
        if sys.version_info >= (3, 0) and isinstance(post_data, basestring):
            post_data = post_data.encode('utf-8')

        req = urllib2.Request(url, post_data, headers)

        if method not in ('get', 'post'):
            req.get_method = lambda: method.upper()

        try:
            response = urllib2.urlopen(req)
            rbody = response.read()
            if hasattr(rbody, 'decode'):
                rbody = rbody.decode('utf-8')
            rcode = response.code
        except urllib2.HTTPError, e:
            rcode = e.code
            rbody = e.read()
            if hasattr(rbody, 'decode'):
                rbody = rbody.decode('utf-8')
            if rcode == 400:
                json_content = json.loads(rbody)
                raise error.ValidationError("Validation failed",
                                            json_content['errors'], rbody,
                                            rcode, json_content)
            elif rcode == 401:
                json_content = json.loads(rbody)
                raise error.AuthenticationError(json_content['errors'][0],
                                                rbody, rcode, json_content)
            elif rcode >= 400:
                raise error.APIConnectionError(str(e), rbody, rcode)
Пример #2
0
    def _handle_request_error(self, e):
        if isinstance(e, requests.exceptions.RequestException):
            msg = ('There was a problem communicating with Iterable.')
            err = '%s %s' % (type(e).__name__, str(e))

        else:
            msg = ('There was a problem communicating with Iterable. '
                   'This is likely do to a local configuration problem.')
            err = 'A %s was raised' % (type(e).__name__, )
            if str(e):
                err += ' with error message %s' % (str(e), )
            else:
                err += ' with no error message'

        msg = textwrap.fill(msg) + '\n\n(Network error: %s)' % (err, )
        raise error.APIConnectionError(msg)
Пример #3
0
 def _handle_request_error(self, e):
     if isinstance(e, requests.exceptions.RequestException):
         msg = ("Unexpected error communicating with Taxamo.  "
                "If this problem persists, let us know at "
                "[email protected].")
         err = "%s: %s" % (type(e).__name__, str(e))
     else:
         msg = ("Unexpected error communicating with Taxamo. "
                "It looks like there's probably a configuration "
                "issue locally.  If this problem persists, let us "
                "know at [email protected].")
         err = "A %s was raised" % (type(e).__name__, )
         if str(e):
             err += " with error message %s" % (str(e), )
         else:
             err += " with no error message"
     msg = textwrap.fill(msg) + "\n\n(Network error: %s)" % (err, )
     raise error.APIConnectionError(msg)
Пример #4
0
    def _handle_request_error(self, e, url):
        if isinstance(e, urlfetch.InvalidURLError):
            msg = ("The UserKit library attempted to fetch an "
                   "invalid URL (%r). This is likely due to a bug "
                   "in the UserKit Python bindings. Please let us know "
                   "at [email protected]." % (url, ))
        elif isinstance(e, urlfetch.DownloadError):
            msg = "There was a problem retrieving data from UserKit."
        elif isinstance(e, urlfetch.ResponseTooLargeError):
            msg = ("There was a problem receiving all of your data from "
                   "UserKit.  This is likely due to a bug in UserKit. "
                   "Please let us know at [email protected].")
        else:
            msg = ("Unexpected error communicating with UserKit. If this "
                   "problem persists, let us know at [email protected].")

        msg = textwrap.fill(msg) + "\n\n(Network error: " + str(e) + ")"
        raise error.APIConnectionError(message=msg)
Пример #5
0
    def request_raw(self, method, url, post_data=None, supplied_headers=None):
        if not self._api_key:
            raise error.AuthenticationError('No API key has been provided.')

        if method not in ['get', 'post']:
            raise error.APIConnectionError('Unrecognized HTTP method %r.' % (method, ))

        headers = {'api_key': self._api_key}

        if method == 'post':
            headers['Content-Type'] = 'application/json'

        if supplied_headers is not None:
            for key, val in supplied_headers.iteritems():
                headers[key] = val

        rbody, rcode, rheaders = self._client.request(
            method, url, headers, post_data)

        return rbody, rcode, rheaders
Пример #6
0
    def _handle_request_error(self, e):
        if e[0] in [
                pycurl.E_COULDNT_CONNECT, pycurl.E_COULDNT_RESOLVE_HOST,
                pycurl.E_OPERATION_TIMEOUTED
        ]:
            msg = ("Could not connect to UserKit.  Please check your "
                   "internet connection and try again.  If this problem "
                   "persists, you should check UserKit's service status at "
                   "https://twitter.com/, or let us know at "
                   "[email protected].")
        elif (e[0] in [pycurl.E_SSL_CACERT, pycurl.E_SSL_PEER_CERTIFICATE]):
            msg = ("Could not verify UserKit's SSL certificate.  Please make "
                   "sure that your network is not intercepting certificates.  "
                   "If this problem persists, let us know at "
                   "[email protected].")
        else:
            msg = ("Unexpected error communicating with UserKit. If this "
                   "problem persists, let us know at [email protected].")

        msg = textwrap.fill(msg) + "\n\n(Network error: " + e[1] + ")"
        raise error.APIConnectionError(message=msg)
Пример #7
0
 def _handle_request_error(self, e):
     msg = ("Unexpected error communicating with Taxamo. "
            "If this problem persists, let us know at [email protected].")
     msg = textwrap.fill(msg) + "\n\n(Network error: " + str(e) + ")"
     raise error.APIConnectionError(msg)