Esempio n. 1
0
 def test_requests_with_no_body_are_signed(self, token_auth_args,
                                           expected_authorization):
     """Tests that requests without a body are signed."""
     auth = TokenAuth(*token_auth_args)
     request = Mock(headers={})
     auth(request)
     assert request.headers['Authorization'] == expected_authorization
Esempio n. 2
0
def _get_api_client(token=None):
    token = TokenAuth(token, 'Bearer') if token else None
    return APIClient(
        settings.ADMIN_OAUTH2_BASE_URL,
        auth=token,
        raise_for_status=True,
        default_timeout=settings.ADMIN_OAUTH2_REQUEST_TIMEOUT,
    )
Esempio n. 3
0
def _get_api_client(request=None):
    return APIClient(
        settings.DNB_SERVICE_BASE_URL,
        TokenAuth(settings.DNB_SERVICE_TOKEN),
        raise_for_status=False,
        default_timeout=settings.DNB_SERVICE_TIMEOUT,
        request=request,
    )
Esempio n. 4
0
def _request(method,
             path,
             response_serializer_class=None,
             request=None,
             **kwargs):
    """
    Internal utility function to make a generic API request to Staff SSO.

    As this deals with authentication, this aims to be more robust than might be the case
    for other API requests.
    """
    api_client = APIClient(
        settings.STAFF_SSO_BASE_URL,
        TokenAuth(settings.STAFF_SSO_AUTH_TOKEN, token_keyword='Bearer'),
        default_timeout=settings.STAFF_SSO_REQUEST_TIMEOUT,
        request=request,
    )

    try:
        response = api_client.request(method, path, **kwargs)
    except APIBadGatewayException as exc:
        raise SSORequestError('SSO service unavailable') from exc
    except RequestException as exc:
        raise SSORequestError('SSO request failed',
                              response=exc.response) from exc

    try:
        response_data = response.json()
    except ValueError as exc:
        raise SSORequestError('SSO response parsing failed') from exc

    if not response_serializer_class:
        return response_data

    try:
        serializer = response_serializer_class(data=response_data)
        serializer.is_valid(raise_exception=True)
    except serializers.ValidationError as exc:
        raise SSORequestError('SSO response validation failed') from exc

    return serializer.validated_data
Esempio n. 5
0
from datahub.core import statsd
from datahub.core.api_client import APIClient, TokenAuth
from datahub.core.serializers import AddressSerializer
from datahub.dnb_api.constants import (
    ALL_DNB_UPDATED_MODEL_FIELDS,
    ALL_DNB_UPDATED_SERIALIZER_FIELDS,
)
from datahub.dnb_api.serializers import DNBCompanySerializer
from datahub.metadata.models import Country

logger = logging.getLogger(__name__)

api_client = APIClient(
    settings.DNB_SERVICE_BASE_URL,
    TokenAuth(settings.DNB_SERVICE_TOKEN),
    raise_for_status=False,
    default_timeout=settings.DNB_SERVICE_TIMEOUT,
)


class DNBServiceException(Exception):
    """
    Base exception class for DNBService related errors.
    """


class DNBServiceError(DNBServiceException):
    """
    Exception for when DNB service doesn't return a response with a status code of 200.
    """
Esempio n. 6
0
def _get_api_client():
    return APIClient(
        settings.STAFF_SSO_BASE_URL,
        TokenAuth(settings.STAFF_SSO_AUTH_TOKEN, token_keyword='Bearer'),
        default_timeout=settings.STAFF_SSO_REQUEST_TIMEOUT,
    )