Completed
Push — master ( a9b8b6...971575 )
by Matěj
12s
created

nextcloud.api_wrappers.share.Share.get_shares()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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