luckywood.async_http_call   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A AsyncHttpCall.start() 0 14 1
B AsyncHttpCall.thread_call() 0 32 8
1
"""
2
Class to create an http call without waiting of a response.
3
"""
4
5
__author__ = "Frederik Glücks"
6
__email__ = "[email protected]"
7
__copyright__ = "Frederik Glücks - Glücks GmbH"
8
9
# Generic/Built-in Imports
10
import threading
11
from typing import Dict
12
13
# External libs/modules
14
import requests
15
16
17
class AsyncHttpCall:
18
    """
19
    Class to create an http call without waiting of a response.
20
    """
21
22
    @staticmethod
23
    def start(url: str, method: str = "GET", payload: str = "", bearer_token: str = ""):
24
        """
25
        Starts a thread that calls a http call without waiting for a response
26
27
        :param url: str
28
        :param method: str
29
        :param payload: json
30
        :param bearer_token: str
31
        :return: None
32
        """
33
        threading.Thread(target=AsyncHttpCall.thread_call,
34
                         args=(url, method.upper(), payload, bearer_token)
35
                         ).start()
36
37
    @staticmethod
38
    def thread_call(url: str, method: str, payload: str, bearer_token: str):
39
        """
40
        Runs the http call by http method
41
42
        :param url: str
43
        :param method: str
44
        :param payload: str
45
        :param bearer_token: str
46
        :return: None
47
        """
48
49
        if bearer_token != "":
50
            headers: Dict[str, str] = {"Authorization": "Bearer " + bearer_token}
51
        else:
52
            headers: Dict[str, str] = {}
53
54
        if method == 'GET':
55
            requests.get(url, data=payload, headers=headers)
56
57
            if payload != {}:
58
                raise Warning("HTTP method get should not have a payload!")
59
        elif method == 'PUT':
60
            requests.put(url, data=payload, headers=headers)
61
        elif method == 'POST':
62
            requests.post(url, data=payload, headers=headers)
63
        elif method == 'DELETE':
64
            requests.delete(url, headers=headers)
65
        elif method == 'OPTIONS':
66
            requests.options(url, headers=headers)
67
        else:
68
            raise RuntimeError("unknown http method {}".format(method))
69