1+ pub mod labels {
2+ use reqwest:: Response ;
3+ use serde_json:: { json, Value } ;
4+ use serde:: { Deserialize , Serialize } ;
5+
6+ #[ derive( Default , Debug , Clone , PartialEq , Serialize , Deserialize ) ]
7+ #[ allow( non_snake_case) ]
8+ pub struct Label {
9+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
10+ pub name : Option < String > ,
11+ }
12+
13+ #[ derive( Default , Debug , Clone , PartialEq , Serialize , Deserialize ) ]
14+ #[ allow( non_snake_case) ]
15+ pub struct AddLabel {
16+ pub prefix : String ,
17+ pub name : String ,
18+ }
19+
20+ pub struct LabelService {
21+ pub labels : Vec < Label > ,
22+ }
23+
24+ impl LabelService {
25+ pub async fn add_label ( url : & str , token : String , id : String , labels : Vec < String > ) -> String {
26+ let mut labels_vec: Vec < AddLabel > = vec ! [ ] ;
27+ for label in labels {
28+ labels_vec. push ( AddLabel {
29+ prefix : String :: from ( "global" ) ,
30+ name : label,
31+ } ) ;
32+ }
33+ let request_url = format ! ( "{url}/rest/api/content/{id}/label/" ) ;
34+ let client = reqwest:: Client :: new ( ) ;
35+ let resp: Response = client. post ( & request_url)
36+ . body ( labels_vec)
37+ . header ( "Authorization" , format ! ( "Basic {token}" ) )
38+ . header ( "Accept" , "application/json" )
39+ . header ( "Content-Type" , "application/json" )
40+ . send ( ) . await . unwrap ( ) ;
41+ let body = resp. text ( ) . await . unwrap ( ) ;
42+ return serde_json:: from_str ( body. as_str ( ) ) . unwrap ( ) ;
43+ }
44+ }
45+ }
0 commit comments