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
use serde::{Deserialize, Serialize};
pub mod sync {
use super::{InsertPayload, InsertRequestQuery, InsertResponse};
use crate::inner_client::sync::InnerClient;
use crate::Error;
use serde::Serialize;
pub struct InsertRequest<'a, T>
where
T: Serialize,
{
client: InnerClient,
payload: InsertPayload<'a, T>,
query: InsertRequestQuery,
}
impl<'a, T> InsertRequest<'a, T>
where
T: Serialize,
{
pub(crate) fn new(
client: &InnerClient,
document: &'a T,
id: impl Into<Option<String>>,
) -> Self {
InsertRequest {
client: client.duplicate(),
payload: InsertPayload {
_id: id.into(),
payload: document,
},
query: InsertRequestQuery::default(),
}
}
pub fn send(self) -> Result<InsertResponse, Error> {
let response = self
.client
.post()
.json(&self.payload)
.query(&self.query)
.send()?
.json()?;
Ok(response)
}
}
}
pub mod r#async {
use super::{InsertPayload, InsertRequestQuery, InsertResponse};
use crate::inner_client::r#async::InnerClient;
use crate::Error;
use serde::Serialize;
use tokio::prelude::Future;
pub struct InsertRequest<'a, T>
where
T: Serialize,
{
client: InnerClient,
payload: InsertPayload<'a, T>,
query: InsertRequestQuery,
}
impl<'a, T> InsertRequest<'a, T>
where
T: Serialize,
{
pub(crate) fn new(
client: &InnerClient,
document: &'a T,
id: impl Into<Option<String>>,
) -> Self {
InsertRequest {
client: client.duplicate(),
payload: InsertPayload {
_id: id.into(),
payload: document,
},
query: InsertRequestQuery::default(),
}
}
pub fn send(self) -> impl Future<Item = InsertResponse, Error = Error> {
self.client
.post()
.json(&self.payload)
.query(&self.query)
.send()
.and_then(|mut response| response.json())
.map_err(Error::from)
}
}
}
#[derive(Serialize)]
pub struct InsertPayload<'a, T> {
#[serde(skip_serializing_if = "Option::is_none")]
_id: Option<String>,
#[serde(flatten)]
payload: &'a T,
}
#[derive(Serialize, Clone, Default)]
pub struct InsertRequestQuery {
batch: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct InsertResponse {
pub id: String,
pub ok: bool,
pub rev: String,
}