rustify/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! # rustify
//!
//! <p align="center">
//!     <a href="https://crates.io/crates/rustify">
//!         <img src="https://img.shields.io/crates/v/rustify">
//!     </a>
//!     <a href="https://docs.rs/rustify">
//!         <img src="https://img.shields.io/docsrs/rustify" />
//!     </a>
//!     <a href="https://github.com/jmgilman/rustify/actions/workflows/ci.yml">
//!         <img src="https://github.com/jmgilman/rustify/actions/workflows/ci.yml/badge.svg"/>
//!     </a>
//! </p>
//!
//! > A Rust library for interacting with HTTP API endpoints
//!
//! Rustify is a small library written in Rust which eases the burden of
//! scaffolding HTTP APIs. It provides an `Endpoint` trait along with a macro helper
//! which allows templating various remote endpoints. Both asynchronous and
//! synchrounous clients are offered for executing requests against endpoints with
//! the option of implementing custom clients using the `Client` trait.
//!
//! Rustify provides support for serializing requests and deserializing responses.
//! Raw requests and responses in the form of bytes are also supported. The library
//! also contains many helpers for dealing with requests like support for middleware
//! and wrapping API responses.
//!
//! ## Installation
//!
//! Add rustify as a dependency to your cargo.toml:
//!
//! ```ignore
//! [dependencies]
//! rustify = "0.6.0"
//! rustify_derive = "0.5.3"
//! ```
//!
//! ## Usage
//!
//! ### Basic
//!
//! ```rust
//! use rustify::{Client, Endpoint};
//! use rustify_derive::Endpoint;
//!
//! // Defines an API endpoint at /test/path that takes no inputs and returns an
//! // empty response.
//! #[derive(Endpoint)]
//! #[endpoint(path = "test/path")]
//! struct Test {}
//!
//! # tokio_test::block_on(async {
//! let endpoint = Test {};
//! let client = Client::default("http://api.com"); // Configures base address of http://api.com
//! let result = endpoint.exec(&client).await; // Sends GET request to http://api.com/test/path
//!
//! // assert!(result.is_ok());
//! # });
//! ```
//!
//!
//! ### Request Body
//!
//! ```rust
//! use derive_builder::Builder;
//! use rustify::{Client, Endpoint};
//! use rustify_derive::Endpoint;
//!
//! // Defines an API endpoint at /test/path/{name} that takes one input for
//! // creating the url and two inputs for building the request body. The content
//! // type of the request body defaults to JSON, however, it can be modified by
//! // passing the `request_type` parameter to the endpoint configuration.
//! //
//! // Note: The `#[endpoint(body)]` attribute tags are technically optional in the
//! // below example. If no `body` attribute is found anywhere then rustify defaults
//! // to serializing all "untagged" fields as part of the body. Fields can be opted
//! // out of this behavior by tagging them with #[endpoint(skip)].
//! #[derive(Builder, Endpoint)]
//! #[endpoint(path = "test/path/{self.name}", method = "POST", builder = "true")]
//! #[builder(setter(into))] // Improves the building process
//! struct Test {
//!     #[endpoint(skip)] // This field shouldn't be serialized anywhere
//!     pub name: String, // Used to create a dynamic URL
//!     #[endpoint(body)] // Instructs rustify to serialize this field as part of the body
//!     pub age: i32,
//!     #[endpoint(body)]
//!     pub role: String,
//! }
//!
//! // Setting `builder` to true creates a `builder()` method on our struct that
//! // returns the TestBuilder type created by `derive_builder`.
//! # tokio_test::block_on(async {
//! let endpoint = Test::builder()
//!         .name("jmgilman")
//!         .age(42)
//!         .role("CEO")
//!         .build()
//!         .unwrap();
//! let client = Client::default("http://api.com");
//! let result = endpoint.exec(&client).await; // Sends POST request to http://api.com/test/path/jmgilman
//!
//! // assert!(result.is_ok());
//! # });
//! ```
//!
//! ### Query Parameters
//!
//! ```rust
//! use derive_builder::Builder;
//! use rustify::{Client, Endpoint};
//! use rustify_derive::Endpoint;
//!
//! // Defines a similar API endpoint as in the previous example but adds an
//! // optional query parameter to the request. Additionally, this example opts to
//! // not specify the `#[endpoint(body)]` attributes to make use of the default
//! // behavior covered in the previous example.
//! #[derive(Default, Builder, Endpoint)]
//! #[endpoint(path = "test/path/{self.name}", method = "POST", builder = "true")]
//! #[builder(setter(into, strip_option), default)] // Improves building process
//! struct Test {
//!     #[endpoint(skip)]
//!     pub name: String,
//!     #[endpoint(query)]
//!     pub scope: Option<String>, // Note: serialization is skipped when this field is None
//!     pub age: i32, // Serialized into the request body
//!     pub role: String, // Serialized into the request body
//! }
//!
//! # tokio_test::block_on(async {
//! let endpoint = Test::builder()
//!         .name("jmgilman")
//!         .scope("global")
//!         .age(42)
//!         .role("CEO")
//!         .build()
//!         .unwrap();
//! let client = Client::default("http://api.com");
//! let result = endpoint.exec(&client).await; // Sends POST request to http://api.com/test/path/jmgilman?scope=global
//!
//! // assert!(result.is_ok());
//! # });
//! ```
//!
//! ### Responses
//!
//! ```should_panic
//! use rustify::{Client, Endpoint};
//! use rustify_derive::Endpoint;
//! use serde::Deserialize;
//!
//! // Defines an API endpoint at /test/path that takes a single byte array which
//! // will be used as the request body (no serialization occurs). The endpoint
//! // returns a `TestResponse` which contains the result of the operation.
//! #[derive(Endpoint)]
//! #[endpoint(path = "test/path", response = "TestResponse")]
//! struct Test {
//!     #[endpoint(raw)] // Indicates this field contains the raw request body
//!     pub file: Vec<u8>
//! }
//!
//! #[derive(Deserialize)]
//! struct TestResponse {
//!     pub success: bool,
//! }
//!
//! # tokio_test::block_on(async {
//! let endpoint = Test {
//!     file: b"contents".to_vec(),
//! };
//! let client = Client::default("http://api.com");
//! let result = endpoint.exec(&client).await;
//!
//! // assert!(result.is_ok());
//!
//! let response = result.unwrap().parse().unwrap(); // Returns the parsed `TestResponse`
//! // dbg!(response.success);
//! # });
//! ```
//!
//! ## Examples
//!
//! You can find example usage in the [examples](examples) directory. They can
//! be run with cargo:
//!
//! ```ignore
//! cargo run --package rustify --example reqres1
//! cargo run --package rustify --example reqres2
//! ```
//!
//! The [vaultrs](https://github.com/jmgilman/vaultrs) crate is built upon rustify
//! and serves as as good reference.
//!
//! ## Features
//! The following features are available for this crate:
//!
//! * `blocking`: Enables the blocking variants of `Client`s as well as the blocking
//!    `exec()` functions in `Endpoint`s.
//!
//! ## Error Handling
//!
//! All errors generated by this crate are wrapped in the `ClientError` enum
//! provided by the crate.
//!
//! ## Testing
//!
//! See the the [tests](tests) directory for tests. Run tests with `cargo test`.
//!
//! ## Contributing
//!
//! Check out the [issues][1] for items needing attention or submit your own and
//! then:
//!
//! 1. Fork it (https://github.com/jmgilman/rustify/fork)
//! 2. Create your feature branch (git checkout -b feature/fooBar)
//! 3. Commit your changes (git commit -am 'Add some fooBar')
//! 4. Push to the branch (git push origin feature/fooBar)
//! 5. Create a new Pull Request
//!
//! [1]: https://github.com/jmgilman/rustify/issues

#[macro_use]
extern crate tracing;

#[cfg(feature = "blocking")]
pub mod blocking;
pub mod client;
pub mod clients;
pub mod endpoint;
pub mod enums;
pub mod errors;
pub mod http;

#[doc(hidden)]
#[path = "private/mod.rs"]
pub mod __private;

pub use crate::{
    clients::reqwest::Client,
    endpoint::{Endpoint, MiddleWare, Wrapper},
};