Expand description
§Introduction
This Rust crate provides an easy-to-use async interface to the Anytype API. At this stage of development, the code consists largely of builder structs that setup and execute an Anytype API call.
WARNING!
This crate is still in development and therefore subject to change and not yet optimized nor fully tested. Do not use it to write production code!
§Example of how to use the crate
The code shown below will, once it is modified to use a ‘real’ api_key, list all the spaces contained in the Anytype vault
use any_type::spaces::ListSpacesRequest;
use any_type::vault::Vault;
#[tokio::main]
async fn main() {
let api_key = "bye-bye-baby-baby-bye-bye-42";
let server = "http://127.0.0.1:31009";
// Execute the ListSpaces API call (multiple times if required) to list all spaces contained in the vault
let mut request = ListSpacesRequest::new(api_key, server).offset(0).limit(100);
'while_more: loop {
let response = request.send().await;
let list_of = response.unwrap(); // Don't do this in production code! Remember Cloudflare!
for s in list_of.spaces.iter() {
println!("name : {}", s.name);
println!("description : {}", s.description);
println!("gateway url : {}", s.gateway_url);
println!("id : {}", s.id);
println!("network id : {}", s.network_id);
println!("object : {}", s.object);
println!("");
}
if !list_of.has_more {
break 'while_more;
}
request = request.offset(list_of.next_offset);
}
}
§Getting started
The crate includes example code that illustrate how to use the library. The code can be found in the examples directory and can be run by executing “cargo run –example <example_name>” command. All the example code assumes the following:
- The Anytype desktop is running
- The Anytype desktop is listening for API calls on the default endpoint: http://127.0.0.1:31009
- An API key has been generated and assigned to the environment variable “ANY_TYPE_API_KEY”
- This does not apply to the binary “api_key_gen”
§any_type_1_api_key_gen
Generates a new api_key by executing the CreateChallenge and CreateApiKey API calls. The generated api_key is associated with the app_name of “any_test”.
§any_type_2_space
Creates a new Anytype space by executing the CreateSpace API call. The program will prompt you for a name for the space that will be created.
§any_type_3_properties_tags_type
Creates the following:
- An Anytype Property called “Duck Testing” by executing a CreateProperty API call.
- Three Anytype Tags called “Basic”, “Intermediate” and “Advanced” using the CreateTag API call.
- An Anytype Type called “Ducks” by executing a CreateType API call.
This program prompts you to enter the id for the space that was created by “any_type_2_space”.
The new “Ducks” type will have the “Duck Testing” property associated with it. However, you will have to edit the “Ducks” type to make the property visible for objects created with this type.
§any_type_4_objects
Creates 2 objects of the type “Ducks”. The program will prompt you to enter the id for the space that was created by “any_type_2_space”. If successful, details about the two new objects is displayed.
Once run you will see two new objects in the Anytype Desktop with the following titles:
- Welcome to any_type!
- The future is bright, the future is Anytype!
§any_type_5_spaces
The code for this binary is similar to the example code listed earlier on in this README file. The program simply prints out information relating to all the spaces that are defined in your local Anytype vault.
§any_type_6_search
Runs a search using the contents of a file or a sample string that is hard-coded in the binary. The program prompts you to enter both a filename and a space-id. Both are optional. If you do not enter a filename the hard-coded sample string will be used. If you do not enter the space-id, all spaces will be searched.
§Important library features
§Limited abstraction
The library creates a very thin layer of abstraction for the Anytype API. The goal is to develop the crate in-step with the development of the Anytype API. For example, since there are currently no Chat related API calls, this library does not provide functions that allow for working with a chat channel.
§Builder structs
The library provide access to Anytypes objects via objects created by builder structs. This results in code that looks like this:
use any_type::properties::GetProperty;
...
let request = GetProperty::new("my_api_key_is_quite_long", "http://127.0.0.1:31009")
.space_id("byebyeverylongspaceidthathas.oddcharsinit")
.property_id("byethepropertyidcanalsobelong42")
.send();The “send” function will return either the desired object or a RequestFailure object.
§Several enum types to simplify defining Anytype values
The Anytype API requires the programmer to use string literals to define a number of API parameters. In order to make life easier for the application programmer, a set of enums have been created to make this easier:
- The “PropertyFormat” enum defines values for the various property formats (e.g. checkbox, select, multi_select, etc)
- The “AnytypeColor” enum defines values for the colors that icons and tags can have(e.g grey, yellow, orange, etc)
- The “IconName” enum defines values for various “named” icons that can be defined (e.g airplane, bluetooth radio, etc)
§An experimental REPL
The binary crate “any_type_repl” allows you to display information about collections and spaces. The application can be installed by running “cargo install any_type”. Provided that your Rust environment is correctly configured, you should be able to run it simply by entering “any_type_repl” in a command prompt. The program assumes the following:
- The Anytype desktop is running
- The Anytype desktop is listening for API calls on the default endpoint: http://127.0.0.1:31009
- An API key has been generated and assigned to the environment variable “ANY_TYPE_API_KEY”
- The program will prompt you to enter the api_key if the variable is not set
§Current state of development (v0.4.0)
§Complete set of API calls
All the API calls documented on the Antype website have now been implemented.
§Not optimized
Owing to the state of developement of the Anytype API (i.e. subject to change), the code has not been written to be efficient. The focus has been on developing a library that is easy for the user (i.e. software developer) to use.
§Incomplete documentation
Some of the elements of the library have been documented, but there is still work to be done!
§Limited Testing
Testing is being conducted every day, but there is still some way to go! However, at this stage at the very least the intro binaries should work on all systems. If this is not the case, the author would be very happy to receive any feedback you may have via email.
§Known Bugs (i.e. no fix)
- GetTemplateRequest will fail if no template-id is provided
- Not fixed because this is an Anytype API error that should be fixed by the Anytype API developers
§License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
§Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
§Sample search used by “any_type_6_search”
{
"filters": {
"conditions": [],
"filters": [null],
"operator": "and"
},
"query": "Diarmuid",
"sort": {
"direction": "desc",
"property_key": "last_modified_date"
},
"types": ["page", "task", "bookmark", "duck_type"]
}