nextcloud.api_wrappers.share   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 25
eloc 64
dl 0
loc 156
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A Share.update_share() 0 32 4
B Share.create_share() 0 36 8
A Share.delete_share() 0 11 1
A Share.get_shares() 0 3 1
A Share.get_local_url() 0 4 2
A Share.validate_share_parameters() 0 21 5
A Share.get_shares_from_path() 0 22 3
A Share.get_share_info() 0 10 1
1
# -*- coding: utf-8 -*-
2
from nextcloud.base import WithRequester, ShareType
3
4
5
class Share(WithRequester):
6
    API_URL = "/ocs/v2.php/apps/files_sharing/api/v1"
7
    LOCAL = "shares"
8
    SUCCESS_CODE = 200
9
10
    def get_local_url(self, additional_url=""):
11
        if additional_url:
12
            return "/".join([self.LOCAL, additional_url])
13
        return self.LOCAL
14
15
    @staticmethod
16
    def validate_share_parameters(path, share_type, share_with):
17
        """
18
        Check if share parameters make sense
19
20
        Args:
21
            path (str): path to the file/folder which should be shared
22
            share_type (int): ShareType attribute
23
            share_with (str): user/group id with which the file should be shared
24
25
        Returns:
26
            bool: True if parameters make sense together, False otherwise
27
        """
28
        if (
29
            path is None or not isinstance(share_type, int) or (
30
                share_with is None and
31
                share_type in [ShareType.GROUP, ShareType.USER, ShareType.FEDERATED_CLOUD_SHARE]
32
            )
33
        ):
34
            return False
35
        return True
36
37
    def get_shares(self):
38
        """ Get all shares from the user """
39
        return self.requester.get(self.get_local_url())
40
41
    def get_shares_from_path(self, path, reshares=None, subfiles=None):
42
        """
43
        Get all shares from a given file/folder
44
45
        Args:
46
            path (str): path to file/folder
47
            reshares (bool): (optional) return not only the shares from the current user but
48
                all shares from the given file
49
            subfiles (bool): (optional) return all shares within a folder, given that path
50
                defines a folder
51
52
        Returns:
53
54
        """
55
        url = self.get_local_url()
56
        params = {
57
            "path": path,
58
            # TODO: test reshares, subfiles
59
            "reshares": None if reshares is None else str(bool(reshares)).lower(),
60
            "subfiles": None if subfiles is None else str(bool(subfiles)).lower(),
61
        }
62
        return self.requester.get(url, params=params)
63
64
    def get_share_info(self, sid):
65
        """
66
        Get information about a given share
67
68
        Args:
69
            sid (int): share id
70
71
        Returns:
72
        """
73
        return self.requester.get(self.get_local_url(sid))
74
75
    def create_share(
76
            self, path, share_type, share_with=None, public_upload=None,
77
            password=None, permissions=None):
78
        """
79
        Share a file/folder with a user/group or as public link
80
81
        Mandatory fields: share_type, path and share_with for share_type USER (0) or GROUP (1).
82
83
        Args:
84
            path (str): path to the file/folder which should be shared
85
            share_type (int): ShareType attribute
86
            share_with (str): user/group id with which the file should be shared
87
            public_upload (bool): bool, allow public upload to a public shared folder (true/false)
88
            password (str): password to protect public link Share with
89
            permissions (int): sum of selected Permission attributes
90
91
        Returns:
92
93
        """
94
        if not self.validate_share_parameters(path, share_type, share_with):
95
            return False
96
97
        url = self.get_local_url()
98
        if public_upload:
99
            public_upload = "true"
100
101
        data = {"path": path, "shareType": share_type}
102
        if share_type in [ShareType.GROUP, ShareType.USER, ShareType.FEDERATED_CLOUD_SHARE]:
103
            data["shareWith"] = share_with
104
        if public_upload:
105
            data["publicUpload"] = public_upload
106
        if share_type == ShareType.PUBLIC_LINK and password is not None:
107
            data["password"] = str(password)
108
        if permissions is not None:
109
            data["permissions"] = permissions
110
        return self.requester.post(url, data)
111
112
    def delete_share(self, sid):
113
        """
114
        Remove the given share
115
116
        Args:
117
            sid (str): share id
118
119
        Returns:
120
121
        """
122
        return self.requester.delete(self.get_local_url(sid))
123
124
    def update_share(self, sid,
125
                     permissions=None, password=None, public_upload=None, expire_date=""):
126
        """
127
        Update a given share, only one value can be updated per request
128
129
        Args:
130
            sid (str): share id
131
            permissions (int): sum of selected Permission attributes
132
            password (str): password to protect public link Share with
133
            public_upload (bool): bool, allow public upload to a public shared folder (true/false)
134
            expire_date (str): set an expire date for public link shares. Format: ‘YYYY-MM-DD’
135
136
        Returns:
137
138
        """
139
        params = dict(
140
            permissions=permissions,
141
            password=password,
142
            expireDate=expire_date
143
        )
144
        if public_upload:
145
            params["publicUpload"] = "true"
146
        if public_upload is False:
147
            params["publicUpload"] = "false"
148
149
        # check if only one param specified
150
        specified_params_count = sum([int(bool(each)) for each in params.values()])
151
        if specified_params_count > 1:
152
            raise ValueError("Only one parameter for update can be specified per request")
153
154
        url = self.get_local_url(sid)
155
        return self.requester.put(url, data=params)
156