tcllib.requests.runner.RequestRunner.run()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 24
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 24
rs 8.8853
c 0
b 0
f 0
cc 5
nop 3
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""Generic request executors."""
5
6
from . import http, serverselector
7
from .tclrequest import TclRequest
8
9
10
class UnknownMethodException(Exception):
11
    """Ignore unknown methods."""
12
    pass
13
14
15
class RequestRunner:
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
16
    """Generic request executor."""
17
18
    def __init__(self, server_selector: serverselector.ServerSelector, https=True):
19
        """Populate variables."""
20
        self.server_selector = server_selector
21
        self.protocol = "https://" if https else "http://"
22
        self.max_tries = 5
23
24
    def get_http(self, method="GET") -> http.HttpRequest:
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
25
        """Return the http class according to desired method."""
26
        if method == "GET":
27
            return http.HttpRequest
28
        elif method == "POST":
29
            return http.HttpPostRequest
30
        raise UnknownMethodException("Unknown http method: {}".format(method))
31
32
    def get_server(self) -> str:
33
        """Return a master server."""
34
        return self.server_selector.get_master_server()
35
36
    def run(self, query: TclRequest, timeout: int=10) -> bool:
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
37
        """Run the actual query."""
38
        for _ in range(0, self.max_tries):
39
            url = "{}{}{}".format(self.protocol, self.get_server(), query.uri)
40
            http_handler = self.get_http(query.method)(url, timeout)
41
            http_handler.headers = query.get_headers()
42
            http_handler.params = query.get_params()
43
            http_handler.reset_session()
44
            self.server_selector.hook_prerequest()
45
            try:
46
                req = http_handler.run()
47
                if query.rawmode:
48
                    done = query.is_done(req.status_code, req.content)
49
                else:
50
                    req.encoding = "utf-8"
51
                    done = query.is_done(req.status_code, req.text)
52
                self.server_selector.hook_postrequest(done)
53
                if done:
54
                    return done
55
            except http.TimeoutException:
56
                self.server_selector.hook_postrequest(False)
57
                query.error = "Timeout."
58
        query.error = "Max tries ({}) reached.".format(self.max_tries)
59
        return False
60