|
| 1 | +from __future__ import absolute_import, division, print_function, \ |
| 2 | + unicode_literals |
| 3 | +import abc |
| 4 | +import re |
| 5 | +import six |
| 6 | +from splitapiclient.util.exceptions import MissingParametersException |
| 7 | + |
| 8 | + |
| 9 | +class BaseHttpClient(six.with_metaclass(abc.ABCMeta)): |
| 10 | + ''' |
| 11 | + Abstrac class providing an interface and generic methods of the HTTP client |
| 12 | + responsible for interacting with the Split APIs |
| 13 | + ''' |
| 14 | + |
| 15 | + def __init__(self, baseurl, auth_token): |
| 16 | + ''' |
| 17 | + Class constructor. Sotores basic connection information. |
| 18 | +
|
| 19 | + :param baseurl: string. Split host and base url. |
| 20 | + :param auth_token: string. Authentication token needed to make API |
| 21 | + calls. |
| 22 | + ''' |
| 23 | + self.config = { |
| 24 | + 'base_url': baseurl, |
| 25 | + 'base_args': { |
| 26 | + 'Authorization': auth_token |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @abc.abstractmethod |
| 31 | + def make_request(self, method, body=None, **kwargs): |
| 32 | + ''' |
| 33 | + Method responsible for executing an actual requesting, retrievieng an |
| 34 | + appropiarte response and raising an exception if the HTTP call fails. |
| 35 | +
|
| 36 | + :param endpoint: dict. Endpoint description (method, url, etc) |
| 37 | + :param body: dict/list. POST/PUT/PATCH request body (optional) |
| 38 | + :param kwargs: dict. Extra parameters required for headers, querystring, |
| 39 | + url template paramters, etc. |
| 40 | +
|
| 41 | + :rtype: dict/list/None |
| 42 | + ''' |
| 43 | + pass |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def get_params_from_url_template(url): |
| 47 | + ''' |
| 48 | + Retuns a list of tempated variables that must be replaced when |
| 49 | + instantiating the url template. |
| 50 | +
|
| 51 | + :param url: url template |
| 52 | +
|
| 53 | + :rtype: list. |
| 54 | + ''' |
| 55 | + regex = '{([\w-]+)}*' |
| 56 | + url_params = re.findall(regex, url) |
| 57 | + return list(set(url_params)) if url_params else [] |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def _process_single_header(header, value): |
| 61 | + ''' |
| 62 | + Checks if the header's value is a templated string. If that is the case, |
| 63 | + the template is instantiated with the correct value. Otherwise the |
| 64 | + single value is returned. |
| 65 | +
|
| 66 | + :param header: dict. Header description from endpoint. |
| 67 | + :param value: string. Header value |
| 68 | +
|
| 69 | + rtype: string. |
| 70 | + ''' |
| 71 | + template = header.get('template') |
| 72 | + if template: |
| 73 | + return template.format(value=value) |
| 74 | + else: |
| 75 | + return value |
| 76 | + |
| 77 | + @staticmethod |
| 78 | + def _setup_headers(endpoint, params): |
| 79 | + ''' |
| 80 | + Returns a dictionary with header_name: value format. |
| 81 | + Mandatory headers are always added and an exception will be thrown |
| 82 | + if the value is not available. |
| 83 | + Optional headers are only added if the value is available in params. |
| 84 | +
|
| 85 | + :param endpoint: dict. Endpoint description |
| 86 | + :param params: dict. List of parameter values |
| 87 | +
|
| 88 | + :rtype: dict. |
| 89 | + ''' |
| 90 | + headers = { |
| 91 | + header['name']: BaseHttpClient._process_single_header( |
| 92 | + header, params[header['name']] |
| 93 | + ) |
| 94 | + for header in endpoint['headers'] |
| 95 | + if header.get('required', False) |
| 96 | + } |
| 97 | + |
| 98 | + # add optional headers |
| 99 | + headers.update({ |
| 100 | + header['name']: BaseHttpClient._process_single_header( |
| 101 | + header, params[header['name']] |
| 102 | + ) |
| 103 | + for header in endpoint['headers'] |
| 104 | + if (not header.get('required', False)) and header['name'] in params |
| 105 | + }) |
| 106 | + |
| 107 | + return headers |
| 108 | + |
| 109 | + def _setup_url(self, endpoint, params): |
| 110 | + ''' |
| 111 | + Instantiates the url template with values supplied in params. |
| 112 | +
|
| 113 | + :param endpoint: dict. Endpoint description. |
| 114 | + :param params: dict. Parameter values. |
| 115 | +
|
| 116 | + :rtype: string. |
| 117 | + ''' |
| 118 | + base_template = '{base}/{endpoint}'.format( |
| 119 | + base=self.config['base_url'], |
| 120 | + endpoint=endpoint['url_template'] |
| 121 | + ) |
| 122 | + |
| 123 | + params_for_url = BaseHttpClient.get_params_from_url_template( |
| 124 | + base_template |
| 125 | + ) |
| 126 | + |
| 127 | + parameter_values = { |
| 128 | + param: params[param] |
| 129 | + for param in params |
| 130 | + if param in params_for_url |
| 131 | + } |
| 132 | + |
| 133 | + return base_template.format(**parameter_values) |
| 134 | + |
| 135 | + @staticmethod |
| 136 | + def validate_params(endpoint, all_arguments): |
| 137 | + ''' |
| 138 | + Checks that all the required parameters are supplied. Raises an |
| 139 | + exception otherwise. |
| 140 | +
|
| 141 | + :param endpoint: dict. Endpoint description |
| 142 | + :param all_arguments: Parameter values |
| 143 | +
|
| 144 | + :rtype: None |
| 145 | + ''' |
| 146 | + required_params = ( |
| 147 | + BaseHttpClient.get_params_from_url_template(endpoint['url_template']) + |
| 148 | + [i['name'] for i in endpoint['headers'] if i['required']] + |
| 149 | + [i['name'] for i in endpoint['query_string'] if i['required']] |
| 150 | + ) |
| 151 | + |
| 152 | + missing = [p for p in required_params if p not in all_arguments] |
| 153 | + |
| 154 | + if missing: |
| 155 | + raise MissingParametersException( |
| 156 | + 'The following required parameters are missing: {missing}' |
| 157 | + .format(missing=', '.join(missing)) |
| 158 | + ) |
0 commit comments