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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
//! Species domain endpoints: read-only access to the Species table.
use axum::Json;
use axum::extract::Path;
use axum::http::StatusCode;
use gnauth_macros::gnauth;
use crate::auth::AuthToken;
use crate::db;
use crate::error::{ApiError, Unauthorized};
use crate::response::{Ctx, Envelope, path_segment};
#[utoipa::path(get, path = "/species", summary = "List all species in the database", responses(
(status = 200, description = "All species in the database", body = Envelope<Vec<db::Species>>),
Unauthorized,
(status = 500, description = "Database error", body = ApiError)
))]
#[gnauth(token_only)]
pub async fn species(
ctx: Ctx,
_token: AuthToken,
) -> Result<Json<Envelope<Vec<db::Species>>>, ApiError> {
let rid = ctx.request_info();
// mysql's Conn is blocking; run it on a blocking thread so it doesn't
// stall the async runtime.
tokio::task::spawn_blocking(db::get_all_species)
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"species",
)
.with_request_id(rid.clone())
})?
.map(|species| {
// A `next` link has to be a call a client can make, so the
// one-species link points at one of the species just listed
// rather than at the `/species/{species}` template: the template
// is a description, not an address, and following it verbatim is a
// 404 (tests/envelope.rs walks every link it is offered).
let example = species.first().map(|first| {
(
format!("/species/{}", path_segment(&first.name)),
format!(
"One species' full record -- {} here; put any other name from the \
list in its place",
first.name
),
)
});
let mut envelope = ctx
.envelope(species)
.with_next(
"/groups",
"Groups (populations); add ?species=<name> to list one species' groups",
)
.with_links(crate::suggest::root_links());
if let Some((href, description)) = example {
envelope = envelope.with_next(&href, &description);
}
Json(envelope)
})
.map_err(|e| ApiError::from_db(e, "species").with_request_id(rid))
}
#[utoipa::path(get, path = "/species/{species}", summary = "Get one species' full record by name", params(
("species" = String, Path, description = "Species Name, FullName or SpeciesName")
), responses(
(status = 200, description = "The matching row of the Species table", body = Envelope<db::SpeciesDetail>),
Unauthorized,
(status = 404, description = "No matching species", body = ApiError),
(status = 500, description = "Database error", body = ApiError)
))]
#[gnauth(token_only)]
pub async fn species_by_name(
ctx: Ctx,
Path(species): Path<String>,
_token: AuthToken,
) -> Result<Json<Envelope<db::SpeciesDetail>>, ApiError> {
let rid = ctx.request_info();
let not_found_name = species.clone();
tokio::task::spawn_blocking(move || db::get_species_by_name(&species))
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"species_by_name",
)
.with_request_id(rid.clone())
})?
.map_err(|e| ApiError::from_db(e, "species_by_name").with_request_id(rid.clone()))?
.map(|detail| {
let name = path_segment(&detail.name);
Json(
ctx.envelope(detail)
.with_next(
&format!("/species/{name}/datasets"),
"Expression datasets of this species",
)
.with_next(&format!("/groups?species={name}"), "Groups of this species")
.with_links(crate::suggest::root_links()),
)
})
.ok_or_else(|| {
ApiError::new(
StatusCode::NOT_FOUND,
"species_not_found",
"Species not found",
format!("No species matching '{not_found_name}'."),
"species_by_name",
)
.with_request_id(rid)
.with_next("/species")
})
}
#[utoipa::path(get, path = "/species/{species}/datasets", summary = "List the expression (ProbeSet) datasets belonging to a species", params(
("species" = String, Path, description = "Species Name, FullName or SpeciesName")
), responses(
(status = 200, description = "The expression (ProbeSet) datasets belonging to the species", body = Envelope<Vec<db::Dataset>>),
Unauthorized,
(status = 404, description = "No matching species", body = ApiError),
(status = 500, description = "Database error", body = ApiError)
))]
#[gnauth(token_only)]
pub async fn species_datasets(
ctx: Ctx,
Path(species): Path<String>,
_token: AuthToken,
) -> Result<Json<Envelope<Vec<db::Dataset>>>, ApiError> {
let rid = ctx.request_info();
let not_found_name = species.clone();
tokio::task::spawn_blocking(move || {
db::get_species_by_name(&species).and_then(|species| match species {
Some(species) => db::get_species_datasets(species.id).map(Some),
None => Ok(None),
})
})
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"species_datasets",
)
.with_request_id(rid.clone())
})?
.map_err(|e| ApiError::from_db(e, "species_datasets").with_request_id(rid.clone()))?
.map(|datasets| {
// One real dataset of this species, for the reason given in
// `/species`: a template is a shape, not an address a client can
// follow.
let example = datasets.first().map(|dataset| {
(
format!("/datasets/{}", path_segment(&dataset.name)),
format!(
"One dataset's record -- {} here; put any other name from \
the list in its place",
dataset.name
),
)
});
let mut envelope = ctx
.envelope(datasets)
.with_next(
&format!("/species/{}", path_segment(¬_found_name)),
"The species these datasets belong to",
)
.with_links(crate::suggest::root_links());
if let Some((href, description)) = example {
envelope = envelope.with_next(&href, &description);
}
Json(envelope)
})
.ok_or_else(|| {
ApiError::new(
StatusCode::NOT_FOUND,
"species_not_found",
"Species not found",
format!("No species matching '{not_found_name}'."),
"species_datasets",
)
.with_request_id(rid)
.with_next("/species")
})
}
|