-
Notifications
You must be signed in to change notification settings - Fork 1
1. Anytype
Charles K. Neimog edited this page Feb 4, 2025
·
2 revisions
The Anytype
class is used to interact with the Anytype API for authentication, retrieving spaces, creating spaces, and performing global searches. It provides methods to authenticate via a token, fetch spaces, create new spaces, and search for objects across spaces.
-
app_name
: The name of the application (default is"Python API"
). -
space_id
: The ID of the space the user is working with. -
token
: The session token used for authentication. -
app_key
: The application key for authentication.
Authenticates the user by retrieving or creating a session token. If the session token already exists, it validates the token. If not, the user will be prompted to enter a 4-digit code for authentication.
def auth(self) -> None:
-
Returns:
None
-
Raises:
- Raises an error if the authentication request or token validation fails.
Retrieves a list of spaces associated with the authenticated user.
def get_spaces(self, offset=0, limit=10) -> list[Space]:
-
Parameters:
-
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
Space
instances.
- A list of
-
Raises:
- Raises an error if the request to the API fails.
Creates a new space with a given name.
def create_space(self, name: str) -> Space:
-
Parameters:
-
name
(str): The name of the space to create.
-
-
Returns:
- A
Space
instance representing the newly created space.
- A
-
Raises:
- Raises an error if the space creation request fails.
Performs a global search for objects across all spaces using a query string.
def global_search(self, query, offset=0, limit=10) -> list[Object]:
-
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.
- A list of
-
Raises:
- Raises an error if the search request fails.
# Create an instance of Anytype
anytype = Anytype()
# Authenticate the user
anytype.auth()
# Retrieve all spaces
spaces = anytype.get_spaces()
# Create a new space
new_space = anytype.create_space("My New Space")
# Perform a global search for objects
search_results = anytype.global_search("my query")
# Print the spaces
for space in spaces:
print(space)
# Print the search results
for obj in search_results:
print(obj)