-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheets.rs
More file actions
113 lines (104 loc) · 3.22 KB
/
sheets.rs
File metadata and controls
113 lines (104 loc) · 3.22 KB
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
use anyhow::Context;
use http::Uri;
use sheets::{spreadsheets::Spreadsheets, types::CellData};
use tower_sessions::Session;
use crate::{
google_auth::{make_redirect_uri, redirect_endpoint, GoogleScope},
Error, ServerState,
};
pub(crate) fn cell_string(cell: &CellData) -> Result<String, anyhow::Error> {
let value = cell.effective_value.clone();
if let Some(value) = value {
Ok(value.string_value)
} else {
Ok(String::new())
}
}
pub(crate) fn cell_bool(cell: &CellData) -> Result<bool, anyhow::Error> {
let value = cell.effective_value.clone();
if let Some(value) = value {
Ok(value.bool_value)
} else {
Ok(false)
}
}
pub(crate) fn cell_date(cell: &CellData) -> Result<chrono::NaiveDate, anyhow::Error> {
let date_string = &cell.formatted_value;
chrono::NaiveDate::parse_from_str(date_string, "%Y-%m-%d")
.with_context(|| format!("Failed to parse {} as a date", date_string))
}
pub(crate) async fn sheets_client(
session: &Session,
server_state: ServerState,
original_uri: Uri,
) -> Result<SheetsClient, Error> {
let maybe_token: Option<String> = session
.get(GoogleScope::Sheets.token_session_key())
.await
.context("Session load error")?;
let redirect_endpoint = redirect_endpoint(&server_state);
if let Some(token) = maybe_token {
let client = ::sheets::Client::new(
server_state.config.google_apis_client_id.clone(),
server_state.config.google_apis_client_secret.to_string(),
&redirect_endpoint,
token,
"",
);
Ok(SheetsClient {
client,
original_uri,
server_state,
})
} else {
Err(Error::Redirect(
make_redirect_uri(
&server_state,
original_uri,
&redirect_endpoint,
GoogleScope::Sheets,
)
.await?,
))
}
}
#[derive(Clone)]
pub struct SheetsClient {
client: ::sheets::Client,
original_uri: Uri,
server_state: ServerState,
}
impl SheetsClient {
pub async fn get(
self,
sheet_id: &str,
include_grid_data: bool,
ranges: &[String],
) -> Result<::sheets::Response<::sheets::types::Spreadsheet>, Error> {
let result = Spreadsheets {
client: self.client,
}
.get(sheet_id, include_grid_data, ranges)
.await;
match result {
Ok(value) => Ok(value),
Err(::sheets::ClientError::HttpError { status, .. }) if status.as_u16() == 401 => {
Err(Error::Redirect(
make_redirect_uri(
&self.server_state,
self.original_uri,
&redirect_endpoint(&self.server_state),
GoogleScope::Sheets,
)
.await?,
))
}
Err(err @ ::sheets::ClientError::HttpError { status, .. })
if status.as_u16() == 403 =>
{
Err(Error::PotentiallyIgnorablePermissions(err.into()))
}
Err(err) => Err(Error::Fatal(err.into())),
}
}
}