Passed
Push — master ( 2adcb6...75d235 )
by Markus
01:50
created

tcllib.requests.runner.RequestRunner.get_http()   A

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
nop 2
1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
3
from .tclrequest import TclRequest
4
from . import http
5
from . import serverselector
6
7
class UnknownMethodException(Exception):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
8
    pass
9
10
class RequestRunner:
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
11
    def __init__(self, server_selector: serverselector.ServerSelector, https=True):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable serverselector does not seem to be defined.
Loading history...
12
        self.server_selector = server_selector
13
        self.protocol = "https://" if https else "http://"
14
        self.max_tries = 5
15
16
    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...
17
        """Returns the http class according to desired method."""
18
        if method == "GET":
19
            return http.HttpRequest
20
        elif method == "POST":
21
            return http.HttpPostRequest
22
        raise UnknownMethodException("Unknown http method: {}".format(method))
23
24
    def get_server(self) -> str:
25
        """Returns a master server."""
26
        return self.server_selector.get_master_server()
27
28
    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...
29
        """Runs the actual query."""
30
        for _ in range(0, self.max_tries):
31
            url = "{}{}{}".format(self.protocol, self.get_server(), query.uri)
32
            http_handler = self.get_http(query.method)(url, timeout)
33
            http_handler.headers = query.get_headers()
34
            http_handler.params = query.get_params()
35
            http_handler.reset_session()
36
            self.server_selector.hook_prerequest()
37
            try:
38
                req = http_handler.run()
39
                if query.rawmode:
40
                    done = query.is_done(req.status_code, req.content)
41
                else:
42
                    req.encoding = "utf-8"
43
                    done = query.is_done(req.status_code, req.text)
44
                self.server_selector.hook_postrequest(done)
45
                if done:
46
                    return done
47
            except http.TimeoutException:
48
                self.server_selector.hook_postrequest(False)
49
                query.error = "Timeout."
50
        query.error = "Max tries ({}) reached.".format(self.max_tries)
51
        return False
52