|
| 1 | +""" |
| 2 | +Copyright 2020 ShipChain, Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +import json |
| 18 | +import logging |
| 19 | + |
| 20 | +import requests |
| 21 | +from aws_requests_auth.boto_utils import BotoAWSRequestsAuth |
| 22 | +from django.conf import settings |
| 23 | +from influxdb_metrics.loader import log_metric, TimingMetric |
| 24 | +from rest_framework import status |
| 25 | + |
| 26 | +from .exceptions import AWSIoTError |
| 27 | +from .utils import DecimalEncoder |
| 28 | + |
| 29 | +LOG = logging.getLogger('python-common') |
| 30 | + |
| 31 | + |
| 32 | +class AWSClient: |
| 33 | + @property |
| 34 | + def url(self): |
| 35 | + raise NotImplementedError |
| 36 | + |
| 37 | + @property |
| 38 | + def session(self): |
| 39 | + raise NotImplementedError |
| 40 | + |
| 41 | + METHOD_POST = 'post' |
| 42 | + METHOD_PUT = 'put' |
| 43 | + METHOD_GET = 'get' |
| 44 | + METHOD_DELETE = 'delete' |
| 45 | + |
| 46 | + RESPONSE_200_METHODS = [METHOD_PUT, METHOD_GET, METHOD_DELETE] |
| 47 | + |
| 48 | + def _call(self, http_method, endpoint, payload=None, params=None): |
| 49 | + metric_name = self._get_generic_endpoint_for_metric(http_method, endpoint) |
| 50 | + calling_url = f'{self.url}/{endpoint}' |
| 51 | + |
| 52 | + if payload: |
| 53 | + payload = json.dumps(payload, cls=DecimalEncoder) |
| 54 | + |
| 55 | + try: |
| 56 | + |
| 57 | + with TimingMetric('python_common_aws.call', tags={'method': metric_name}) as timer: |
| 58 | + |
| 59 | + if http_method == self.METHOD_POST: |
| 60 | + response = self.session.post(calling_url, data=payload, params=params) |
| 61 | + response_json = response.json() |
| 62 | + |
| 63 | + if response.status_code != status.HTTP_201_CREATED: |
| 64 | + self._process_error_object(metric_name, response, response_json) |
| 65 | + |
| 66 | + elif http_method in self.RESPONSE_200_METHODS: |
| 67 | + response = getattr(self.session, http_method)(calling_url, data=payload, params=params) |
| 68 | + response_json = response.json() |
| 69 | + |
| 70 | + if response.status_code != status.HTTP_200_OK: |
| 71 | + self._process_error_object(metric_name, response, response_json) |
| 72 | + |
| 73 | + else: |
| 74 | + log_metric('python_common_aws.error', tags={'method': metric_name, 'code': 'InvalidHTTPMethod'}) |
| 75 | + LOG.error('aws_client(%s) error: %s', metric_name, 'Invalid HTTP Method') |
| 76 | + raise AWSIoTError(f'Invalid HTTP Method {http_method}') |
| 77 | + |
| 78 | + LOG.info('aws_client(%s) duration: %.3f', metric_name, timer.elapsed) |
| 79 | + |
| 80 | + except requests.exceptions.ConnectionError: |
| 81 | + log_metric('python_common_aws.error', tags={'method': metric_name, 'code': 'ConnectionError'}) |
| 82 | + raise AWSIoTError("Service temporarily unavailable, try again later", status.HTTP_503_SERVICE_UNAVAILABLE, |
| 83 | + 'service_unavailable') |
| 84 | + |
| 85 | + except Exception as exception: |
| 86 | + log_metric('python_common_aws.error', tags={'method': metric_name, 'code': 'exception'}) |
| 87 | + raise AWSIoTError(str(exception)) |
| 88 | + |
| 89 | + return response_json |
| 90 | + |
| 91 | + def _post(self, endpoint='', payload=None, query_params=None): |
| 92 | + return self._call(self.METHOD_POST, endpoint, payload, params=query_params) |
| 93 | + |
| 94 | + def _put(self, endpoint='', payload=None, query_params=None): |
| 95 | + return self._call(self.METHOD_PUT, endpoint, payload, params=query_params) |
| 96 | + |
| 97 | + def _get(self, endpoint='', query_params=None): |
| 98 | + return self._call(self.METHOD_GET, endpoint, params=query_params) |
| 99 | + |
| 100 | + def _delete(self, endpoint='', query_params=None): |
| 101 | + return self._call(self.METHOD_DELETE, endpoint, params=query_params) |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def _process_error_object(endpoint, response, response_json): |
| 105 | + error_code = response.status_code |
| 106 | + |
| 107 | + if 'error' in response_json: |
| 108 | + message = response_json['error'] |
| 109 | + if isinstance(message, dict): |
| 110 | + if 'code' in message: |
| 111 | + error_code = message['code'] |
| 112 | + if 'message' in message: |
| 113 | + message = message['message'] |
| 114 | + |
| 115 | + elif 'message' in response_json: |
| 116 | + message = response_json['message'] |
| 117 | + |
| 118 | + else: |
| 119 | + message = response_json |
| 120 | + |
| 121 | + log_metric('python_common_aws.error', tags={'method': endpoint, 'code': error_code}) |
| 122 | + LOG.error('aws_client(%s) error: %s', endpoint, message) |
| 123 | + raise AWSIoTError(f'Error in AWS IoT Request: [{error_code}] {message}') |
| 124 | + |
| 125 | + def _get_generic_endpoint_for_metric(self, http_method, endpoint): |
| 126 | + # This should be overwritten by each usage of this class for added clarity. |
| 127 | + return f'{http_method}::{endpoint}' |
| 128 | + |
| 129 | + |
| 130 | +class URLShortenerClient(AWSClient): |
| 131 | + url = settings.URL_SHORTENER_URL |
| 132 | + session = requests.session() |
| 133 | + |
| 134 | + def __init__(self): |
| 135 | + aws_auth = BotoAWSRequestsAuth( |
| 136 | + aws_host=settings.URL_SHORTENER_HOST, |
| 137 | + aws_region='us-east-1', |
| 138 | + aws_service='execute-api' |
| 139 | + ) |
| 140 | + |
| 141 | + self.session.headers = {'content-type': 'application/json'} |
| 142 | + self.session.auth = aws_auth |
| 143 | + |
| 144 | + def _get_generic_endpoint_for_metric(self, http_method, endpoint): |
| 145 | + return f'urlshortener::{http_method}::{endpoint}' |
0 commit comments