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
use super::*;
pub fn list<T, BEARER>(auth: &BEARER, collection_id: impl Into<String>) -> List<T, BEARER>
where
BEARER: FirebaseAuthBearer,
{
let collection_id = collection_id.into();
List {
url: firebase_url(auth.project_id(), &collection_id),
auth,
next_page_token: None,
documents: vec![],
current: 0,
done: false,
collection_id,
phantom: std::marker::PhantomData,
}
}
#[inline]
fn get_new_data<'a>(
collection_id: &str,
url: &str,
auth: &'a impl FirebaseAuthBearer,
) -> Result<dto::ListDocumentsResponse> {
let mut resp = auth
.client()
.get(url)
.bearer_auth(auth.access_token().to_owned())
.send()?;
extract_google_api_error(&mut resp, || collection_id.to_owned())?;
let json: dto::ListDocumentsResponse = resp.json()?;
Ok(json)
}
pub struct List<'a, T, BEARER> {
auth: &'a BEARER,
next_page_token: Option<String>,
documents: Vec<dto::Document>,
current: usize,
done: bool,
url: String,
collection_id: String,
phantom: std::marker::PhantomData<T>,
}
impl<'a, T, BEARER> Iterator for List<'a, T, BEARER>
where
for<'b> T: Deserialize<'b>,
BEARER: FirebaseAuthBearer,
{
type Item = Result<(T, dto::Document)>;
fn next(&mut self) -> Option<Self::Item> {
if self.done {
return None;
}
if self.documents.len() <= self.current {
let url = match &self.next_page_token {
Some(next_page_token) => format!("{}pageToken={}", self.url, next_page_token),
None => self.url.clone(),
};
let result = get_new_data(&self.collection_id, &url, self.auth);
match result {
Err(e) => {
self.done = true;
return Some(Err(e));
}
Ok(v) => match v.documents {
None => return None,
Some(documents) => {
self.documents = documents;
self.current = 0;
self.next_page_token = v.next_page_token;
}
},
};
}
let doc = self.documents.get(self.current).unwrap();
self.current += 1;
if self.documents.len() <= self.current && self.next_page_token.is_none() {
self.done = true;
}
let result = document_to_pod(&doc);
match result {
Err(e) => Some(Err(e)),
Ok(pod) => Some(Ok((
pod,
dto::Document {
update_time: doc.update_time.clone(),
create_time: doc.create_time.clone(),
name: doc.name.clone(),
fields: None,
},
))),
}
}
}