Skip to content

Commit 17d7476

Browse files
committed
controllers/version/downloads: Extract DownloadsQueryParams struct
1 parent d4c7c18 commit 17d7476

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ tracing-subscriber = { version = "=0.3.19", features = ["env-filter", "json"] }
125125
typomania = { version = "=0.1.2", default-features = false }
126126
url = "=2.5.4"
127127
unicode-xid = "=0.2.6"
128-
utoipa = "=5.2.0"
128+
utoipa = { version = "=5.2.0", features = ["chrono"] }
129129
utoipa-axum = "=0.1.2"
130130

131131
[dev-dependencies]

src/controllers/version/downloads.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::schema::*;
99
use crate::util::errors::AppResult;
1010
use crate::util::{redirect, RequestUtils};
1111
use crate::views::EncodableVersionDownload;
12+
use axum::extract::{FromRequestParts, Query};
1213
use axum::response::{IntoResponse, Response};
1314
use axum_extra::json;
1415
use axum_extra::response::ErasedJson;
@@ -41,29 +42,37 @@ pub async fn download_version(
4142
}
4243
}
4344

45+
#[derive(Debug, Deserialize, FromRequestParts, utoipa::IntoParams)]
46+
#[from_request(via(Query))]
47+
#[into_params(parameter_in = Query)]
48+
pub struct DownloadsQueryParams {
49+
/// Only return download counts before this date.
50+
#[param(example = "2024-06-28")]
51+
before_date: Option<NaiveDate>,
52+
}
53+
4454
/// Get the download counts for a crate version.
4555
///
4656
/// This includes the per-day downloads for the last 90 days.
4757
#[utoipa::path(
4858
get,
4959
path = "/api/v1/crates/{name}/{version}/downloads",
50-
params(CrateVersionPath),
60+
params(CrateVersionPath, DownloadsQueryParams),
5161
tag = "versions",
5262
responses((status = 200, description = "Successful Response")),
5363
)]
5464
pub async fn get_version_downloads(
5565
app: AppState,
5666
path: CrateVersionPath,
57-
req: Parts,
67+
params: DownloadsQueryParams,
5868
) -> AppResult<ErasedJson> {
5969
let mut conn = app.db_read().await?;
6070
let version = path.load_version(&mut conn).await?;
6171

62-
let cutoff_end_date = req
63-
.query()
64-
.get("before_date")
65-
.and_then(|d| NaiveDate::parse_from_str(d, "%F").ok())
72+
let cutoff_end_date = params
73+
.before_date
6674
.unwrap_or_else(|| Utc::now().date_naive());
75+
6776
let cutoff_start_date = cutoff_end_date - Duration::days(89);
6877

6978
let downloads = VersionDownload::belonging_to(&version)

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,20 @@ snapshot_kind: text
10441044
"schema": {
10451045
"type": "string"
10461046
}
1047+
},
1048+
{
1049+
"description": "Only return download counts before this date.",
1050+
"example": "2024-06-28",
1051+
"in": "query",
1052+
"name": "before_date",
1053+
"required": false,
1054+
"schema": {
1055+
"format": "date",
1056+
"type": [
1057+
"string",
1058+
"null"
1059+
]
1060+
}
10471061
}
10481062
],
10491063
"responses": {

0 commit comments

Comments
 (0)