@@ -12,13 +12,27 @@ 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 reqwest:: Url ;
18
19
use serde:: { de:: DeserializeOwned , Serialize } ;
19
20
use serde_derive:: Deserialize ;
20
21
use thiserror:: Error ;
21
22
23
+ fn colored_method ( method : & reqwest:: Method ) -> ColoredString {
24
+ let m = method. as_str ( ) ;
25
+ match * method {
26
+ reqwest:: Method :: GET => m. green ( ) ,
27
+ reqwest:: Method :: POST => m. yellow ( ) ,
28
+ reqwest:: Method :: PUT => m. blue ( ) ,
29
+ reqwest:: Method :: PATCH => m. magenta ( ) ,
30
+ reqwest:: Method :: DELETE => m. red ( ) ,
31
+ _ => m. normal ( ) ,
32
+ }
33
+ . bold ( )
34
+ }
35
+
22
36
#[ derive( Debug , Clone ) ]
23
37
pub enum CoreClient {
24
38
Direct ( HttpClient ) ,
@@ -48,6 +62,9 @@ impl CoreClient {
48
62
path : & str ,
49
63
body : Option < & B > ,
50
64
) -> Result < R :: Response > {
65
+ let method_s = colored_method ( & method) ;
66
+ log:: info!( target: "editoast::coreclient" , "{method_s} {path}" ) ;
67
+ log:: debug!( target: "editoast::coreclient" , "Request content: {body}" , body = body. and_then( |b| serde_json:: to_string_pretty( b) . ok( ) ) . unwrap_or_default( ) ) ;
51
68
match self {
52
69
CoreClient :: Direct ( client) => {
53
70
let mut request = client. request ( method, path) ;
@@ -65,8 +82,10 @@ impl CoreClient {
65
82
msg : err. to_string ( ) ,
66
83
} ) ?;
67
84
if status. is_success ( ) {
85
+ log:: info!( target: "editoast::coreclient" , "{method_s} {path} {status}" , status = status. to_string( ) . bold( ) . green( ) ) ;
68
86
R :: from_bytes ( bytes. as_ref ( ) )
69
87
} else {
88
+ log:: error!( target: "editoast::coreclient" , "{method_s} {path} {status}" , status = status. to_string( ) . bold( ) . red( ) ) ;
70
89
// We try to deserialize the response as the standard Core error format
71
90
// If that fails we try to return a generic error containing the raw error
72
91
let core_error =
0 commit comments