def _do_request(self, method: str = GET, url: str = "/", params: dict = None, json: dict = None, headers: dict = None) -> list or dict or None: """ Executes a http request based on the given parameters :param method: the method to use (GET, POST) :param url: the url to use :param params: query parameters that will be appended to the url :param json: request body :param headers: custom headers :return: the response parsed as a json """ access_token = self.get_token() _headers = {'Authorization': 'Bearer {}'.format(access_token)} if headers is not None: _headers.update(headers) url = create_request_url(url, params) if method is GET: response = requests.get(url, headers=_headers, json=json) elif method is POST: response = requests.post(url, headers=_headers, json=json) else: raise ValueError("Unsupported method: {}".format(method)) response.raise_for_status() # some responses do not return data so we just ignore the body in that case if len(response.content) > 0: return response.json()
def test_create_request_url(self): from n26.util import create_request_url expected = "https://api.tech26.de?bar=baz&foo=bar" result = create_request_url(BASE_URL_DE, {"foo": "bar", "bar": "baz"}) self.assertEqual(result, expected)