:param url: :param headers: :param param: :param body: :param method: :return:
(url, headers, param=None, body=None, method="POST")
| 24 | class HttpRequest(object): |
| 25 | @staticmethod |
| 26 | def request(url, headers, param=None, body=None, method="POST"): |
| 27 | """ |
| 28 | |
| 29 | :param url: |
| 30 | :param headers: |
| 31 | :param param: |
| 32 | :param body: |
| 33 | :param method: |
| 34 | :return: |
| 35 | """ |
| 36 | if param: |
| 37 | url += "?" + param |
| 38 | if body and isinstance(body, str): |
| 39 | body = body.encode("utf-8") |
| 40 | |
| 41 | try: |
| 42 | request = Request(url=url, data=body, headers=headers) |
| 43 | request.get_method = lambda: method.upper() |
| 44 | response = urlopen(request, context=ssl._create_unverified_context()) |
| 45 | return response |
| 46 | except HTTPError as err: |
| 47 | error_msg = err.read().decode('utf-8') |
| 48 | logger.error(f"api request error: {error_msg}\nurl: {url}") |
| 49 | raise |
| 50 | |
| 51 | |
| 52 | class HttpClient(object): |
no test coverage detected