saucenao.http   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 8
eloc 27
dl 0
loc 42
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B verify_status_code() 0 27 8
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4 1
import requests
5
6 1
from saucenao.exceptions import *
7
8 1
PREVIOUS_STATUS_CODE = None
9
10 1
STATUS_CODE_OK = 1
11 1
STATUS_CODE_SKIP = 2
12 1
STATUS_CODE_REPEAT = 3
13
14
15 1
def verify_status_code(request_response: requests.Response) -> tuple:
16
    """Verify the status code of the post request to the search url and raise exceptions if the code is unexpected
17
18
    :type request_response: requests.Response
19
    :return:
20
    """
21 1
    if request_response.status_code == 200:
22 1
        return STATUS_CODE_OK, ''
23
24 1
    elif request_response.status_code == 429:
25 1
        if 'user\'s rate limit' in request_response.text:
26
            msg = "Search rate limit reached"
27
            return STATUS_CODE_REPEAT, msg
28 1
        if 'limit of 150 searches' in request_response.text:
29 1
            raise DailyLimitReachedException('Daily search limit for unregistered users reached')
30 1
        elif 'limit of 300 searches' in request_response.text:
31 1
            raise DailyLimitReachedException('Daily search limit for basic users reached')
32
        else:
33 1
            raise DailyLimitReachedException('Daily search limit reached')
34 1
    elif request_response.status_code == 403:
35 1
        raise InvalidOrWrongApiKeyException("Invalid or wrong API key")
36 1
    elif request_response.status_code == 413:
37 1
        msg = "Payload too large, skipping file"
38 1
        return STATUS_CODE_SKIP, msg
39
    else:
40 1
        msg = "Unknown status code: {0:d}".format(request_response.status_code)
41
        return STATUS_CODE_REPEAT, msg
42