]> Untitled Git - lemmy.git/blob - crates/api_common/README.md
Separate federated_instances into its own endpoint. Fixes #1931 (#2813)
[lemmy.git] / crates / api_common / README.md
1 lemmy_api_common
2 ===
3
4 This crate provides all the data types which are necessary to build a client for [Lemmy](https://join-lemmy.org/). You can use them with the HTTP client of your choice.
5
6 Here is an example using [reqwest](https://crates.io/crates/reqwest):
7
8 ```rust
9     let params = GetPosts {
10         community_name: Some("asklemmy".to_string()),
11         ..Default::default()
12     };
13     let client = Client::new();
14     let response = client
15         .get("https://lemmy.ml/api/v3/post/list")
16         .query(&params)
17         .send()
18         .await?;
19     let json = response.json::<GetPostsResponse>().await.unwrap();
20     print!("{:?}", &json);
21 ```
22
23 As you can see, each API endpoint needs a parameter type ( GetPosts), path (/post/list) and response type (GetPostsResponse). You can find the paths and parameter types from [this file](https://github.com/LemmyNet/lemmy/blob/main/src/api_routes_http.rs). For the response types you need to look through the crates [lemmy_api](https://github.com/LemmyNet/lemmy/tree/main/crates/api/src) and [lemmy_api_crud](https://github.com/LemmyNet/lemmy/tree/main/crates/api_crud/src) for the place where Perform/PerformCrud is implemented for the parameter type. The response type is specified as a type parameter on the trait.
24
25 For a real example of a Lemmy API client, look at [lemmyBB](https://github.com/LemmyNet/lemmyBB/tree/main/src/api).
26
27 Lemmy also provides a websocket API. You can find the full websocket code in [this file](https://github.com/LemmyNet/lemmy/blob/main/src/api_routes_websocket.rs).