-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
144 lines (134 loc) · 4.4 KB
/
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
pub mod event;
pub mod event_data;
pub mod event_id;
pub mod event_store;
pub mod event_stream_id;
pub mod event_stream_seq;
pub mod firestore_rest;
use std::{collections::HashMap, env};
use anyhow::ensure;
use reqwest::Response;
use crate::firestore_rest::{
BeginTransactionRequestBody, BeginTransactionResponse, Document, TransactionOptions, Value,
};
async fn begin_transaction_example() -> anyhow::Result<BeginTransactionResponse> {
let bearer_token = env::var("GOOGLE_BEARER_TOKEN")?;
let project_id = env::var("PROJECT_ID")?;
let database_id = "(default)";
let database = format!("projects/{}/databases/{}", project_id, database_id);
let response = firestore_rest::begin_transaction(
(&bearer_token, &project_id),
&database,
BeginTransactionRequestBody {
options: TransactionOptions::ReadWrite {
retry_transaction: None,
},
},
)
.await?;
ensure!(response.status() == 200);
let response: BeginTransactionResponse = response.json().await?;
Ok(response)
}
// select (one)
async fn get_example() -> anyhow::Result<Document> {
let bearer_token = env::var("GOOGLE_BEARER_TOKEN")?;
let project_id = env::var("PROJECT_ID")?;
let database_id = "(default)";
let collection_id = "cities";
let document_id = "LA";
let document_path = format!("{}/{}", collection_id, document_id);
let name = format!(
"projects/{}/databases/{}/documents/{}",
project_id, database_id, document_path
);
let response =
firestore_rest::get((&bearer_token, &project_id), &name, None, None, None).await?;
ensure!(response.status() == 200);
let document: Document = response.json().await?;
Ok(document)
}
// insert
async fn create_document_example() -> anyhow::Result<Response> {
let bearer_token = env::var("GOOGLE_BEARER_TOKEN")?;
let project_id = env::var("PROJECT_ID")?;
let database_id = "(default)";
let parent = format!(
"projects/{}/databases/{}/documents",
project_id, database_id,
);
let collection_id = "cities";
let document_id = "LA";
let document = Document {
name: "unused".to_string(),
fields: {
let mut map = HashMap::new();
map.insert("name".to_string(), Value::String("Los Angeles".to_string()));
map.insert("state".to_string(), Value::String("CA".to_string()));
map.insert("country".to_string(), Value::String("USA".to_string()));
map
},
create_time: "unused".to_string(),
update_time: "unused".to_string(),
};
firestore_rest::create_document(
(&bearer_token, &project_id),
&parent,
collection_id,
Some(document_id),
None,
document,
)
.await
}
// update or insert
async fn patch_example() -> anyhow::Result<Response> {
let bearer_token = env::var("GOOGLE_BEARER_TOKEN")?;
let project_id = env::var("PROJECT_ID")?;
let database_id = "(default)";
let collection_id = "cities";
let document_id = "LA2";
let document_name = format!(
"projects/{}/databases/{}/documents/{}/{}",
project_id, database_id, collection_id, document_id
);
let document = Document {
name: "unused".to_string(),
fields: {
let mut map = HashMap::new();
map.insert("name".to_string(), Value::String("Los Angeles".to_string()));
map.insert("state".to_string(), Value::String("CA".to_string()));
map.insert("country".to_string(), Value::String("USA".to_string()));
map
},
create_time: "unused".to_string(),
update_time: "unused".to_string(),
};
firestore_rest::patch(
(&bearer_token, &project_id),
&document_name,
None,
None,
None,
None,
document,
)
.await
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
#[ignore]
async fn test() -> anyhow::Result<()> {
let response = begin_transaction_example().await?;
assert_eq!(serde_json::to_string(&response)?, "");
// let response = create_document_example().await?;
// let status = response.status();
// assert_eq!(status, 200);
// let response = patch_example().await?;
// let document = get_example().await?;
// assert_eq!(serde_json::to_string(&document)?, "");
Ok(())
}
}