Passed
Push — master ( 7c4ebb...fe8df7 )
by Steffen
02:45
created

saucenao.http.verify_status_code()   B

Complexity

Conditions 8

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8.0747

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 28
ccs 17
cts 19
cp 0.8947
rs 7.3333
c 0
b 0
f 0
cc 8
nop 2
crap 8.0747
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3 1
import time
4
5 1
import requests
6
7 1
from saucenao.exceptions import *
8
9 1
PREVIOUS_STATUS_CODE = None
10
11 1
STATUS_CODE_OK = 1
12 1
STATUS_CODE_SKIP = 2
13 1
STATUS_CODE_REPEAT = 3
14
15
16 1
def verify_status_code(request_response: requests.Response, file_name: str) -> tuple:
17
    """Verify the status code of the post request to the search url and raise exceptions if the code is unexpected
18
19
    :type request_response: requests.Response
20
    :type file_name: str
21
    :return:
22
    """
23 1
    if request_response.status_code == 200:
24 1
        return STATUS_CODE_OK, ''
25
26 1
    elif request_response.status_code == 429:
27 1
        if 'Search Rate Too High.' in request_response.text:
28
            msg = "Search rate limit reached"
29
            return STATUS_CODE_REPEAT, msg
30 1
        if 'limit of 150 searches' in request_response.text:
31 1
            raise DailyLimitReachedException('Daily search limit for unregistered users reached')
32 1
        elif 'limit of 300 searches' in request_response.text:
33 1
            raise DailyLimitReachedException('Daily search limit for basic users reached')
34
        else:
35 1
            raise DailyLimitReachedException('Daily search limit reached')
36 1
    elif request_response.status_code == 403:
37 1
        raise InvalidOrWrongApiKeyException("Invalid or wrong API key")
38 1
    elif request_response.status_code == 413:
39 1
        msg = "Payload too large, skipping file: {0:s}".format(file_name)
40 1
        return STATUS_CODE_SKIP, msg
41
    else:
42 1
        msg = "Unknown status code: {0:d}".format(request_response.status_code)
43
        return STATUS_CODE_REPEAT, msg
44