From 28219623e7a5622042d4414948a07448ff7e1d7a Mon Sep 17 00:00:00 2001 From: Nutomic Date: Wed, 8 Jun 2022 15:25:50 +0000 Subject: [PATCH] Add documentation for Rust API (#144) --- src/en/SUMMARY.md | 1 + src/en/client_development/rust_api.md | 37 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/en/client_development/rust_api.md diff --git a/src/en/SUMMARY.md b/src/en/SUMMARY.md index 41bd2e0..f9be3af 100644 --- a/src/en/SUMMARY.md +++ b/src/en/SUMMARY.md @@ -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) diff --git a/src/en/client_development/rust_api.md b/src/en/client_development/rust_api.md new file mode 100644 index 0000000..6b5d6ea --- /dev/null +++ b/src/en/client_development/rust_api.md @@ -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(¶ms).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) +- \ No newline at end of file