Passed
Pull Request — master (#27)
by
unknown
01:13
created

nextcloud.response.WebDAVResponse.__init__()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nop 3
1
from .api_wrappers.webdav import WebDAVStatusCodes
2
3
4
class NextCloudResponse(object):
5
6
    def __init__(self, response, json_output=True, data=None):
7
        self.raw = response
8
        if not data:
9
            self.data = response.json() if json_output else response.content.decode("UTF-8")
10
        else:
11
            self.data = data
12
13
14
class WebDAVResponse(NextCloudResponse):
15
    """ Response class for WebDAV api methods """
16
17
    METHODS_SUCCESS_CODES = {
18
        "PROPFIND": [WebDAVStatusCodes.MULTISTATUS_CODE],
19
        "PROPPATCH": [WebDAVStatusCodes.MULTISTATUS_CODE],
20
        "REPORT": [WebDAVStatusCodes.MULTISTATUS_CODE],
21
        "MKCOL": [WebDAVStatusCodes.CREATED_CODE],
22
        "COPY": [WebDAVStatusCodes.CREATED_CODE, WebDAVStatusCodes.NO_CONTENT_CODE],
23
        "MOVE": [WebDAVStatusCodes.CREATED_CODE, WebDAVStatusCodes.NO_CONTENT_CODE],
24
        "PUT": [WebDAVStatusCodes.CREATED_CODE],
25
        "DELETE": [WebDAVStatusCodes.NO_CONTENT_CODE]
26
    }
27
28
    def __init__(self, response, data=None):
29
        super(WebDAVResponse, self).__init__(response=response, data=data, json_output=False)
30
        request_method = response.request.method
31
        self.is_ok = False
32
        if request_method in self.METHODS_SUCCESS_CODES:
33
            self.is_ok = response.status_code in self.METHODS_SUCCESS_CODES[request_method]
34
35
    def __repr__(self):
36
        is_ok_str = "OK" if self.is_ok else "Failed"
37
        return "<OCSResponse: Status: {}>".format(is_ok_str)
38
39
40
class OCSResponse(NextCloudResponse):
41
    """ Response class for OCS api methods """
42
43
    def __init__(self, response, json_output=True, success_code=None):
44
        self.raw = response
45
        self.is_ok = None
46
        if json_output:
47
            self.full_data = response.json()
48
            self.meta = self.full_data['ocs']['meta']
49
            self.status_code = self.full_data['ocs']['meta']['statuscode']
50
            self.data = self.full_data['ocs']['data']
51
            if success_code:
52
                self.is_ok = self.full_data['ocs']['meta']['statuscode'] == success_code
53
54
        else:
55
            self.data = response.content.decode("UTF-8")
56
57
    def __repr__(self):
58
        is_ok_str = "OK" if self.is_ok else "Failed"
59
        return "<OCSResponse: Status: {}>".format(is_ok_str)
60