예제 #1
0
    def get(self, request, pk):
        """
        Proxy to Export Wins API for GET requests for given company's match id
        is obtained from Company Matching Service.
        """
        company = self._get_company(pk)
        try:
            match_ids = self._get_match_ids(company, request)
        except (
                CompanyMatchingServiceConnectionError,
                CompanyMatchingServiceTimeoutError,
                CompanyMatchingServiceHTTPError,
        ) as exc:
            raise APIUpstreamException(str(exc))

        if not match_ids:
            return JsonResponse(
                {
                    'count': 0,
                    'next': None,
                    'previous': None,
                    'results': [],
                }, )

        try:
            export_wins_results = get_export_wins(match_ids, request)
        except (
                ExportWinsAPIConnectionError,
                ExportWinsAPITimeoutError,
                ExportWinsAPIHTTPError,
        ) as exc:
            raise APIUpstreamException(str(exc))

        return JsonResponse(export_wins_results.json())
예제 #2
0
    def get(self, request, pk):
        """
        Proxy to Export Wins API for GET requests for given company's match id
        is obtained from Company Matching Service.
        """
        company = self._get_company(pk)
        try:
            match_ids = self._get_match_ids(company, request)
            export_wins_results = get_export_wins(match_ids, request)
            return JsonResponse(export_wins_results.json())

        except (
                CompanyMatchingServiceConnectionError,
                CompanyMatchingServiceTimeoutError,
                CompanyMatchingServiceHTTPError,
                ExportWinsAPIConnectionError,
                ExportWinsAPITimeoutError,
                ExportWinsAPIHTTPError,
        ) as exc:
            raise APIUpstreamException(str(exc))
        else:
            return HttpResponse(
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
                content_type='application/json',
            )
예제 #3
0
    def post(self, request):
        """
        Given a Data Hub Company ID and a duns-number, link the Data Hub
        Company to the D&B record.
        """
        link_serializer = DNBCompanyLinkSerializer(data=request.data)

        link_serializer.is_valid(raise_exception=True)

        # This bit: validated_data['company_id'].id is weird but the alternative
        # is to rename the field to `company_id` which would (1) still be weird
        # and (2) leak the weirdness to the API
        company_id = link_serializer.validated_data['company_id'].id
        duns_number = link_serializer.validated_data['duns_number']

        try:
            company = link_company_with_dnb(company_id, duns_number,
                                            request.user)

        except (
                DNBServiceConnectionError,
                DNBServiceInvalidResponse,
                DNBServiceError,
        ) as exc:
            raise APIUpstreamException(str(exc))

        except (
                DNBServiceInvalidRequest,
                CompanyAlreadyDNBLinkedException,
        ) as exc:
            raise APIBadRequestException(str(exc))

        return Response(CompanySerializer().to_representation(company), )
예제 #4
0
    def post(self, request):
        """
        A wrapper around the investigation API endpoint for dnb-service.
        """
        investigation_serializer = DNBCompanyInvestigationSerializer(
            data=request.data)
        investigation_serializer.is_valid(raise_exception=True)

        data = {'company_details': investigation_serializer.validated_data}
        company = data['company_details'].pop('company')

        try:
            response = create_investigation(data)

        except (
                DNBServiceConnectionError,
                DNBServiceTimeoutError,
                DNBServiceError,
        ) as exc:
            raise APIUpstreamException(str(exc))

        company.dnb_investigation_id = response['id']
        company.pending_dnb_investigation = True
        company.save()

        return Response(response)
예제 #5
0
    def get(self, request):
        """
        A thin wrapper around the dnb-service change request API.
        """
        duns_number = request.query_params.get('duns_number', None)
        status = request.query_params.get('status', None)

        change_request_serializer = DNBGetCompanyChangeRequestSerializer(data={
            'duns_number':
            duns_number,
            'status':
            status
        }, )

        change_request_serializer.is_valid(raise_exception=True)

        try:
            response = get_change_request(
                **change_request_serializer.validated_data)

        except (
                DNBServiceConnectionError,
                DNBServiceTimeoutError,
                DNBServiceError,
        ) as exc:
            raise APIUpstreamException(str(exc))

        return Response(response)
예제 #6
0
    def post(self, request):
        """
        A thin wrapper around the dnb-service change request API.
        """
        change_request_serializer = DNBCompanyChangeRequestSerializer(
            data=request.data)
        change_request_serializer.is_valid(raise_exception=True)

        try:
            response = request_changes(
                **change_request_serializer.validated_data)

        except (
                DNBServiceConnectionError,
                DNBServiceTimeoutError,
                DNBServiceError,
        ) as exc:
            raise APIUpstreamException(str(exc))

        return Response(response)
예제 #7
0
    def post(self, request):
        """
        Given a duns_number, get the data for the company from dnb-service
        and create a record in DataHub.
        """
        duns_serializer = DUNSNumberSerializer(data=request.data)
        duns_serializer.is_valid(raise_exception=True)
        duns_number = duns_serializer.validated_data['duns_number']

        try:
            dnb_company = get_company(duns_number)

        except (DNBServiceConnectionError, DNBServiceError,
                DNBServiceInvalidResponse) as exc:
            raise APIUpstreamException(str(exc))

        except DNBServiceInvalidRequest as exc:
            raise APIBadRequestException(str(exc))

        company_serializer = DNBCompanySerializer(data=dnb_company, )

        try:
            company_serializer.is_valid(raise_exception=True)
        except serializers.ValidationError:
            message = 'Company data from DNB failed DH serializer validation'
            extra_data = {
                'formatted_dnb_company_data': dnb_company,
                'dh_company_serializer_errors': company_serializer.errors,
            }
            logger.error(message, extra=extra_data)
            raise

        datahub_company = company_serializer.save(
            created_by=request.user,
            modified_by=request.user,
            dnb_modified_on=now(),
        )

        statsd.incr(f'dnb.create.company')
        return Response(
            company_serializer.to_representation(datahub_company), )