Bases: APIWrapper
The Type class is used to interact with and manage templates in a specific space. It allows for retrieving available templates, setting a specific template for a type, and handling template-related actions within the space.
Source code in anytype/type.py
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 | class Type(APIWrapper):
"""
The Type class is used to interact with and manage templates in a specific space. It allows for retrieving available templates, setting a specific template for a type, and handling template-related actions within the space.
"""
def __init__(self, name: str = ""):
self._apiEndpoints: apiEndpoints | None = None
self._all_templates = []
self.type = ""
self.space_id = ""
self.id = ""
self.name = ""
self.icon = {}
self.key = ""
self.template_id = ""
if name != "":
self.set_template(name)
@requires_auth
def get_templates(self, offset: int = 0, limit: int = 100) -> list[Template]:
"""
Retrieves all templates associated with the type from the API.
Parameters:
offset (int): The offset to start retrieving templates (default: 0).
limit (int): The maximum number of templates to retrieve (default: 100).
Returns:
A list of Template objects.
Raises:
Raises an error if the request to the API fails.
"""
response = self._apiEndpoints.getTemplates(self.space_id, self.id, offset, limit)
self._all_templates = [
Template._from_api(self._apiEndpoints, data)
for data in response.get("data", [])
]
return self._all_templates
def set_template(self, template_name: str) -> None:
"""
Sets a template for the type by name. If no templates are loaded, it will first fetch all templates.
Parameters:
template_name (str): The name of the template to assign.
Returns:
None
Raises:
ValueError: If a template with the specified name is not found.
"""
if len(self._all_templates) == 0:
self.get_templates()
found = False
for template in self._all_templates:
if template.name == template_name:
found = True
self.template_id = template.id
return
if not found:
raise ValueError(
f"Type '{self.name}' does not have " "a template named '{template_name}'"
)
@requires_auth
def get_template(self, id: str) -> Template:
response = self._apiEndpoints.getTemplate(self.space_id, self.id, id)
# TODO: This API response is unlike the rest, it returns a list for
# "data" even though we're asking for info on a single template.
# Bug in anytype-heart, or am I misunderstanding?
datas = response.get("data", [])
if len(datas) > 1:
print(f"getTemplate response data has more than one entry: {response}")
return Template._from_api(self._apiEndpoints, datas[0])
@requires_auth
def get_template(self, id: str) -> Template:
response_data = self._apiEndpoints.getTemplate(self.space_id, self.id, id)
template = Template()
template._apiEndpoints = self._apiEndpoints
for data in response_data.get("data", []):
for key, value in data.items():
template.__dict__[key] = value
return template
def __repr__(self):
if "emoji" in self.icon:
return f"<Type(name={self.name}, icon={self.icon['emoji']})>"
else:
return f"<Type(name={self.name}, icon={self.icon['name']})>"
|
get_templates(offset=0, limit=100)
Retrieves all templates associated with the type from the API.
Parameters:
Name |
Type |
Description |
Default |
offset
|
int
|
The offset to start retrieving templates (default: 0).
|
0
|
limit
|
int
|
The maximum number of templates to retrieve (default: 100).
|
100
|
Returns:
Type |
Description |
list[Template]
|
A list of Template objects.
|
Source code in anytype/type.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 | @requires_auth
def get_templates(self, offset: int = 0, limit: int = 100) -> list[Template]:
"""
Retrieves all templates associated with the type from the API.
Parameters:
offset (int): The offset to start retrieving templates (default: 0).
limit (int): The maximum number of templates to retrieve (default: 100).
Returns:
A list of Template objects.
Raises:
Raises an error if the request to the API fails.
"""
response = self._apiEndpoints.getTemplates(self.space_id, self.id, offset, limit)
self._all_templates = [
Template._from_api(self._apiEndpoints, data)
for data in response.get("data", [])
]
return self._all_templates
|
set_template(template_name)
Sets a template for the type by name. If no templates are loaded, it will first fetch all templates.
Parameters:
Name |
Type |
Description |
Default |
template_name
|
str
|
The name of the template to assign.
|
required
|
Returns:
Raises:
Type |
Description |
ValueError
|
If a template with the specified name is not found.
|
Source code in anytype/type.py
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 | def set_template(self, template_name: str) -> None:
"""
Sets a template for the type by name. If no templates are loaded, it will first fetch all templates.
Parameters:
template_name (str): The name of the template to assign.
Returns:
None
Raises:
ValueError: If a template with the specified name is not found.
"""
if len(self._all_templates) == 0:
self.get_templates()
found = False
for template in self._all_templates:
if template.name == template_name:
found = True
self.template_id = template.id
return
if not found:
raise ValueError(
f"Type '{self.name}' does not have " "a template named '{template_name}'"
)
|