|
| 1 | +""" |
| 2 | +Define an API connection object |
| 3 | +""" |
| 4 | +import logging |
| 5 | + |
| 6 | +import requests |
| 7 | +import six |
| 8 | + |
| 9 | +from reposit.data.exceptions import InvalidControllerException |
| 10 | +from reposit.data.utils import is_valid_url |
| 11 | +from reposit.settings import BASE_URL |
| 12 | + |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class ApiRequest(object): |
| 18 | + """ |
| 19 | + A class which represents a request/response from the Reposit API. |
| 20 | +
|
| 21 | + Why a class and not a function? |
| 22 | + well we can do some comprehensive validation and checking on creation |
| 23 | + """ |
| 24 | + def __str__(self): |
| 25 | + """ |
| 26 | + This should give us a good idea (for debugging) |
| 27 | + exactly the request and what data we wanted. |
| 28 | + We don't want to log the controller object as this |
| 29 | + will leak the access token. |
| 30 | + :return: |
| 31 | + """ |
| 32 | + return self.url, self.schema |
| 33 | + |
| 34 | + def __init__(self, path, controller, schema, **kwargs): |
| 35 | + """ |
| 36 | + :param path: the url endpoint (e.g. /v2/deployments etc. etc. |
| 37 | + :param controller: a Controller instance |
| 38 | + :param schema: A dict representing the structure of the response. |
| 39 | + E.g. we want houseP and the API returns the following: |
| 40 | + { |
| 41 | + "data": { |
| 42 | + "houseP": {'blah'} |
| 43 | + } |
| 44 | + } |
| 45 | + So the schema in this case should be a dict like so: |
| 46 | + { |
| 47 | + "data": { |
| 48 | + "houseP": {} |
| 49 | + } |
| 50 | + } |
| 51 | + This is because some of the API responses vary in structure, so |
| 52 | + we can define them on the fly easily :) |
| 53 | +
|
| 54 | + :param kwargs: additional arguments when requesting data |
| 55 | + """ |
| 56 | + if path.startswith('/'): |
| 57 | + self.url = '{}{}'.format(BASE_URL, path) |
| 58 | + else: |
| 59 | + self.url = '{}/{}'.format(BASE_URL, path) |
| 60 | + |
| 61 | + assert is_valid_url(self.url) |
| 62 | + |
| 63 | + if not controller.auth_headers: |
| 64 | + raise InvalidControllerException |
| 65 | + self.controller = controller |
| 66 | + |
| 67 | + # a lookup of the response schema |
| 68 | + self.schema = schema |
| 69 | + """ |
| 70 | + Various optional args here |
| 71 | + """ |
| 72 | + # if the response a list? |
| 73 | + self.is_list = kwargs.get('format_list', False) |
| 74 | + |
| 75 | + def get(self): |
| 76 | + """ |
| 77 | + Once a connection is defined as valid, then a request can be |
| 78 | + made. This formats the response as well as checks the response |
| 79 | + returns an OK http code |
| 80 | + :return: |
| 81 | + """ |
| 82 | + resp = requests.get( |
| 83 | + self.url, |
| 84 | + headers=self.controller.auth_headers |
| 85 | + ) |
| 86 | + try: |
| 87 | + resp.raise_for_status() |
| 88 | + except Exception as ex: |
| 89 | + # Hijack the exception to log the exact issue. |
| 90 | + logger.exception('Error retrieving data: {}'.format(self)) |
| 91 | + raise ex |
| 92 | + |
| 93 | + data = self._simple_format_for_fields(resp) |
| 94 | + return data |
| 95 | + |
| 96 | + def _simple_format_for_fields(self, api_response): |
| 97 | + """ |
| 98 | + Based on the schema provided, format the data accordingly. |
| 99 | +
|
| 100 | + :return: |
| 101 | + """ |
| 102 | + |
| 103 | + data = api_response.json() |
| 104 | + data_for_retrieval = [] |
| 105 | + schema_items = self.schema.items() if six.PY3 else self.schema.iteritems() |
| 106 | + for key, _ in schema_items: |
| 107 | + fetched_data = data.get(key) |
| 108 | + if fetched_data: |
| 109 | + data_for_retrieval.append(fetched_data) |
| 110 | + del data[key] |
| 111 | + |
| 112 | + # if its a list of only one thing then simplify it a smidge. |
| 113 | + # Most routes tend to only be. |
| 114 | + if len(data_for_retrieval) == 1: |
| 115 | + return data_for_retrieval[0] |
| 116 | + |
| 117 | + return data_for_retrieval |
0 commit comments