@@ -9,6 +9,7 @@ use crate::schema::*;
99use crate :: util:: errors:: AppResult ;
1010use crate :: util:: { redirect, RequestUtils } ;
1111use crate :: views:: EncodableVersionDownload ;
12+ use axum:: extract:: { FromRequestParts , Query } ;
1213use axum:: response:: { IntoResponse , Response } ;
1314use axum_extra:: json;
1415use 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) ]
5464pub 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)
0 commit comments