Skip to content

Commit 7d2ea07

Browse files
committed
Remove duplicate token methods
1 parent 308133c commit 7d2ea07

File tree

1 file changed

+4
-35
lines changed

1 file changed

+4
-35
lines changed

psst-gui/src/webapi/client.rs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl WebApi {
9494

9595
// Use the current OAuth bearer for the request.
9696
let token = self.access_token()?;
97-
let result = match request.get_method() {
97+
let call = |token: &str| match request.get_method() {
9898
Method::Get => {
9999
let mut req = self
100100
.agent
@@ -123,54 +123,23 @@ impl WebApi {
123123
.send_json(request.get_body()),
124124
};
125125

126-
let mut response = match result {
126+
let mut response = match call(&token) {
127127
Ok(resp) => resp,
128128
Err(err) => return Err(Error::WebApiError(err.to_string())),
129129
};
130130

131-
// Tiny refresh-and-retry: if unauthorized/forbidden and we have a refresh token, refresh once.
131+
// If unauthorized/forbidden, refresh once (if possible) and retry.
132132
if matches!(
133133
response.status(),
134134
StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN
135135
) {
136136
if let Some(rtok) = self.oauth_refresh_token.lock().clone() {
137137
if let Ok((new_access, new_refresh)) = refresh_access_token(&rtok) {
138-
// Update bearer (and refresh token if rotated), then retry once.
139138
*self.oauth_bearer.lock() = Some(new_access.clone());
140139
if let Some(r) = new_refresh {
141140
*self.oauth_refresh_token.lock() = Some(r);
142141
}
143-
let token = new_access;
144-
let result = match request.get_method() {
145-
Method::Get => {
146-
let mut req = self
147-
.agent
148-
.get(request.build())
149-
.header("Authorization", &format!("Bearer {token}"));
150-
for header in request.get_headers() {
151-
req = req.header(header.0, header.1);
152-
}
153-
req.call()
154-
}
155-
Method::Post => self
156-
.agent
157-
.post(request.build())
158-
.header("Authorization", &format!("Bearer {token}"))
159-
.send_json(request.get_body()),
160-
Method::Put => self
161-
.agent
162-
.put(request.build())
163-
.header("Authorization", &format!("Bearer {token}"))
164-
.send_json(request.get_body()),
165-
Method::Delete => self
166-
.agent
167-
.delete(request.build())
168-
.header("Authorization", &format!("Bearer {token}"))
169-
.force_send_body()
170-
.send_json(request.get_body()),
171-
};
172-
173-
response = match result {
142+
response = match call(&new_access) {
174143
Ok(resp) => resp,
175144
Err(err) => return Err(Error::WebApiError(err.to_string())),
176145
};

0 commit comments

Comments
 (0)