-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandafolder.py
125 lines (106 loc) · 3.76 KB
/
pandafolder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""pandafolder.py implements API Wrapper for Documents https://developers.pandadoc.com/reference#organize-folders-with-api"""
__author__ = "Kostyantyn Ovechko"
__copyright__ = "Copyright 2020, Zxscript"
__license__ = "MIT"
__version__ = "0.0.1"
__email__ = "kos@zxscript.com"
__status__ = "Production"
from typing import List, Dict
from abc import ABC, abstractmethod
from .panda_exceptions import ApiException
class FolderUuidRequired(Exception):
pass
class PandaFolder(ABC):
_pandaworkspace = None
def __init__(self, uuid: str = None, name: str = None, date_created: str = None):
"""
:type pandadoc: pandaworkspace
"""
if uuid is None:
raise FolderUuidRequired()
self.__uuid = uuid
self.__name = name
self.__date_created = date_created
@classmethod
@abstractmethod
def get_folder_type(cls):
pass
@property
def uuid(self):
return self.__uuid
@property
def name(self):
return self.__name
@property
def date_created(self):
return self.__date_created
@classmethod
def list(
cls,
parent_uuid: str = None,
count: int = None,
page: int = None,
) -> List[Dict]:
"""
:param parent_uuid: str: The UUID of the folder containing folders. To list the folders located in the root folder, please remove this param in the request.
:param count: int: Optionally specify how many folders to return. Default is 50 folders, maximum is 100 folders.
:param page: int: Optionally specify which page of the dataset to return.
:return List[Dict]: List of folders
"""
data = {}
if parent_uuid:
data['parent_uuid'] = parent_uuid
if count:
data['count'] = count
if page:
data['page'] = page
response = cls._pandaworkspace.get('{folder_type}/folders'.format(
folder_type=cls.get_folder_type(),
), data=data)
if response.status_code != 200:
raise ApiException(response.text)
results = response.json().get('results')
return results
@classmethod
def create(
cls,
parent_uuid: str = None,
name: str = 'Sample Template',
):
"""
:param pandadoc:
:param parent_uuid: ID of the parent folder
:param name: Name for the new folder
:param template_uuid: e.g.: "Cu7KZisX2Hrnug6FgrYX4d"
:return:
"""
data = {
'name': name,
}
if parent_uuid:
data['parent_uuid'] = parent_uuid
response = cls._pandaworkspace.post('{folder_type}/folders'.format(folder_type=cls.get_folder_type()), data=data)
response_json = response.json()
folder_uuid = response_json.get('uuid', None)
print("folder_uuid:", folder_uuid)
panda_folder = cls(
uuid=response_json.get('uuid', None),
name=response_json.get('name', None),
date_created=response_json.get('date_created', None),
)
return panda_folder
def rename(self, new_name: str = 'New folder name') -> int:
data = {
'name': new_name,
}
response = self.__class__._pandaworkspace.put('{folder_type}/folders/{folder_uuid}'.format(
folder_type=self.__class__.get_folder_type(),
folder_uuid=self.uuid,
), data=data)
return response.status_code
# def delete(self) -> int:
# response = self.__class__._pandadoc.delete('{folder_type}/folders/{folder_uuid}'.format(
# folder_type=self.__class__.get_folder_type(),
# folder_uuid=self.uuid,
# ))
# return response.status_code