pub trait Adapter: Send + Sync {
// Required methods
fn list(
&self,
params: &JsonObject,
) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>;
fn get(
&self,
id: &str,
params: &JsonObject,
) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>;
fn post(
&self,
data: &JsonObject,
params: &JsonObject,
) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>;
fn patch(
&self,
id: &str,
data: &JsonObject,
params: &JsonObject,
) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>;
fn delete(
&self,
id: &str,
params: &JsonObject,
) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>;
// Provided method
fn handle(&self, req: Request) -> BoxFuture<Reply, Error> { ... }
}
Expand description
Converts a Request to a static Reply from a database.
An Adapter
talks to a database. If you’re using an Adapter
, and not implementing your own, you
probably just want to use the handle
function. By implementing the five functions list
, get
,
post
, patch
and delete
, an Adapter
gets the handle
function for free.
You most likely won’t want to implement your own Adapter, since these are generic and don’t contain
project-specific code. Backtalk implements memory::MemoryAdapter
for development, but you will
hopefully eventually be able to find third-party adapters for various databases in other crates.
Required Methods§
fn list( &self, params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>
fn get( &self, id: &str, params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>
fn post( &self, data: &JsonObject, params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>
fn patch( &self, id: &str, data: &JsonObject, params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>
fn delete( &self, id: &str, params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>
Provided Methods§
Sourcefn handle(&self, req: Request) -> BoxFuture<Reply, Error>
fn handle(&self, req: Request) -> BoxFuture<Reply, Error>
Takes a Request
, passes it to the appropriate function, and turns the response into a proper
Reply
future. If you’re using an Adapter
in your webapp, this is the function you want to
call.
Examples found in repository?
More examples
8fn main() {
9 let mut server = Server::new();
10 use std::sync::Arc;
11 let database = Arc::new(memory::MemoryAdapter::new());
12 server.resource("/cats", move |req: Request| {
13 let database1 = database.clone();
14 req
15 .and_then(move |req| {
16 database1.handle(req)
17 })
18 });
19 server.listen("127.0.0.1:3000");
20}
8fn main() {
9 let mut server = Server::new();
10 let database = memory::MemoryAdapter::new();
11 server.resource("/cats", move |req: Request| {
12 database.handle(req).and_then(|mut reply| {
13 {
14 let mut data = reply.data_mut().unwrap();
15 data.insert("example".to_string(), json!("data"));
16 }
17 reply
18 })
19 });
20 server.listen("127.0.0.1:3000");
21}
8fn main() {
9 let mut server = Server::new();
10 use std::sync::Arc;
11 use std::ops::Deref;
12 let database = Arc::new(memory::MemoryAdapter::new());
13 let chan = Arc::new(memory::MemoryChannel::new());
14 server.resource("/cats", move |req: Request| {
15 let database1 = database.clone();
16 let chan1 = chan.clone();
17 let chan2 = chan.clone();
18 req
19 .and_then(move |req| {
20 match req.method() {
21 Method::Listen => chan1.handle(req),
22 _ => database1.handle(req),
23 }
24 })
25 .and_then(move |reply| {
26 util::send_from_reply(reply, chan2.deref())
27 })
28 });
29 server.listen("127.0.0.1:3000");
30}
8fn main() {
9 let mut server = Server::new();
10 use std::sync::Arc;
11 let database = Arc::new(memory::MemoryAdapter::new());
12 server.resource("/cats", move |req: Request| {
13 let database1 = database.clone();
14 req
15 .and_then(move |req| {
16 if req.method() == Method::Delete {
17 if let &JsonValue::String(ref password) = req.param("password") {
18 if password != "meow" {
19 return Error::forbidden("incorrect password");
20 }
21 } else {
22 return Error::unauthorized("please provide a password");
23 }
24 }
25 req.boxed()
26 })
27 .and_then(move |req| {
28 database1.handle(req)
29 })
30 });
31 server.listen("127.0.0.1:3000");
32}
7fn main() {
8 let mut s = Server::new();
9 s.resource("/meow", |_req: Request| {
10 Error::forbidden("not allowed! sorry.")
11 });
12 let adapter = Arc::new(memory::MemoryAdapter::new());
13 let channel = Arc::new(memory::MemoryChannel::new());
14 s.resource("/hello2", move |req: Request| {
15 req
16 .and_then(|req| {
17 req.into_reply(JsonObject::new())
18 })
19 });
20 s.resource("/hello", move |req: Request| {
21 let adapter = adapter.clone();
22 let channel1 = channel.clone();
23 // let channel2 = channel.clone();
24 req
25 .and_then(move |req| {
26 match req.method().clone() {
27 Method::Action(_) => Error::forbidden("not allowed! sorry."),
28 Method::Listen => channel1.handle(req),
29 _ => adapter.handle(req),
30 }
31 })
32 // .and_then(move |reply| { util::send_from_reply(reply, channel2.deref()) })
33 });
34 s.listen("127.0.0.1:3000");
35}