Example #1
0
 def _make_request(self, url, method, headers, data=None, params=None,
                   **kwargs):
     """Actually make an HTTP request."""
     try:
         return HTTP.request_with_timeout(
             method, url, headers=headers, data=data,
             params=params, timeout=90, **kwargs
         )
     except RequestTimedOut, e:
         self.log.info("Request to %s timed out once. Trying a second time.", url)
         return HTTP.request_with_timeout(
             method, url, headers=headers, data=data,
             params=params, timeout=90, **kwargs
         )
Example #2
0
    def patron_request(self, patron, pin, url, extra_headers={}, data=None, exception_on_401=False, method=None):
        """Make an HTTP request on behalf of a patron.

        The results are never cached.
        """
        headers = dict(Authorization="Bearer %s" % self.token)
        headers['Content-Type'] = 'application/json'
        headers.update(extra_headers)

        if method and method.lower() in ('get', 'post', 'put', 'delete'):
            method = method.lower()
        else:
            if data:
                method = 'post'
            else:
                method = 'get'

        url = self._make_absolute_url(url)
        response = HTTP.request_with_timeout(
            method, url, headers=headers, data=data,
            timeout=60
        )
        if response.status_code == 401:
            if exception_on_401:
                # This is our second try. Give up.
                raise Exception("Something's wrong with the patron OAuth Bearer Token!")
            else:
                # Refresh the token and try again.
                self.check_creds(True)
                return self.patron_request(patron, pin, url, extra_headers, data, True)
        else:
            return response
Example #3
0
    def patron_request(self, patron, pin, url, extra_headers={}, data=None,
                       exception_on_401=False, method=None):
        """Make an HTTP request on behalf of a patron.

        The results are never cached.
        """
        patron_credential = self.get_patron_credential(patron, pin)
        headers = dict(Authorization="Bearer %s" % patron_credential.credential)
        headers.update(extra_headers)
        if method and method.lower() in ('get', 'post', 'put', 'delete'):
            method = method.lower()
        else:
            if data:
                method = 'post'
            else:
                method = 'get'
        response = HTTP.request_with_timeout(
            method, url, headers=headers, data=data
        )
        if response.status_code == 401:
            if exception_on_401:
                # This is our second try. Give up.
                raise Exception("Something's wrong with the patron OAuth Bearer Token!")
            else:
                # Refresh the token and try again.
                self.refresh_patron_access_token(
                    patron_credential, patron, pin)
                return self.patron_request(
                    patron, pin, url, extra_headers, data, True)
        else:
            # This is commented out because it may expose patron
            # information.
            #
            # self.log.debug("%s: %s", url, response.status_code)
            return response
Example #4
0
    def patron_request(self, patron, pin, url, extra_headers={}, data=None,
                       exception_on_401=False, method=None):
        """Make an HTTP request on behalf of a patron.

        The results are never cached.
        """
        patron_credential = self.get_patron_credential(patron, pin)
        headers = dict(Authorization="Bearer %s" % patron_credential.credential)
        headers.update(extra_headers)
        if method and method.lower() in ('get', 'post', 'put', 'delete'):
            method = method.lower()
        else:
            if data:
                method = 'post'
            else:
                method = 'get'
        response = HTTP.request_with_timeout(
            method, url, headers=headers, data=data
        )
        if response.status_code == 401:
            if exception_on_401:
                # This is our second try. Give up.
                raise Exception("Something's wrong with the patron OAuth Bearer Token!")
            else:
                # Refresh the token and try again.
                self.refresh_patron_access_token(
                    patron_credential, patron, pin)
                return self.patron_request(
                    patron, pin, url, extra_headers, data, True)
        else:
            # This is commented out because it may expose patron
            # information.
            #
            # self.log.debug("%s: %s", url, response.status_code)
            return response
Example #5
0
    def _request(self, method, url, headers, data, params, **kwargs):
        """Actually make an HTTP request.

        MockEnkiAPI overrides this method.
        """
        return HTTP.request_with_timeout(
            method, url, headers=headers, data=data,
            params=params, timeout=90, disallowed_response_codes=None,
            **kwargs
        )
Example #6
0
    def _request(self, method, url, headers, data, params, **kwargs):
        """Actually make an HTTP request.

        MockEnkiAPI overrides this method.
        """
        return HTTP.request_with_timeout(
            method, url, headers=headers, data=data,
            params=params, timeout=90, disallowed_response_codes=None,
            **kwargs
        )
Example #7
0
 def _make_request(self,
                   url,
                   method,
                   headers,
                   data=None,
                   params=None,
                   **kwargs):
     """Actually make an HTTP request."""
     return HTTP.request_with_timeout(method,
                                      url,
                                      headers=headers,
                                      data=data,
                                      params=params,
                                      **kwargs)
Example #8
0
    def patron_request(
        self,
        patron,
        pin,
        url,
        extra_headers={},
        data=None,
        exception_on_401=False,
        method=None,
    ):
        """Make an HTTP request on behalf of a patron.

        The results are never cached.
        """
        headers = dict(Authorization="Bearer %s" % self.token)
        headers["Content-Type"] = "application/json"
        headers.update(extra_headers)

        if method and method.lower() in ("get", "post", "put", "delete"):
            method = method.lower()
        else:
            if data:
                method = "post"
            else:
                method = "get"

        url = self._make_absolute_url(url)
        response = HTTP.request_with_timeout(method,
                                             url,
                                             headers=headers,
                                             data=data,
                                             timeout=60)

        # TODO: If Odilo doesn't recognize the patron it will send
        # 404 in this case.
        if response.status_code == 401:
            if exception_on_401:
                # This is our second try. Give up.
                raise Exception(
                    "Something's wrong with the patron OAuth Bearer Token!")
            else:
                # Refresh the token and try again.
                self.check_creds(True)
                return self.patron_request(patron, pin, url, extra_headers,
                                           data, True)
        else:
            return response
    def delete_content_item(self, identifier):
        content_item_id = identifier

        if isinstance(identifier, Identifier):
            if not identifier.type == Identifier.BIBBLIO_CONTENT_ITEM_ID:
                raise TypeError('Identifier is not a Bibblio Content Item')

            content_item_id = identifier.identifier
        delete_url = self.CONTENT_ITEMS_ENDPOINT + content_item_id
        response = HTTP.request_with_timeout(
            'DELETE', delete_url, headers=self.default_headers,
            allowed_response_codes=[200]
        )

        if not isinstance(identifier, basestring) and response.status_code == 200:
            self._db.delete(identifier)
            self.log.info("DELETED: Bibblio Content Item '%s'" % content_item_id)
 def _request_with_timeout(self, method, url, *args, **kwargs):
     """Wrapper around HTTP.request_with_timeout to be overridden for tests."""
     return HTTP.request_with_timeout(method, url, *args, **kwargs)
Example #11
0
 def request(self, url, *args, **kwargs):
     """Actually make an HTTP request. This method exists only so the mock
     can override it.
     """
     self._update_request_kwargs(kwargs)
     return HTTP.request_with_timeout("GET", url, *args, **kwargs)
 def _request_with_timeout(self, method, url, *args, **kwargs):
     """Wrapper around HTTP.request_with_timeout to be overridden for tests."""
     return HTTP.request_with_timeout(method, url, *args, **kwargs)