Add documentation for Rust API (#144)

This commit is contained in:
Nutomic 2022-06-08 15:25:50 +00:00 committed by GitHub
parent 8b9c725eb1
commit 28219623e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View file

@ -21,6 +21,7 @@
- [Lemmy Protocol](federation/lemmy_protocol.md)
- [Client Development](client_development/client_development.md)
- [HTTP API extras](client_development/http_api_extras.md)
- [Rust API](client_development/rust_api.md)
- [Creating a Custom Frontend](client_development/custom_frontend.md)
- [Contributing](contributing/contributing.md)
- [Docker Development](contributing/docker_development.md)

View file

@ -0,0 +1,37 @@
# Rust API
If you want to develop a Rust application which interacts with Lemmy, you can directly pull in the relevant API structs. This relies on the exact code used in Lemmy, but with most heavyweight dependencies disabled (like diesel).
To get started, add the following to your `Cargo.toml`:
```
[dependencies]
lemmy_api_common = { git = "https://github.com/LemmyNet/lemmy.git" }
```
Note, at the time of writing, this code is not available on crates.io yet. You can use "0.16.3" from crates.io, but it pulls in many heavy dependencies including diesel. Best use the git dependency for now, or wait for a newer version to become available.
You can then use the following code to make an API request:
```rust
use lemmy_api_common::post::{GetPosts, GetPostsResponse};
use lemmy_db_schema::{ListingType, SortType};
use ureq::Agent;
pub fn list_posts() -> GetPostsResponse {
let params = GetPosts {
type_: Some(ListingType::Local),
sort: Some(SortType::New),
..Default::default()
};
Agent::new()
.get("https://lemmy.ml/post/list")
.send_json(&params).unwrap()
.into_json().unwrap()
}
```
You can also look at the following real-world projects as examples:
- [lemmyBB](https://github.com/Nutomic/lemmyBB)
- [lemmy-stats-crawler](https://yerbamate.ml/LemmyNet/lemmy-stats-crawler)
-