Test Failed
Push — master ( 16370d...2bdc04 )
by Steffen
02:30
created

saucenao.http.verify_status_code()   B

Complexity

Conditions 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 25
rs 8
c 0
b 0
f 0
cc 7
nop 2
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
import requests
5
6
from saucenao.exceptions import *
7
8
PREVIOUS_STATUS_CODE = None
9
10
STATUS_CODE_OK = 1
11
STATUS_CODE_SKIP = 2
12
STATUS_CODE_REPEAT = 3
13
14
15
def verify_status_code(request_response: requests.Response, file_name: str) -> 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
    :type file_name: str
20
    :return:
21
    """
22
    if request_response.status_code == 200:
23
        return STATUS_CODE_OK, ''
24
25
    elif request_response.status_code == 429:
26
        if 'limit of 150 searches' in request_response.text:
27
            raise DailyLimitReachedException('Daily search limit for unregistered users reached')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable DailyLimitReachedException does not seem to be defined.
Loading history...
28
        elif 'limit of 300 searches' in request_response.text:
29
            raise DailyLimitReachedException('Daily search limit for basic users reached')
30
        else:
31
            raise DailyLimitReachedException('Daily search limit reached')
32
    elif request_response.status_code == 403:
33
        raise InvalidOrWrongApiKeyException("Invalid or wrong API key")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable InvalidOrWrongApiKeyException does not seem to be defined.
Loading history...
34
    elif request_response.status_code == 413:
35
        msg = "Payload too large, skipping file: {0:s}".format(file_name)
36
        return STATUS_CODE_SKIP, msg
37
    else:
38
        msg = "Unknown status code: {0:d}".format(request_response.status_code)
39
        return STATUS_CODE_REPEAT, msg
40