Skip to content

Commit 6ee9f48

Browse files
committed
chore(api): consistent scoping in lib
1 parent b0f121e commit 6ee9f48

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

crates/api/src/lib.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#![warn(clippy::pedantic)]
22
use std::time::Duration;
33

4-
use poem::{endpoint::BoxEndpoint, EndpointExt};
5-
use poem_openapi::{payload::PlainText, ApiResponse, OpenApi, OpenApiService};
4+
use poem::{
5+
endpoint::BoxEndpoint, middleware, web::Redirect, EndpointExt, FromRequest, Request,
6+
RequestBody, Route,
7+
};
8+
use poem_openapi::{param, payload, ApiResponse, OpenApi, OpenApiService};
69
use serde::{Deserialize, Serialize};
710
use sqlx::{
811
pool::{PoolConnection, PoolOptions},
@@ -14,8 +17,8 @@ use pr_tracker_store::{ForPrError, Landing, PrNumberNonPositiveError};
1417
const DOCS_PATH: &str = "/api-docs";
1518

1619
#[poem::handler]
17-
fn index() -> poem::web::Redirect {
18-
poem::web::Redirect::see_other(DOCS_PATH)
20+
fn index() -> Redirect {
21+
Redirect::see_other(DOCS_PATH)
1922
}
2023

2124
/// # Panics
@@ -34,22 +37,19 @@ pub async fn endpoint(db_url: &str) -> BoxEndpoint<'static> {
3437

3538
util::migrate(&db_pool).await.unwrap();
3639

37-
poem::Route::new()
40+
Route::new()
3841
.at("/", poem::get(index))
3942
.nest(DOCS_PATH, api_service.swagger_ui())
4043
.at("/openapi.json", api_service.spec_endpoint())
4144
.nest(API_PREFIX, api_service)
42-
.with(poem::middleware::AddData::new(db_pool))
45+
.with(middleware::AddData::new(db_pool))
4346
.boxed()
4447
}
4548

4649
pub struct DbConnection(PoolConnection<Postgres>);
4750

48-
impl<'a> poem::FromRequest<'a> for DbConnection {
49-
async fn from_request(
50-
req: &'a poem::Request,
51-
_body: &mut poem::RequestBody,
52-
) -> poem::Result<Self> {
51+
impl<'a> FromRequest<'a> for DbConnection {
52+
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> poem::Result<Self> {
5353
let pool = req.extensions()
5454
.get::<Pool<Postgres>>()
5555
.expect("Could not find a db pool on `req.extensions`. Perhaps you forgot to register an `AddData` middleware that adds it?");
@@ -67,23 +67,26 @@ impl Api {
6767
#[oai(path = "/:pr", method = "get")]
6868
async fn landed(
6969
&self,
70-
poem_openapi::param::Path(pr): poem_openapi::param::Path<i32>,
70+
param::Path(pr): param::Path<i32>,
7171
DbConnection(mut conn): DbConnection,
72-
) -> poem::Result<poem_openapi::payload::Json<LandedIn>, LandedError> {
72+
) -> poem::Result<payload::Json<LandedIn>, LandedError> {
7373
let landings = Landing::for_pr(&mut conn, pr.try_into()?).await?;
7474

7575
let branches = landings
7676
.into_iter()
7777
.map(|branch| Branch::new(branch.name()))
7878
.collect();
7979

80-
Ok(poem_openapi::payload::Json(LandedIn { branches }))
80+
Ok(payload::Json(LandedIn { branches }))
8181
}
8282

8383
#[oai(path = "/healthcheck", method = "get")]
8484
#[allow(clippy::unused_async)]
85-
async fn health_check(&self, DbConnection(_conn): DbConnection) -> PlainText<&'static str> {
86-
PlainText("Here is your 200, but in the body")
85+
async fn health_check(
86+
&self,
87+
DbConnection(_conn): DbConnection,
88+
) -> payload::PlainText<&'static str> {
89+
payload::PlainText("Here is your 200, but in the body")
8790
}
8891
}
8992

@@ -104,29 +107,29 @@ pub struct LandedIn {
104107
#[derive(Debug, ApiResponse)]
105108
enum LandedError {
106109
#[oai(status = 400)]
107-
PrNumberNonPositive(PlainText<String>),
110+
PrNumberNonPositive(payload::PlainText<String>),
108111

109112
#[oai(status = 500)]
110-
Sqlx(PlainText<String>),
113+
Sqlx(payload::PlainText<String>),
111114

112115
#[oai(status = 404)]
113-
PrNotFound(PlainText<String>),
116+
PrNotFound(payload::PlainText<String>),
114117
}
115118

116119
impl From<PrNumberNonPositiveError> for LandedError {
117120
fn from(_: PrNumberNonPositiveError) -> Self {
118-
Self::PrNumberNonPositive(PlainText(String::from("Pull request number non-positive.")))
121+
Self::PrNumberNonPositive(payload::PlainText(String::from(
122+
"Pull request number non-positive.",
123+
)))
119124
}
120125
}
121126

122127
impl From<ForPrError> for LandedError {
123128
fn from(value: ForPrError) -> Self {
124129
match value {
125-
ForPrError::Sqlx(_) => Self::Sqlx(poem_openapi::payload::PlainText(String::from(
126-
"Error. Sorry.",
127-
))),
130+
ForPrError::Sqlx(_) => Self::Sqlx(payload::PlainText(String::from("Error. Sorry."))),
128131
ForPrError::PrNotFound => {
129-
Self::PrNotFound(PlainText(String::from("Pull request not found.")))
132+
Self::PrNotFound(payload::PlainText(String::from("Pull request not found.")))
130133
}
131134
}
132135
}

0 commit comments

Comments
 (0)