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

tcllib.requests.checkrequest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 43
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A CheckRequest.is_done() 0 21 4
A CheckRequest.get_params() 0 14 1
A CheckRequest.get_headers() 0 2 1
A CheckRequest.__init__() 0 5 1
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 collections import OrderedDict
4
from .. import devices
5
from .tclrequest import TclRequest
6
from .tclresult import CheckResult
7
8
class CheckRequest(TclRequest):
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...
9
    def __init__(self, device: devices.Device):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable devices does not seem to be defined.
Loading history...
10
        super().__init__()
11
        self.uri = "/check.php"
12
        self.method = "GET"
13
        self.device = device
14
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
15
    def get_headers(self):
16
        return {"User-Agent": self.device.ua}
17
18
    def get_params(self):
19
        params = OrderedDict()
20
        params["id"] = self.device.imei
21
        params["curef"] = self.device.curef
22
        params["fv"] = self.device.fwver
23
        params["mode"] = self.device.mode
24
        params["type"] = self.device.type
25
        params["cltp"] = self.device.cltp
26
        params["cktp"] = self.device.cktp
27
        params["rtd"] = self.device.rtd
28
        params["chnl"] = self.device.chnl
29
        #params["osvs"] = self.device.osvs
30
        #params["ckot"] = self.device.ckot
31
        return params
32
33
    def is_done(self, http_status: int, contents: str) -> bool:
34
        ok_states = {
35
            204: "No update available.",
36
            404: "No data for requested CUREF/FV combination.",
37
        }
38
        if http_status == 200:
39
            self.response = contents
40
            self.result = CheckResult(contents)
41
            self.success = True
42
            return True
43
        elif http_status in ok_states:
44
            self.error = ok_states[http_status]
45
            self.success = False
46
            return True
47
        elif http_status not in [500, 502, 503]:
48
            # Errors OTHER than 500, 502 or 503 are probably
49
            # errors where we don't need to retry
50
            self.error ="HTTP {}.".format(http_status)
0 ignored issues
show
Coding Style introduced by
Exactly one space required after assignment
Loading history...
51
            self.success = False
52
            return True
53
        return False
54
55
# Check requests have 4 possible outcomes:
56
# 1. HTTP 200 with XML data - our desired info
57
# 2. HTTP 204 - means: no newer update available
58
# 3. HTTP 404 - means: invalid device or firmware version
59
# 4. anything else: server problem (esp. 500, 502, 503)
60