edjx/lib.rs
1#![allow(dead_code)]
2//! # EDJX Library SDK for Rust
3//! This crate contains the EDJX SDK library for Rust. This SDK is used to develop EDJX serverless functions.
4//! In this version, functions are triggered using HTTP only.
5//!
6//! Additionally, developers can use the streaming feature while building serverless functions.
7//! Streaming optimizes data transfer (upload file/download file) to the EDJX object storage,
8//! and HTTP transfers. Using the streaming feature, developers can work with large data sets
9//! without allocating huge memory to the functions. Since functions process data as
10//! smaller chunks, the use of streaming in functions drastically reduces the response time.
11//!
12//! ## EDJX HTTP Trigger Functions
13//! These functions are triggered from standard HTTP methods. The input
14//! for the function is an HTTP Request object (represented by [`HttpRequest`] in the EDJX SDK)
15//! and the return value must be an HTTP Response object (represented by [`HttpResponse`] in the EDJX SDK).
16//!
17//! EDJX sample [`Rust Serverless Application`](https://github.com/edjx/edjfunction-example-rust) has two
18//! Rust files :
19//!
20//! 1. `lib.rs` : This file contains helper code to fetch HTTP Requests and pass the request as input to the
21//! serverless function being developed. Additionally, this file has code related to HTTP Response
22//! processing. Modify this file only when a mutable reference to the request is required, or the
23//! HTTP Response type needs to be changed. See [`HttpRequest`] and [`HttpResponse`] for more details.
24//!
25//! ```
26//! #[no_mangle]
27//! pub fn init() {
28//! let req = match HttpRequest::from_client() {
29//! Ok(val) => val,
30//! Err(e) => {
31//! error!("{}", e.to_string().as_str());
32//! HttpResponse::new()
33//! .set_status(StatusCode::BAD_REQUEST)
34//! .send()
35//! .unwrap();
36//! return;
37//! }
38//! };
39//!
40//! let res = crate::serverless_function::serverless(req);
41//! match res.send() {
42//! Ok(x) => x,
43//! Err(e) => {
44//! error!("{}", e.to_string().as_str());
45//! }
46//! };
47//! }
48//! ```
49//!
50//! 2. `serverless_function.rs` : This file contains the code to create a Serverless Function.
51//!
52//! ```
53//! pub fn serverless(req: HttpRequest) -> HttpResponse {
54//! return HttpResponse::from("Success from EDJX".to_string())
55//! }
56//! ```
57//!
58//! [`HttpRequest`] is read-only, that is, it has only methods to read input HTTP Request.
59//!
60//! Both [`HttpRequest`] and [`HttpResponse`] APIs are similar to [`standard RUST HTTP interface`](
61//! https://docs.rs/http/0.2.4/http/). The other important HTTP structs like Headers,
62//! Method, StatusCode, and Version are similar to [`standard RUST HTTP interface`](
63//! https://docs.rs/http/0.2.4/http/).
64//!
65//!
66//! Two other important structures in this crate are [`HttpFetch`] and [`FetchResponse`]. [`HttpFetch`] is used
67//! to execute HTTP Requests. [`FetchResponse`] is returned as a response to a Fetch Request. Both
68//! these types APIs are similar to [`standrard RUST HTTP interface`](
69//! https://docs.rs/http/0.2.4/http/)
70//!
71//! ### HTTP body size limits
72//! Currently, all HTTP APIs have a size limit of 512 MB for the HTTP body.
73//!
74//!
75//! ## Interacting with the EDJX Object Store and KV Store
76//!
77//! The most important APIs in the EDJX Library SDK are those used to interact with the
78//! Object Storage (represented by [`storage`] in the EDJX SDK ) and the
79//! KV Store (represented by [`kv`] in the EDJX SDK)
80//!
81//! The Object Store is a decentralized datastore backed by the EDJX P2P network.
82//! This SDK uses GET and PUT methods for file contents and associated attributes.
83//!
84//! The KV Store is a decentralized Key-Value datastore backed by the EDJX P2P network.
85//! Key size is limited to 512 bytes and Value is limited to 512 KB.
86//!
87//!
88//! ## Logging
89//! This SDK provides generic Rust-compatible logger APIs similar to
90//! [`standard RUST Log`](https://docs.rs/log/0.4.14/log/).
91
92pub mod error;
93pub mod fetch;
94mod fetch_response;
95pub mod kv;
96
97#[doc(hidden)]
98pub mod logger;
99
100pub mod request;
101pub mod response;
102mod result;
103pub mod storage;
104mod storage_response;
105pub mod stream;
106mod utils;
107
108pub use ::http::{Method, StatusCode, Uri};
109pub use fetch::HttpFetch;
110pub use fetch_response::{FetchResponse, FetchResponsePending};
111pub use http::header::{HeaderMap, HeaderName, HeaderValue};
112pub use logger::LogLevel;
113pub use request::{HttpMethod, HttpRequest};
114pub use response::HttpResponse;
115pub use storage_response::{FileAttributes, StorageResponse, StorageResponsePending};
116pub use stream::{BaseStream, ReadStream, WriteStream};