Skip to content

anytype.Space

Bases: APIWrapper

Used to interact with and manage objects, types, and other elements within a specific Space. It provides methods to retrieve objects, types, and perform search operations within the space. Additionally, it allows creating new objects associated with specific types.

Source code in anytype/space.py
 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
class Space(APIWrapper):
    """
    Used to interact with and manage objects, types, and other elements within a specific Space. It provides methods to retrieve objects, types, and perform search operations within the space. Additionally, it allows creating new objects associated with specific types.
    """

    def __init__(self):
        self._apiEndpoints: apiEndpoints | None = None
        self.name = ""
        self.id = ""
        self._all_types = []


    @requires_auth
    def get_object(self, objectId: str) -> Object:
        """
        Retrieves a specific object by its ID.

        Parameters:
            objectId (str): The ID of the object to retrieve.

        Returns:
            An Object instance representing the retrieved object.

        Raises:
            Raises an error if the request to the API fails.
        """
        response = self._apiEndpoints.getObject(self.id, objectId)
        data = response.get("object", {})
        return Object._from_api(self._apiEndpoints, data)


    @requires_auth
    def delete_object(self, objectId: str) -> None:
        # BUG: not working yet
        self._apiEndpoints.deleteObject(self.id, objectId)


    @requires_auth
    def get_objects(self, offset=0, limit=100) -> list[Object]:
        """
        Retrieves a list of objects associated with the space.

        Parameters:
            offset (int, optional): The offset for pagination (default: 0).
            limit (int, optional): The limit for the number of results (default: 100).

        Returns:
            A list of Object instances.

        Raises:
            Raises an error if the request to the API fails.
        """
        response_data = self._apiEndpoints.getObjects(self.id, offset, limit)
        objects = [
            Object._from_api(self._apiEndpoints, data)
            for data in response_data.get("data", [])
        ]

        self._all_types = objects # TODO: is this supposed to be here?
        return objects


    @requires_auth
    def get_type(self, typeId: str) -> Type:
        """
        Retrieves a specific type by its ID.

        Parameters:
            type_name (str): The name of the type to retrieve.

        Returns:
            A Type instance representing the type.

        Raises:
            ValueError: If the type with the specified name is not found.
        """
        response = self._apiEndpoints.getType(self.id, typeId)
        data = response.get("type", {})
        # TODO: Sometimes we need to add more attributes beyond the ones in the 
        # API response. There might be a cleaner way to do this, but doing
        # a dict merge with | works for now.
        return Type._from_api(self._apiEndpoints, data | {"space_id": self.id})


    @requires_auth
    def get_types(self, offset=0, limit=100) -> list[Type]:
        """
        Retrieves a list of types associated with the space.

        Parameters:
            offset (int, optional): The offset for pagination (default: 0).
            limit (int, optional): The limit for the number of results (default: 100).

        Returns:
            A list of Type instances.

        Raises:
            Raises an error if the request to the API fails.
        """
        response = self._apiEndpoints.getTypes(self.id, offset, limit)
        types = [
            Type._from_api(self._apiEndpoints, data | {"space_id": self.id})
            for data in response.get("data", [])
        ]

        self._all_types = types
        return types


    def get_typebyname(self, name: str) -> Type:
        all_types = self.get_types(limit=200)
        for type in all_types:
            if type.name == name:
                return type

        raise ValueError("Type not found")


    @requires_auth
    def get_member(self, memberId: str) -> Member:
        response = self._apiEndpoints.getMember(self.id, memberId)
        data = response.get("object", {})
        return Member._from_api(self._apiEndpoints, data)


    @requires_auth
    def get_members(self, offset: int = 0, limit: int = 100) -> list[Member]:
        """
        Retrieves a list of members associated with the space.

        Parameters:
            offset (int, optional): The offset for pagination (default: 0).
            limit (int, optional): The limit for the number of results (default: 100).

        Returns:
            A list of Member instances.

        Raises:
            Raises an error if the request to the API fails.
        """
        response = self._apiEndpoints.getMembers(self.id, offset, limit)
        return [
            Member._from_api(self._apiEndpoints, data)
            for data in response.get("data", [])
        ]


    def get_listviewfromobject(
        self, obj: Object, offset: int = 0, limit: int = 100
    ) -> list[ListView]:
        if obj.type != "Collection":
            raise ValueError("Object is not a collection")
        return self.get_listviews(obj.id, offset, limit)


    @requires_auth
    def get_listviews(self, listId: str, offset: int = 0, limit: int = 100) -> list[ListView]:
        response = self._apiEndpoints.getListViews(self.id, listId, offset, limit)
        return [
            ListView._from_api(self._apiEndpoints, data | {
                "space_id": self.id,
                "list_id": listId,
            })
            for data in response.get("data", [])
        ]


    @requires_auth
    def search(self, query, offset=0, limit=10) -> list[Object]:
        """
        Performs a search for objects in the space using a query string.

        Parameters:
            query (str): The search query string.
            offset (int, optional): The offset for pagination (default: 0).
            limit (int, optional): The limit for the number of results (default: 10).

        Returns:
            A list of Object instances that match the search query.

        Raises:
            ValueError: If the space ID is not set.
        """
        if self.id == "":
            raise ValueError("Space ID is required")

        response = self._apiEndpoints.search(self.id, query, offset, limit)
        return [
            Object._from_api(self._apiEndpoints, data)
            for data in response.get("data", [])
        ]


    @requires_auth
    def create_object(self, obj: Object, type: Type = Type()) -> Object:
        """
        Creates a new object within the space, associated with a specified type.

        Parameters:
            obj (Object): The Object instance to create.
            type (Type): The Type instance to associate the object with.

        Returns:
            A new Object instance representing the created object.

        Raises:
            Raises an error if the request to the API fails.
        """
        if type.key == "" and obj.type_key == "":
            raise Exception(
                "You need to set one type for the object, use add_type method from the Object class"
            )

        type_key = obj.type_key if obj.type_key != "" else type.key
        template_id = obj.template_id if obj.template_id != "" else type.template_id

        icon = {}
        if isinstance(obj.icon, Icon):
            icon = obj.icon._get_json()
        else:
            raise ValueError("Invalid icon type")

        object_data = {
            "icon": icon,
            "name": obj.name,
            "description": obj.description,
            "body": obj.body,
            "source": "",
            "template_id": template_id,
            "type_key": type_key,
        }

        obj_clone = deepcopy(obj)
        obj_clone._apiEndpoints = self._apiEndpoints
        obj_clone.space_id = self.id

        response = self._apiEndpoints.createObject(self.id, object_data)

        for key, value in response.get("object", {}).items():
            if key == "icon":
                icon = Icon()
                icon._update_with_json(value)
            else:
                obj_clone.__dict__[key] = value

        return obj_clone


    def __repr__(self):
        return f"<Space(name={self.name})>"

