tcllib.requests.http   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 27
dl 0
loc 49
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A HttpRequest.reset_session() 0 4 1
A HttpPostRequest.run() 0 7 2
A HttpRequest.__init__() 0 5 1
A HttpRequest.run() 0 7 2
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""Base HTTP requests."""
5
6
from collections import OrderedDict
7
8
import requests
9
10
11
class TimeoutException(Exception):
12
    """Ignore timeouts."""
13
    pass
14
15
16
class HttpRequest:
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
17
    """Provides all generic features for making HTTP GET requests"""
18
19
    def __init__(self, url, timeout=10):
20
        self.url = url
21
        self.params = OrderedDict()
22
        self.timeout = timeout
23
        self.headers = {}
24
25
    def reset_session(self):
26
        """Reset everything to default."""
27
        self.sess = requests.Session()
0 ignored issues
show
Coding Style introduced by
The attribute sess was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
28
        self.sess.headers.update(self.headers)
29
30
    def run(self):
31
        """Run query."""
32
        try:
33
            req = self.sess.get(self.url, params=self.params, timeout=self.timeout)
34
        except requests.exceptions.Timeout as exc:
35
            raise TimeoutException(exc)
36
        return req
37
38
39
class HttpPostRequest(HttpRequest):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
40
    """Provides all generic features for making HTTP POST requests"""
41
42
    def run(self):
43
        """Run query."""
44
        try:
45
            req = self.sess.post(self.url, data=self.params, timeout=self.timeout)
46
        except requests.exceptions.Timeout as exc:
47
            raise TimeoutException(exc)
48
        return req
49