|
| 1 | +import os |
| 2 | +import json, requests |
| 3 | + |
| 4 | +from .. import API_VERSION, MLJAR_ENDPOINT |
| 5 | +from ..exceptions import MljarException, TokenException, DataReadException, BadRequestException |
| 6 | +from ..exceptions import JSONReadException, NotFoundException, AuthenticationException |
| 7 | + |
| 8 | + |
| 9 | +from ..log import logger |
| 10 | + |
| 11 | +class MljarHttpClient(object): |
| 12 | + ''' |
| 13 | + Mljar Client for HTTP Requests. |
| 14 | + ''' |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + self.TOKEN = os.environ.get('MLJAR_TOKEN', None) |
| 18 | + if not self.TOKEN: |
| 19 | + raise TokenException('Please define environment variable MLJAR_TOKEN. \ |
| 20 | + You can get you MLJAR token by login to mljar.com account. \ |
| 21 | + It is available in your settings.') |
| 22 | + |
| 23 | + self.base_url = '/'.join([MLJAR_ENDPOINT, API_VERSION]) |
| 24 | + |
| 25 | + def request(self, method, url, data=None, with_header=True, url_outside_mljar=False, parse_json=True): |
| 26 | + """ |
| 27 | + Execute the request using requests library. |
| 28 | + """ |
| 29 | + if url_outside_mljar: |
| 30 | + request_url = url |
| 31 | + else: |
| 32 | + request_url = self.base_url + url |
| 33 | + logger.debug("Starting request to url: {} with data: {}".format(request_url, data)) |
| 34 | + |
| 35 | + headers = {'Authorization': 'Token '+self.TOKEN } |
| 36 | + if with_header: |
| 37 | + response = requests.request(method, request_url, headers=headers, data=data) |
| 38 | + else: |
| 39 | + response = requests.request(method, request_url, data=data) |
| 40 | + |
| 41 | + if parse_json: |
| 42 | + try: |
| 43 | + if response.status_code != 204: |
| 44 | + logger.debug("Response content: {}, headers: {}".format(response.json(), response.headers)) |
| 45 | + except Exception as e: |
| 46 | + logger.error("Request failed: {} {}".format(response.content, str(e))) |
| 47 | + self._check_response_status(response) |
| 48 | + return response |
| 49 | + |
| 50 | + def _check_response_status(self, response): |
| 51 | + """ |
| 52 | + Check if response is successful else raise Exception. |
| 53 | + """ |
| 54 | + if not (200 <= response.status_code < 300): |
| 55 | + try: |
| 56 | + message = response.json()["errors"] |
| 57 | + except Exception: |
| 58 | + message = None |
| 59 | + logger.debug("Error received : status_code: {}, message: {}".format(response.status_code, |
| 60 | + message or response.content)) |
| 61 | + |
| 62 | + if response.status_code == 401: |
| 63 | + raise AuthenticationException() |
| 64 | + elif response.status_code == 404: |
| 65 | + raise NotFoundException() |
| 66 | + elif response.status_code == 400: |
| 67 | + raise BadRequestException() |
| 68 | + else: |
| 69 | + response.raise_for_status() |
| 70 | + |
| 71 | + |
| 72 | +''' |
| 73 | +def _get_data(self, response): |
| 74 | + if response is None: |
| 75 | + return None |
| 76 | +
|
| 77 | + try: |
| 78 | + data = response.json() |
| 79 | + except ValueError as e: |
| 80 | + raise JSONReadException('Get data failed, %s' % str(e) ) |
| 81 | +
|
| 82 | + if not response.ok: |
| 83 | + msg = [data[m] for m in ("id", "message") if m in data][1] |
| 84 | + raise DataReadException(msg) |
| 85 | +
|
| 86 | + return data |
| 87 | +''' |
| 88 | + |
| 89 | +''' |
| 90 | +
|
| 91 | + self._urls = { |
| 92 | + 'project': '/'.join([self.API_ENDPOINT, API_VERSION, 'projects']), |
| 93 | + 'dataset': '/'.join([self.API_ENDPOINT, API_VERSION, 'datasets']), |
| 94 | + 'experiment': '/'.join([self.API_ENDPOINT, API_VERSION, 'experiments']), |
| 95 | + 'result': '/'.join([self.API_ENDPOINT, API_VERSION, 'results/']), |
| 96 | + 'predict': '/'.join([self.API_ENDPOINT, API_VERSION, 'predict/']), |
| 97 | + 'predictions': '/'.join([self.API_ENDPOINT, API_VERSION, 'predictions']), # it is not a bug, we don't need here '/' |
| 98 | + 'download_prediction': '/'.join([self.API_ENDPOINT, API_VERSION, 'download/prediction/']), |
| 99 | + 's3policy': '/'.join([self.API_ENDPOINT, API_VERSION, 's3policy/']), |
| 100 | + 'accept_column_usage': '/'.join([self.API_ENDPOINT, API_VERSION, 'accept_column_usage/']), |
| 101 | + } |
| 102 | +
|
| 103 | +
|
| 104 | + def _make_request(self, url_name = '', custom_url = '', request_type = 'get', url_additional = '', input_json = {}, with_header = True): |
| 105 | + try: |
| 106 | + response = None |
| 107 | + headers = {'Authorization': 'Token '+self.TOKEN } #'Content-Type': 'application/json' |
| 108 | + my_url = '' |
| 109 | + if url_name in self._urls: |
| 110 | + if my_url == '': |
| 111 | + raise Exception('Wrong URL address') |
| 112 | + if url_additional != '': |
| 113 | + my_url += url_additional |
| 114 | +
|
| 115 | + #print 'request', my_url, request_type, input_json, with_header, headers |
| 116 | +
|
| 117 | + if request_type == 'get': |
| 118 | + response = requests.get(my_url, headers=headers) |
| 119 | + elif request_type == 'post': |
| 120 | + if with_header: |
| 121 | + response = requests.post(my_url, data=input_json, headers=headers) |
| 122 | + else: |
| 123 | + response = requests.post(my_url, data=input_json) |
| 124 | + elif request_type == 'put': |
| 125 | + if with_header: |
| 126 | + print my_url, 'with header', input_json |
| 127 | + response = requests.put(my_url, data=input_json, headers=headers) |
| 128 | + else: |
| 129 | + response = requests.put(my_url, data=input_json) |
| 130 | +
|
| 131 | + except MljarException as e: |
| 132 | + print 'There was an error during API call, %s' % str(e) |
| 133 | + finally: |
| 134 | + return response |
| 135 | +''' |
0 commit comments