Passed
Push — master ( 3059ff...eef0f7 )
by John
01:52
created

CheckRequest.is_done()   B

Complexity

Conditions 4

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
nop 3
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""Generic update check request."""
5
6
from collections import OrderedDict
7
8
from .. import devices
9
from .tclrequest import TclRequest
10
from .tclresult import CheckResult
11
12
13
class CheckRequest(TclRequest):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
14
    """Generic update check request."""
15
16
    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...
17
        """Populate variables.."""
18
        super().__init__()
19
        self.uri = "/check.php"
20
        self.method = "GET"
21
        self.device = device
22
23
    def get_headers(self):
24
        """Return request headers."""
25
        return {"User-Agent": self.device.uagent}
26
27
    def get_params(self):
28
        """Return request parameters."""
29
        params = OrderedDict()
30
        params["id"] = self.device.imei
31
        params["curef"] = self.device.curef
32
        params["fv"] = self.device.fwver
33
        params["mode"] = self.device.mode
34
        params["type"] = self.device.type
35
        params["cltp"] = self.device.cltp
36
        params["cktp"] = self.device.cktp
37
        params["rtd"] = self.device.rtd
38
        params["chnl"] = self.device.chnl
39
        #params["osvs"] = self.device.osvs
40
        #params["ckot"] = self.device.ckot
41
        return params
42
43
    def is_done(self, http_status: int, contents: str) -> bool:
44
        """Handle request result."""
45
        ok_states = {
46
            204: "No update available.",
47
            404: "No data for requested CUREF/FV combination.",
48
        }
49
        if http_status == 200:
50
            self.response = contents
51
            self.result = CheckResult(contents)
52
            self.success = True
53
            return True
54
        elif http_status in ok_states:
55
            self.error = ok_states[http_status]
56
            self.success = False
57
            return True
58
        elif http_status not in [500, 502, 503]:
59
            # Errors OTHER than 500, 502 or 503 are probably
60
            # errors where we don't need to retry
61
            self.error = "HTTP {}.".format(http_status)
62
            self.success = False
63
            return True
64
        return False
65
66
# Check requests have 4 possible outcomes:
67
# 1. HTTP 200 with XML data - our desired info
68
# 2. HTTP 204 - means: no newer update available
69
# 3. HTTP 404 - means: invalid device or firmware version
70
# 4. anything else: server problem (esp. 500, 502, 503)
71