@@ -12,6 +12,7 @@ use std::marker::PhantomData;
12
12
13
13
use crate :: error:: Result ;
14
14
use async_trait:: async_trait;
15
+ use colored:: { ColoredString , Colorize } ;
15
16
use editoast_derive:: EditoastError ;
16
17
pub use http_client:: { HttpClient , HttpClientBuilder } ;
17
18
use log:: info;
@@ -22,6 +23,19 @@ use thiserror::Error;
22
23
23
24
const MAX_RETRIES : u8 = 5 ;
24
25
26
+ fn colored_method ( method : & reqwest:: Method ) -> ColoredString {
27
+ let m = method. as_str ( ) ;
28
+ match * method {
29
+ reqwest:: Method :: GET => m. green ( ) ,
30
+ reqwest:: Method :: POST => m. yellow ( ) ,
31
+ reqwest:: Method :: PUT => m. blue ( ) ,
32
+ reqwest:: Method :: PATCH => m. magenta ( ) ,
33
+ reqwest:: Method :: DELETE => m. red ( ) ,
34
+ _ => m. normal ( ) ,
35
+ }
36
+ . bold ( )
37
+ }
38
+
25
39
#[ derive( Debug , Clone ) ]
26
40
pub enum CoreClient {
27
41
Direct ( HttpClient ) ,
@@ -51,6 +65,9 @@ impl CoreClient {
51
65
path : & str ,
52
66
body : Option < & B > ,
53
67
) -> Result < R :: Response > {
68
+ let method_s = colored_method ( & method) ;
69
+ log:: info!( target: "editoast::coreclient" , "{method_s} {path}" ) ;
70
+ log:: debug!( target: "editoast::coreclient" , "Request content: {body}" , body = body. and_then( |b| serde_json:: to_string_pretty( b) . ok( ) ) . unwrap_or_default( ) ) ;
54
71
match self {
55
72
CoreClient :: Direct ( client) => {
56
73
let mut i_try = 0 ;
@@ -84,8 +101,11 @@ impl CoreClient {
84
101
msg : err. to_string ( ) ,
85
102
} ) ?;
86
103
if status. is_success ( ) {
104
+ log:: info!( target: "editoast::coreclient" , "{method_s} {path} {status}" , status = status. to_string( ) . bold( ) . green( ) ) ;
87
105
return R :: from_bytes ( bytes. as_ref ( ) ) ;
88
106
}
107
+
108
+ log:: error!( target: "editoast::coreclient" , "{method_s} {path} {status}" , status = status. to_string( ) . bold( ) . red( ) ) ;
89
109
// We try to deserialize the response as the standard Core error format
90
110
// If that fails we try to return a generic error containing the raw error
91
111
let core_error = <Json < CoreErrorPayload > as CoreResponse >:: from_bytes (
0 commit comments