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
145
146
|
//! Publications domain endpoint: which traits a paper reports.
use axum::Json;
use axum::extract::Path;
use axum::http::StatusCode;
use std::collections::HashMap;
use crate::auth::{AuthToken, is_authorised};
use crate::config::Config;
use crate::db;
use crate::error::{ApiError, Unauthorized};
use crate::response::{Ctx, Envelope, path_segment};
enum PublicationTraits {
NotFound,
/// The traits the caller may view, and how many were left out.
Found(Vec<db::TraitInfo>, usize),
}
enum LookupError {
Db(db::DbError),
Auth(String),
}
impl From<db::DbError> for LookupError {
fn from(e: db::DbError) -> Self {
LookupError::Db(e)
}
}
/// The publication's traits, minus those of datasets the caller may
/// not view (each dataset is checked once, as in GET /traits/{dataset}).
fn get_publication_traits_result(
pubmed_id: u32,
auth_server_url: &str,
token: &AuthToken,
) -> Result<PublicationTraits, LookupError> {
let Some(traits) = db::get_publication_traits(pubmed_id)? else {
return Ok(PublicationTraits::NotFound);
};
let mut readable: HashMap<String, bool> = HashMap::new();
let mut visible = Vec::new();
let mut omitted = 0;
for info in traits {
let Some(dataset) = info.dataset.clone() else {
omitted += 1;
continue;
};
if !readable.contains_key(&dataset) {
let allowed = match db::get_publish_freeze_by_name(&dataset)? {
Some(pf) => is_authorised(
auth_server_url,
token,
&pf.name,
None,
pf.public,
pf.confidentiality,
&pf.create_time,
)
.map_err(LookupError::Auth)?,
None => false,
};
readable.insert(dataset.clone(), allowed);
}
if readable[&dataset] {
visible.push(info);
} else {
omitted += 1;
}
}
Ok(PublicationTraits::Found(visible, omitted))
}
#[utoipa::path(get, path = "/publications/{pubmed_id}/traits", summary = "List the published traits a publication reports", description = "Every published (phenotype) trait, across groups and datasets, whose publication has this PubMed id. Traits of datasets the caller isn't authorized to view are left out.", params(
("pubmed_id" = u32, Path, description = "The publication's PubMed id, e.g. 31634382")
), responses(
(status = 200, description = "The publication's traits (possibly none)", body = Envelope<Vec<db::TraitInfo>>),
Unauthorized,
(status = 404, description = "No publication with this PubMed id", body = ApiError),
(status = 500, description = "Database error", body = ApiError),
(status = 502, description = "Could not reach gn-auth to check authorization", body = ApiError)
))]
pub async fn publication_traits(
ctx: Ctx,
Path(pubmed_id): Path<u32>,
token: AuthToken,
) -> Result<Json<Envelope<Vec<db::TraitInfo>>>, ApiError> {
let rid = ctx.request_info();
let auth_server_url = Config::load().auth_server_url.clone();
tokio::task::spawn_blocking(move || {
get_publication_traits_result(pubmed_id, &auth_server_url, &token)
})
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"publication_traits",
)
.with_request_id(rid.clone())
})?
.map_err(|e| match e {
LookupError::Db(e) => {
ApiError::from_db(e, "publication_traits").with_request_id(rid.clone())
}
LookupError::Auth(e) => ApiError::new(
StatusCode::BAD_GATEWAY,
"auth_server_error",
"Could not check authorization",
e,
"publication_traits",
)
.with_request_id(rid.clone()),
})
.and_then(|lookup| match lookup {
PublicationTraits::Found(traits, omitted) => {
let first = traits
.first()
.and_then(|t| Some((t.dataset.clone()?, t.id)));
let mut envelope = ctx.envelope(traits).with_note(db::LOD_NOTE);
if omitted > 0 {
envelope = envelope.partial(&format!(
"{omitted} trait(s) were left out: their datasets are not public \
and you are not authorized to view them."
));
}
if let Some((dataset, id)) = first {
envelope = envelope.with_next(
&format!("/traits/{}/{id}", path_segment(&dataset)),
"Metadata of the first trait",
);
}
Ok(Json(envelope.with_links(crate::suggest::root_links())))
}
PublicationTraits::NotFound => Err(ApiError::new(
StatusCode::NOT_FOUND,
"publication_not_found",
"Publication not found",
format!("No publication with PubMed id {pubmed_id}."),
"publication_traits",
)
.with_request_id(rid)),
})
}
|