create_object(obj, type=Type())

Creates a new object within the space, associated with a specified type.

Parameters:

Name Type Description Default
obj Object

The Object instance to create.

required
type Type

The Type instance to associate the object with.

Type()

Returns:

Type Description
Object

A new Object instance representing the created object.

Source code in anytype/space.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
@requires_auth
def create_object(self, obj: Object, type: Type = Type()) -> Object:
    """
    Creates a new object within the space, associated with a specified type.

    Parameters:
        obj (Object): The Object instance to create.
        type (Type): The Type instance to associate the object with.

    Returns:
        A new Object instance representing the created object.

    Raises:
        Raises an error if the request to the API fails.
    """
    if type.key == "" and obj.type_key == "":
        raise Exception(
            "You need to set one type for the object, use add_type method from the Object class"
        )

    type_key = obj.type_key if obj.type_key != "" else type.key
    template_id = obj.template_id if obj.template_id != "" else type.template_id

    icon = {}
    if isinstance(obj.icon, Icon):
        icon = obj.icon._get_json()
    else:
        raise ValueError("Invalid icon type")

    object_data = {
        "icon": icon,
        "name": obj.name,
        "description": obj.description,
        "body": obj.body,
        "source": "",
        "template_id": template_id,
        "type_key": type_key,
    }

    obj_clone = deepcopy(obj)
    obj_clone._apiEndpoints = self._apiEndpoints
    obj_clone.space_id = self.id

    response = self._apiEndpoints.createObject(self.id, object_data)

    for key, value in response.get("object", {}).items():
        if key == "icon":
            icon = Icon()
            icon._update_with_json(value)
        else:
            obj_clone.__dict__[key] = value

    return obj_clone

get_members(offset=0, limit=100)

Retrieves a list of members associated with the space.

Parameters:

Name Type Description Default
offset int

The offset for pagination (default: 0).

0
limit int

The limit for the number of results (default: 100).

100

Returns:

Type Description
list[Member]

A list of Member instances.

Source code in anytype/space.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
@requires_auth
def get_members(self, offset: int = 0, limit: int = 100) -> list[Member]:
    """
    Retrieves a list of members associated with the space.

    Parameters:
        offset (int, optional): The offset for pagination (default: 0).
        limit (int, optional): The limit for the number of results (default: 100).

    Returns:
        A list of Member instances.

    Raises:
        Raises an error if the request to the API fails.
    """
    response = self._apiEndpoints.getMembers(self.id, offset, limit)
    return [
        Member._from_api(self._apiEndpoints, data)
        for data in response.get("data", [])
    ]

