@@ -2,7 +2,9 @@ use crate::cli::http::cli::Content;
22use crate :: command:: Command ;
33use crate :: log:: MaybeFancy ;
44use anyhow:: Error ;
5+ use hyper:: http:: HeaderValue ;
56use reqwest:: blocking;
7+ use reqwest:: header:: HeaderMap ;
68
79pub struct HttpCommand {
810 /// HTTP client
@@ -18,15 +20,16 @@ pub struct HttpCommand {
1820pub enum HttpAction {
1921 Post {
2022 content : Content ,
21- content_type : String ,
22- accept_type : String ,
23+ content_type : Option < String > ,
24+ accept_type : Option < String > ,
2325 } ,
2426 Put {
2527 content : Content ,
26- content_type : String ,
28+ content_type : Option < String > ,
29+ accept_type : Option < String > ,
2730 } ,
2831 Get {
29- accept_type : String ,
32+ accept_type : Option < String > ,
3033 } ,
3134 Delete ,
3235}
@@ -53,25 +56,18 @@ impl HttpCommand {
5356 fn request ( & self ) -> Result < blocking:: RequestBuilder , Error > {
5457 let client = & self . client ;
5558 let url = & self . url ;
59+ let headers = self . action . headers ( ) ;
5660 let request = match & self . action {
57- HttpAction :: Post {
58- content,
59- content_type,
60- accept_type,
61- } => client
61+ HttpAction :: Post { content, .. } => client
6262 . post ( url)
63- . header ( "Accept" , accept_type)
64- . header ( "Content-Type" , content_type)
63+ . headers ( headers)
6564 . body ( blocking:: Body :: try_from ( content. clone ( ) ) ?) ,
66- HttpAction :: Put {
67- content,
68- content_type,
69- } => client
65+ HttpAction :: Put { content, .. } => client
7066 . put ( url)
71- . header ( "Content-Type" , content_type )
67+ . headers ( headers )
7268 . body ( blocking:: Body :: try_from ( content. clone ( ) ) ?) ,
73- HttpAction :: Get { accept_type } => client. get ( url) . header ( "Accept" , accept_type ) ,
74- HttpAction :: Delete => client. delete ( url) ,
69+ HttpAction :: Get { .. } => client. get ( url) . headers ( headers ) ,
70+ HttpAction :: Delete => client. delete ( url) . headers ( headers ) ,
7571 } ;
7672
7773 Ok ( request)
@@ -84,3 +80,66 @@ impl HttpCommand {
8480 Ok ( ( ) )
8581 }
8682}
83+
84+ impl HttpAction {
85+ pub fn headers ( & self ) -> HeaderMap {
86+ let mut headers = HeaderMap :: new ( ) ;
87+
88+ if let Some ( content_length) = self . content_length ( ) {
89+ headers. insert ( "Content-Length" , content_length) ;
90+ }
91+ if let Some ( content_type) = self . content_type ( ) {
92+ headers. insert ( "Content-Type" , content_type) ;
93+ }
94+ if let Some ( accept_type) = self . accept_type ( ) {
95+ headers. insert ( "Accept" , accept_type) ;
96+ }
97+
98+ headers
99+ }
100+
101+ pub fn content_type ( & self ) -> Option < HeaderValue > {
102+ match self {
103+ HttpAction :: Post {
104+ content,
105+ content_type,
106+ ..
107+ }
108+ | HttpAction :: Put {
109+ content,
110+ content_type,
111+ ..
112+ } => content_type
113+ . as_ref ( )
114+ . cloned ( )
115+ . or ( content. mime_type ( ) )
116+ . or ( Some ( "application/json" . to_string ( ) ) )
117+ . and_then ( |s| HeaderValue :: from_str ( & s) . ok ( ) ) ,
118+
119+ _ => None ,
120+ }
121+ }
122+
123+ pub fn accept_type ( & self ) -> Option < HeaderValue > {
124+ match self {
125+ HttpAction :: Post { accept_type, .. }
126+ | HttpAction :: Put { accept_type, .. }
127+ | HttpAction :: Get { accept_type } => accept_type
128+ . as_ref ( )
129+ . and_then ( |s| HeaderValue :: from_str ( s) . ok ( ) ) ,
130+
131+ _ => None ,
132+ }
133+ }
134+
135+ pub fn content_length ( & self ) -> Option < HeaderValue > {
136+ match self {
137+ HttpAction :: Post { content, .. } | HttpAction :: Put { content, .. } => content
138+ . length ( )
139+ . map ( |length| length. to_string ( ) )
140+ . and_then ( |s| HeaderValue :: from_str ( & s) . ok ( ) ) ,
141+
142+ _ => None ,
143+ }
144+ }
145+ }
0 commit comments