any_type 0.3.0

A library for the Anytype API
Documentation

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 several binaries that illustrate how to use the library. The binaries can be compiled and run by executing the appropriate "cargo run --bin <binary_name>" command. All these binaries assume 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 retrieves to prompts for a name for the space that will be created. The

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 "IconColor" enum defines values for the colors that icons 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)

Current state of development (v0.3.0)

Incomplete set of API calls

At this time the following API calls are not yet coded:

  • Add objects to list
  • Remove objects to list
  • Get objects in list

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.

License

Licensed under either of

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"]
}