mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
app: Refactor API calls
This commit is contained in:
parent
e07efc9585
commit
9899c6f5aa
105
app/src/api.rs
105
app/src/api.rs
@ -22,30 +22,41 @@ fn get_claims_from_jwt(jwt: &str) -> Result<JWTClaims> {
|
|||||||
Ok(token.claims().clone())
|
Ok(token.claims().clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_handler<Resp, CallbackResult, F>(
|
||||||
|
callback: Callback<Result<CallbackResult>>,
|
||||||
|
handler: F,
|
||||||
|
) -> Callback<Response<Result<Resp>>>
|
||||||
|
where
|
||||||
|
F: Fn(http::StatusCode, Resp) -> Result<CallbackResult> + 'static,
|
||||||
|
Resp: std::fmt::Display,
|
||||||
|
CallbackResult: 'static,
|
||||||
|
{
|
||||||
|
Callback::once(move |response: Response<Result<Resp>>| {
|
||||||
|
let (meta, maybe_data) = response.into_parts();
|
||||||
|
let message = maybe_data
|
||||||
|
.map_err(|e| anyhow!("Could not reach server: {}", e))
|
||||||
|
.and_then(|data| handler(meta.status, data));
|
||||||
|
callback.emit(message)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
impl HostService {
|
impl HostService {
|
||||||
pub fn list_users(
|
pub fn list_users(
|
||||||
request: ListUsersRequest,
|
request: ListUsersRequest,
|
||||||
callback: Callback<Result<Vec<User>>>,
|
callback: Callback<Result<Vec<User>>>,
|
||||||
) -> Result<FetchTask> {
|
) -> Result<FetchTask> {
|
||||||
let url = "/api/users";
|
let url = "/api/users";
|
||||||
let handler = move |response: Response<Result<String>>| {
|
|
||||||
let (meta, maybe_data) = response.into_parts();
|
|
||||||
let message = maybe_data
|
|
||||||
.map_err(|e| anyhow!("Could not fetch: {}", e))
|
|
||||||
.and_then(|data| {
|
|
||||||
if meta.status.is_success() {
|
|
||||||
serde_json::from_str(&data)
|
|
||||||
.map_err(|e| anyhow!("Could not parse response: {}", e))
|
|
||||||
} else {
|
|
||||||
Err(anyhow!("[{}]: {}", meta.status, data))
|
|
||||||
}
|
|
||||||
});
|
|
||||||
callback.emit(message)
|
|
||||||
};
|
|
||||||
let request = Request::post(url)
|
let request = Request::post(url)
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.body(Json(&request))?;
|
.body(Json(&request))?;
|
||||||
FetchService::fetch_with_options(request, get_default_options(), handler.into())
|
let handler = create_handler(callback, |status, data: String| {
|
||||||
|
if status.is_success() {
|
||||||
|
serde_json::from_str(&data).map_err(|e| anyhow!("Could not parse response: {}", e))
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("[{}]: {}", status, data))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
FetchService::fetch_with_options(request, get_default_options(), handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn authenticate(
|
pub fn authenticate(
|
||||||
@ -53,53 +64,37 @@ impl HostService {
|
|||||||
callback: Callback<Result<String>>,
|
callback: Callback<Result<String>>,
|
||||||
) -> Result<FetchTask> {
|
) -> Result<FetchTask> {
|
||||||
let url = "/auth";
|
let url = "/auth";
|
||||||
let handler = move |response: Response<Result<String>>| {
|
|
||||||
let (meta, maybe_data) = response.into_parts();
|
|
||||||
let message = maybe_data
|
|
||||||
.map_err(|e| anyhow!("Could not reach authentication server: {}", e))
|
|
||||||
.and_then(|data| {
|
|
||||||
if meta.status.is_success() {
|
|
||||||
get_claims_from_jwt(&data)
|
|
||||||
.map_err(|e| anyhow!("Could not parse response: {}", e))
|
|
||||||
.and_then(|jwt_claims| {
|
|
||||||
set_cookie("user_id", &jwt_claims.user, &jwt_claims.exp)
|
|
||||||
.map(|_| jwt_claims.user.clone())
|
|
||||||
.map_err(|e| anyhow!("Error clearing cookie: {}", e))
|
|
||||||
})
|
|
||||||
} else if meta.status == 401 {
|
|
||||||
Err(anyhow!("Invalid username or password"))
|
|
||||||
} else {
|
|
||||||
Err(anyhow!(
|
|
||||||
"Could not authenticate: [{}]: {}",
|
|
||||||
meta.status,
|
|
||||||
data
|
|
||||||
))
|
|
||||||
}
|
|
||||||
});
|
|
||||||
callback.emit(message)
|
|
||||||
};
|
|
||||||
let request = Request::post(url)
|
let request = Request::post(url)
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.body(Json(&request))?;
|
.body(Json(&request))?;
|
||||||
FetchService::fetch_with_options(request, get_default_options(), handler.into())
|
let handler = create_handler(callback, |status, data: String| {
|
||||||
|
if status.is_success() {
|
||||||
|
get_claims_from_jwt(&data)
|
||||||
|
.map_err(|e| anyhow!("Could not parse response: {}", e))
|
||||||
|
.and_then(|jwt_claims| {
|
||||||
|
set_cookie("user_id", &jwt_claims.user, &jwt_claims.exp)
|
||||||
|
.map(|_| jwt_claims.user.clone())
|
||||||
|
.map_err(|e| anyhow!("Error clearing cookie: {}", e))
|
||||||
|
})
|
||||||
|
} else if status == 401 {
|
||||||
|
Err(anyhow!("Invalid username or password"))
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("Could not authenticate: [{}]: {}", status, data))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
FetchService::fetch_with_options(request, get_default_options(), handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn logout(callback: Callback<Result<()>>) -> Result<FetchTask> {
|
pub fn logout(callback: Callback<Result<()>>) -> Result<FetchTask> {
|
||||||
let url = "/auth/logout";
|
let url = "/auth/logout";
|
||||||
let handler = move |response: Response<Result<String>>| {
|
|
||||||
let (meta, maybe_data) = response.into_parts();
|
|
||||||
let message = maybe_data
|
|
||||||
.map_err(|e| anyhow!("Could not reach authentication server: {}", e))
|
|
||||||
.and_then(|data| {
|
|
||||||
if meta.status.is_success() {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(anyhow!("Could not logout: [{}]: {}", meta.status, data))
|
|
||||||
}
|
|
||||||
});
|
|
||||||
callback.emit(message)
|
|
||||||
};
|
|
||||||
let request = Request::post(url).body(yew::format::Nothing)?;
|
let request = Request::post(url).body(yew::format::Nothing)?;
|
||||||
FetchService::fetch_with_options(request, get_default_options(), handler.into())
|
let handler = create_handler(callback, |status, data: String| {
|
||||||
|
if status.is_success() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("Could not logout: [{}]: {}", status, data))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
FetchService::fetch_with_options(request, get_default_options(), handler)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user