Esempio n. 1
0
    def invoke(self, request):
        # type: (ApiClientRequest) -> ApiClientResponse
        """Dispatches a request to an API endpoint described in the
        request.

        Resolves the method from input request object, converts the
        list of header tuples to the required format (dict) for the
        `requests` lib call and invokes the method with corresponding
        parameters on `requests` library. The response from the call is
        wrapped under the `ApiClientResponse` object and the
        responsibility of translating a response code and response/
        error lies with the caller.

        :param request: Request to dispatch to the ApiClient
        :type request: ApiClientRequest
        :return: Response from the client call
        :rtype: ApiClientResponse
        :raises: :py:class:`ask_sdk_core.exceptions.ApiClientException`
        """
        try:
            http_method = self._resolve_method(request)
            http_headers = self._convert_list_tuples_to_dict(
                headers_list=request.headers
            )

            request.url = OPEN_DATA_API_ENDPOINT + request.url
            parsed_url = parse_url(request.url)
            if parsed_url.scheme is None or parsed_url.scheme != "https":
                raise ApiClientException(
                    "Requests against non-HTTPS endpoints are not allowed."
                )

            raw_data = None  # type: Optional[str]
            if request.body:
                body_content_type = http_headers.get("Content-type", None)
                if body_content_type is not None and "json" in body_content_type:
                    raw_data = json.dumps(request.body)
                else:
                    raw_data = request.body

            api_security_token = self.token_helper.get_security_bearer_token()
            with http_method(
                url=request.url,
                headers=http_headers,
                data=raw_data,
                auth=BearerAuth(api_security_token),
            ) as http_response:
                http_response.raise_for_status()
                return ApiClientResponse(
                    headers=self._convert_dict_to_list_tuples(http_response.headers),
                    status_code=http_response.status_code,
                    body=http_response,
                )
        except Exception as error:
            message = str(error)
            raise ApiClientException("Error executing the request: {}".format(message))
Esempio n. 2
0
    def invoke(self, request):
        url = request.url
        if PASSING_TIME_BY_POINT_SUFFIX in url:
            mock_api_response = JsonMockResponse(
                json.loads(PASSING_TIME_BY_POINT_SUCCESS_API_RESPONSE), "200")
        elif STOPS_BY_LINE_SUFFIX in url:
            mock_api_response = JsonMockResponse(
                json.loads(STOPS_BY_LINE_SUCCESS_API_RESPONSE), "200")
        else:
            with open(
                    os.path.dirname(os.path.dirname(__file__)) +
                    "/tests/gtfs.zip", "rb") as gtfs_zip_file:
                data = gtfs_zip_file.read()
            mock_api_response = BinaryMockResponse(binary_content=data)

        api_client_response = ApiClientResponse(body=mock_api_response)
        return api_client_response
Esempio n. 3
0
    def invoke(self, request):
        # type: (ApiClientRequest) -> ApiClientResponse
        """Dispatches a request to an API endpoint described in the
        request.

        Resolves the method from input request object, converts the
        list of header tuples to the required format (dict) for the
        `requests` lib call and invokes the method with corresponding
        parameters on `requests` library. The response from the call is
        wrapped under the `ApiClientResponse` object and the
        responsibility of translating a response code and response/
        error lies with the caller.

        :param request: Request to dispatch to the ApiClient
        :type request: ApiClientRequest
        :return: Response from the client call
        :rtype: ApiClientResponse
        :raises: :py:class:`ask_sdk_core.exceptions.ApiClientException`
        """
        try:
            http_method = self._resolve_method(request)
            http_headers = self._convert_list_tuples_to_dict(
                headers_list=request.headers)

            parsed_url = parse_url(request.url)
            if parsed_url.scheme is None or parsed_url.scheme != "https":
                raise ApiClientException(
                    "Requests against non-HTTPS endpoints are not allowed.")

            if request.body:
                raw_data = json.dumps(request.body)
            else:
                raw_data = None

            http_response = http_method(
                url=request.url, headers=http_headers, data=raw_data)

            return ApiClientResponse(
                headers=self._convert_dict_to_list_tuples(
                    http_response.headers),
                status_code=http_response.status_code,
                body=http_response.text)
        except Exception as e:
            raise ApiClientException(
                "Error executing the request: {}".format(str(e)))