|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights |
| 4 | +# Reserved. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"). |
| 7 | +# You may not use this file except in compliance with the License. |
| 8 | +# A copy of the License is located at |
| 9 | +# |
| 10 | +# http://aws.amazon.com/apache2.0/ |
| 11 | +# |
| 12 | +# or in the "license" file accompanying this file. This file is |
| 13 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS |
| 14 | +# OF ANY KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations under the |
| 16 | +# License. |
| 17 | +# |
| 18 | +import typing |
| 19 | +import requests |
| 20 | +import six |
| 21 | +import json |
| 22 | + |
| 23 | +from abc import ABCMeta, abstractmethod |
| 24 | +from urllib3.util import parse_url |
| 25 | + |
| 26 | +from .api_client_response import ApiClientResponse |
| 27 | + |
| 28 | +from .exceptions import ApiClientException |
| 29 | + |
| 30 | +if typing.TYPE_CHECKING: |
| 31 | + from typing import Callable, Dict, List, Tuple |
| 32 | + from ask_sdk_model_runtime import ApiClientRequest, ApiClientResponse |
| 33 | + |
| 34 | + |
| 35 | +class ApiClient(object): |
| 36 | + """Represents a basic contract for API request invocation.""" |
| 37 | + __metaclass__ = ABCMeta |
| 38 | + |
| 39 | + @abstractmethod |
| 40 | + def invoke(self, request): |
| 41 | + # type: (ApiClientRequest) -> ApiClientResponse |
| 42 | + """Dispatches a request to an API endpoint described in the request. |
| 43 | + The ApiClient is expected to resolve in the case an API returns |
| 44 | + a non-200 HTTP status code. The responsibility of translating a |
| 45 | + particular response code to an error lies with the caller. |
| 46 | + :param request: Request to dispatch to the ApiClient |
| 47 | + :type request: ApiClientRequest |
| 48 | + :return: Response from the client call |
| 49 | + :rtype: ApiClientResponse |
| 50 | + """ |
| 51 | + pass |
| 52 | + |
| 53 | + |
| 54 | +class DefaultApiClient(ApiClient): |
| 55 | + """Default ApiClient implementation of |
| 56 | + :py:class:`ask_sdk_model_runtime.api_client.ApiClient` using the |
| 57 | + `requests` library. |
| 58 | + """ |
| 59 | + |
| 60 | + def invoke(self, request): |
| 61 | + # type: (ApiClientRequest) -> ApiClientResponse |
| 62 | + """Dispatches a request to an API endpoint described in the |
| 63 | + request. |
| 64 | + Resolves the method from input request object, converts the |
| 65 | + list of header tuples to the required format (dict) for the |
| 66 | + `requests` lib call and invokes the method with corresponding |
| 67 | + parameters on `requests` library. The response from the call is |
| 68 | + wrapped under the `ApiClientResponse` object and the |
| 69 | + responsibility of translating a response code and response/ |
| 70 | + error lies with the caller. |
| 71 | + :param request: Request to dispatch to the ApiClient |
| 72 | + :type request: ApiClientRequest |
| 73 | + :return: Response from the client call |
| 74 | + :rtype: ApiClientResponse |
| 75 | + :raises: :py:class:`ask_sdk_model_runtime.exceptions.ApiClientException` |
| 76 | + """ |
| 77 | + try: |
| 78 | + http_method = self._resolve_method(request) |
| 79 | + http_headers = self._convert_list_tuples_to_dict( |
| 80 | + headers_list=request.headers) |
| 81 | + |
| 82 | + parsed_url = parse_url(request.url) |
| 83 | + if parsed_url.scheme is None or parsed_url.scheme != "https": |
| 84 | + raise ApiClientException( |
| 85 | + "Requests against non-HTTPS endpoints are not allowed.") |
| 86 | + |
| 87 | + if request.body: |
| 88 | + body_content_type = http_headers.get("Content-type", None) |
| 89 | + if (body_content_type is not None and |
| 90 | + "json" in body_content_type): |
| 91 | + raw_data = json.dumps(request.body) |
| 92 | + else: |
| 93 | + raw_data = request.body |
| 94 | + else: |
| 95 | + raw_data = None |
| 96 | + |
| 97 | + http_response = http_method( |
| 98 | + url=request.url, headers=http_headers, data=raw_data) |
| 99 | + |
| 100 | + return ApiClientResponse( |
| 101 | + headers=self._convert_dict_to_list_tuples( |
| 102 | + http_response.headers), |
| 103 | + status_code=http_response.status_code, |
| 104 | + body=http_response.text) |
| 105 | + except Exception as e: |
| 106 | + raise ApiClientException( |
| 107 | + "Error executing the request: {}".format(str(e))) |
| 108 | + |
| 109 | + def _resolve_method(self, request): |
| 110 | + # type: (ApiClientRequest) -> Callable |
| 111 | + """Resolve the method from request object to `requests` http |
| 112 | + call. |
| 113 | + :param request: Request to dispatch to the ApiClient |
| 114 | + :type request: ApiClientRequest |
| 115 | + :return: The HTTP method that maps to the request call. |
| 116 | + :rtype: Callable |
| 117 | + :raises :py:class:`ask_sdk_model_runtime.exceptions.ApiClientException` |
| 118 | + if invalid http request method is being called |
| 119 | + """ |
| 120 | + try: |
| 121 | + return getattr(requests, request.method.lower()) |
| 122 | + except AttributeError: |
| 123 | + raise ApiClientException( |
| 124 | + "Invalid request method: {}".format(request.method)) |
| 125 | + |
| 126 | + def _convert_list_tuples_to_dict(self, headers_list): |
| 127 | + # type: (List[Tuple[str, str]]) -> Dict[str, str] |
| 128 | + """Convert list of tuples from headers of request object to |
| 129 | + dictionary format. |
| 130 | + :param headers_list: List of tuples made up of two element |
| 131 | + strings from `ApiClientRequest` headers variable |
| 132 | + :type headers_list: List[Tuple[str, str]] |
| 133 | + :return: Dictionary of headers in keys as strings and values |
| 134 | + as comma separated strings |
| 135 | + :rtype: Dict[str, str] |
| 136 | + """ |
| 137 | + headers_dict = {} # type: Dict |
| 138 | + if headers_list is not None: |
| 139 | + for header_tuple in headers_list: |
| 140 | + key, value = header_tuple[0], header_tuple[1] |
| 141 | + if key in headers_dict: |
| 142 | + headers_dict[key] = "{}, {}".format( |
| 143 | + headers_dict[key], value) |
| 144 | + else: |
| 145 | + headers_dict[header_tuple[0]] = value |
| 146 | + return headers_dict |
| 147 | + |
| 148 | + def _convert_dict_to_list_tuples(self, headers_dict): |
| 149 | + # type: (Dict[str, str]) -> List[Tuple[str, str]] |
| 150 | + """Convert headers dict to list of string tuples format for |
| 151 | + `ApiClientResponse` headers variable. |
| 152 | + :param headers_dict: Dictionary of headers in keys as strings |
| 153 | + and values as comma separated strings |
| 154 | + :type headers_dict: Dict[str, str] |
| 155 | + :return: List of tuples made up of two element strings from |
| 156 | + headers of client response |
| 157 | + :rtype: List[Tuple[str, str]] |
| 158 | + """ |
| 159 | + headers_list = [] |
| 160 | + if headers_dict is not None: |
| 161 | + for key, values in six.iteritems(headers_dict): |
| 162 | + for value in values.split(","): |
| 163 | + value = value.strip() |
| 164 | + if value is not None and value != '': |
| 165 | + headers_list.append((key, value.strip())) |
| 166 | + return headers_list |
| 167 | + |
0 commit comments