1
1
use crate :: models:: ApiToken ;
2
2
use crate :: schema:: api_tokens;
3
- use crate :: util:: { rfc3339, BytesRequest } ;
3
+ use crate :: util:: rfc3339;
4
4
use crate :: views:: EncodableApiTokenWithToken ;
5
5
6
6
use crate :: app:: AppState ;
@@ -18,7 +18,6 @@ use diesel::prelude::*;
18
18
use diesel_async:: async_connection_wrapper:: AsyncConnectionWrapper ;
19
19
use http:: request:: Parts ;
20
20
use http:: StatusCode ;
21
- use serde_json as json;
22
21
use serde_json:: Value ;
23
22
24
23
#[ derive( Deserialize ) ]
@@ -65,33 +64,32 @@ pub async fn list(
65
64
. await
66
65
}
67
66
68
- /// Handles the `PUT /me/tokens` route.
69
- pub async fn new ( app : AppState , req : BytesRequest ) -> AppResult < Json < Value > > {
70
- let ( parts, body) = req. 0 . into_parts ( ) ;
67
+ /// The incoming serialization format for the `ApiToken` model.
68
+ #[ derive( Deserialize ) ]
69
+ pub struct NewApiToken {
70
+ name : String ,
71
+ crate_scopes : Option < Vec < String > > ,
72
+ endpoint_scopes : Option < Vec < String > > ,
73
+ #[ serde( default , with = "rfc3339::option" ) ]
74
+ expired_at : Option < NaiveDateTime > ,
75
+ }
71
76
77
+ /// The incoming serialization format for the `ApiToken` model.
78
+ #[ derive( Deserialize ) ]
79
+ pub struct NewApiTokenRequest {
80
+ api_token : NewApiToken ,
81
+ }
82
+
83
+ /// Handles the `PUT /me/tokens` route.
84
+ pub async fn new (
85
+ app : AppState ,
86
+ parts : Parts ,
87
+ Json ( new) : Json < NewApiTokenRequest > ,
88
+ ) -> AppResult < Json < Value > > {
72
89
let conn = app. db_write ( ) . await ?;
73
90
spawn_blocking ( move || {
74
91
let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
75
92
76
- /// The incoming serialization format for the `ApiToken` model.
77
- #[ derive( Deserialize ) ]
78
- struct NewApiToken {
79
- name : String ,
80
- crate_scopes : Option < Vec < String > > ,
81
- endpoint_scopes : Option < Vec < String > > ,
82
- #[ serde( default , with = "rfc3339::option" ) ]
83
- expired_at : Option < NaiveDateTime > ,
84
- }
85
-
86
- /// The incoming serialization format for the `ApiToken` model.
87
- #[ derive( Deserialize ) ]
88
- struct NewApiTokenRequest {
89
- api_token : NewApiToken ,
90
- }
91
-
92
- let new: NewApiTokenRequest = json:: from_slice ( & body)
93
- . map_err ( |e| bad_request ( format ! ( "invalid new token request: {e:?}" ) ) ) ?;
94
-
95
93
let name = & new. api_token . name ;
96
94
if name. is_empty ( ) {
97
95
return Err ( bad_request ( "name must have a value" ) ) ;
0 commit comments