get_object(objectId)

Retrieves a specific object by its ID.

Parameters:

Name Type Description Default
objectId str

The ID of the object to retrieve.

required

Returns:

Type Description
Object

An Object instance representing the retrieved object.

Source code in anytype/space.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@requires_auth
def get_object(self, objectId: str) -> Object:
    """
    Retrieves a specific object by its ID.

    Parameters:
        objectId (str): The ID of the object to retrieve.

    Returns:
        An Object instance representing the retrieved object.

    Raises:
        Raises an error if the request to the API fails.
    """
    response = self._apiEndpoints.getObject(self.id, objectId)
    data = response.get("object", {})
    return Object._from_api(self._apiEndpoints, data)

get_objects(offset=0, limit=100)

Retrieves a list of objects associated with the space.

Parameters:

Name Type Description Default
offset int

The offset for pagination (default: 0).

0
limit int

The limit for the number of results (default: 100).

100

Returns:

Type Description
list[Object]

A list of Object instances.

Source code in anytype/space.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@requires_auth
def get_objects(self, offset=0, limit=100) -> list[Object]:
    """
    Retrieves a list of objects associated with the space.

    Parameters:
        offset (int, optional): The offset for pagination (default: 0).
        limit (int, optional): The limit for the number of results (default: 100).

    Returns:
        A list of Object instances.

    Raises:
        Raises an error if the request to the API fails.
    """
    response_data = self._apiEndpoints.getObjects(self.id, offset, limit)
    objects = [
        Object._from_api(self._apiEndpoints, data)
        for data in response_data.get("data", [])
    ]

    self._all_types = objects # TODO: is this supposed to be here?
    return objects

get_type(typeId)

Retrieves a specific type by its ID.

Parameters:

Name Type Description Default
type_name str

The name of the type to retrieve.

required

Returns:

Type Description
Type

A Type instance representing the type.

Raises:

Type Description
ValueError

If the type with the specified name is not found.

Source code in anytype/space.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
@requires_auth
def get_type(self, typeId: str) -> Type:
    """
    Retrieves a specific type by its ID.

    Parameters:
        type_name (str): The name of the type to retrieve.

    Returns:
        A Type instance representing the type.

    Raises:
        ValueError: If the type with the specified name is not found.
    """
    response = self._apiEndpoints.getType(self.id, typeId)
    data = response.get("type", {})
    # TODO: Sometimes we need to add more attributes beyond the ones in the 
    # API response. There might be a cleaner way to do this, but doing
    # a dict merge with | works for now.
    return Type._from_api(self._apiEndpoints, data | {"space_id": self.id})

get_types(offset=0, limit=100)

Retrieves a list of types associated with the space.

Parameters:

Name Type Description Default
offset int

The offset for pagination (default: 0).

0
limit int

The limit for the number of results (default: 100).

100

Returns:

Type Description
list[Type]

A list of Type instances.

Source code in anytype/space.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@requires_auth
def get_types(self, offset=0, limit=100) -> list[Type]:
    """
    Retrieves a list of types associated with the space.

    Parameters:
        offset (int, optional): The offset for pagination (default: 0).
        limit (int, optional): The limit for the number of results (default: 100).

    Returns:
        A list of Type instances.

    Raises:
        Raises an error if the request to the API fails.
    """
    response = self._apiEndpoints.getTypes(self.id, offset, limit)
    types = [
        Type._from_api(self._apiEndpoints, data | {"space_id": self.id})
        for data in response.get("data", [])
    ]

    self._all_types = types
    return types

search(query, offset=0, limit=10)

Performs a search for objects in the space using a query string.

Parameters:

Name Type Description Default
query str

The search query string.

required
offset int

The offset for pagination (default: 0).

0
limit int

The limit for the number of results (default: 10).

10

Returns:

Type Description
list[Object]

A list of Object instances that match the search query.

Raises:

Type Description
ValueError

If the space ID is not set.

Source code in anytype/space.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
@requires_auth
def search(self, query, offset=0, limit=10) -> list[Object]:
    """
    Performs a search for objects in the space using a query string.

    Parameters:
        query (str): The search query string.
        offset (int, optional): The offset for pagination (default: 0).
        limit (int, optional): The limit for the number of results (default: 10).

    Returns:
        A list of Object instances that match the search query.

    Raises:
        ValueError: If the space ID is not set.
    """
    if self.id == "":
        raise ValueError("Space ID is required")

    response = self._apiEndpoints.search(self.id, query, offset, limit)
    return [
        Object._from_api(self._apiEndpoints, data)
        for data in response.get("data", [])
    ]