1
1
from aiohttp import (
2
- web ,
3
- ClientSession ,
4
- ClientRequest ,
5
2
ClientResponse ,
6
3
ClientError ,
7
- ClientTimeout ,
8
4
)
9
5
10
- import asyncio
11
6
import logging
12
7
13
8
import json
14
9
15
- from ..helpers .utils import log_msg
16
10
17
- EVENT_LOGGER = logging .getLogger ("__name__" )
11
+ LOGGER = logging .getLogger ("__name__" )
18
12
19
13
20
14
class repr_json :
@@ -34,24 +28,11 @@ def __init__(self, admin_url: str, client_session):
34
28
self .color = None
35
29
self .prefix = None
36
30
37
- def log (self , * msg , ** kwargs ):
38
- self .handle_output (* msg , ** kwargs )
39
-
40
31
@property
41
32
def prefix_str (self ):
42
33
if self .prefix :
43
34
return f"{ self .prefix :10s} |"
44
35
45
- def handle_output (self , * output , source : str = None , ** kwargs ):
46
- end = "" if source else "\n "
47
- if source == "stderr" :
48
- color = "fg:ansired"
49
- elif not source :
50
- color = self .color or "fg:ansiblue"
51
- else :
52
- color = None
53
- log_msg (* output , color = color , prefix = self .prefix_str , end = end , ** kwargs )
54
-
55
36
async def admin_request (
56
37
self , method , path , json_data = None , text = False , params = None , data = None
57
38
) -> ClientResponse :
@@ -72,91 +53,91 @@ async def admin_request(
72
53
73
54
async def admin_GET (self , path , text = False , params = None ) -> ClientResponse :
74
55
try :
75
- EVENT_LOGGER .debug ("Controller GET %s request to Agent" , path )
56
+ LOGGER .debug ("Controller GET %s request to Agent" , path )
76
57
response = await self .admin_request ("GET" , path , None , text , params )
77
- EVENT_LOGGER .debug (
58
+ LOGGER .debug (
78
59
"Response from GET %s received: \n %s" ,
79
60
path ,
80
61
repr_json (response ),
81
62
)
82
63
return response
83
64
except ClientError as e :
84
- self . log (f"Error during GET { path } : { str (e )} " )
65
+ LOGGER . error (f"Error during GET { path } : { str (e )} " )
85
66
raise
86
67
87
68
async def admin_POST (
88
69
self , path , json_data = None , text = False , params = None , data = None
89
70
) -> ClientResponse :
90
71
try :
91
- EVENT_LOGGER .debug (
72
+ LOGGER .debug (
92
73
"Controller POST %s request to Agent%s" ,
93
74
path ,
94
75
(" with data: \n {}" .format (repr_json (json_data )) if json_data else "" ),
95
76
)
96
77
response = await self .admin_request (
97
78
"POST" , path , json_data , text , params , data
98
79
)
99
- EVENT_LOGGER .debug (
80
+ LOGGER .debug (
100
81
"Response from POST %s received: \n %s" ,
101
82
path ,
102
83
repr_json (response ),
103
84
)
104
85
return response
105
86
except ClientError as e :
106
- self . log (f"Error during POST { path } : { str (e )} " )
87
+ LOGGER . error (f"Error during POST { path } : { str (e )} " )
107
88
raise
108
89
109
90
async def admin_PATCH (
110
91
self , path , json_data = None , text = False , params = None , data = None
111
92
) -> ClientResponse :
112
93
try :
113
- EVENT_LOGGER .debug (
94
+ LOGGER .debug (
114
95
"Controller PATCH %s request to Agent%s" ,
115
96
path ,
116
97
(" with data: \n {}" .format (repr_json (json_data )) if json_data else "" ),
117
98
)
118
99
response = await self .admin_request (
119
100
"PATCH" , path , json_data , text , params , data
120
101
)
121
- EVENT_LOGGER .debug (
102
+ LOGGER .debug (
122
103
"Response from PATCH %s received: \n %s" , path , repr_json (response )
123
104
)
124
105
return response
125
106
except ClientError as e :
126
- self . log (f"Error during PATCH { path } : { str (e )} " )
107
+ LOGGER . error (f"Error during PATCH { path } : { str (e )} " )
127
108
raise
128
109
129
110
async def admin_PUT (
130
111
self , path , json_data = None , text = False , params = None , data = None
131
112
) -> ClientResponse :
132
113
try :
133
- EVENT_LOGGER .debug (
114
+ LOGGER .debug (
134
115
"Controller PUT %s request to Agent%s" ,
135
116
path ,
136
117
(" with data: \n {}" .format (repr_json (json_data )) if json_data else "" ),
137
118
)
138
119
response = await self .admin_request (
139
120
"PUT" , path , json_data , text , params , data
140
121
)
141
- EVENT_LOGGER .debug (
122
+ LOGGER .debug (
142
123
"Response from PUT %s received: \n %s" , path , repr_json (response )
143
124
)
144
125
return response
145
126
except ClientError as e :
146
- self . log (f"Error during PUT { path } : { str (e )} " )
127
+ LOGGER . error (f"Error during PUT { path } : { str (e )} " )
147
128
raise
148
129
149
130
async def admin_DELETE (self , path , text = False , params = None ) -> ClientResponse :
150
131
try :
151
- EVENT_LOGGER .debug ("Controller DELETE %s request to Agent" , path )
132
+ LOGGER .debug ("Controller DELETE %s request to Agent" , path )
152
133
153
134
response = await self .admin_request ("DELETE" , path , None , text , params )
154
- EVENT_LOGGER .debug (
135
+ LOGGER .debug (
155
136
"Response from DELETE %s received: \n %s" ,
156
137
path ,
157
138
repr_json (response ),
158
139
)
159
140
return response
160
141
except ClientError as e :
161
- self . log (f"Error during DELETE { path } : { str (e )} " )
142
+ LOGGER . error (f"Error during DELETE { path } : { str (e )} " )
162
143
raise
0 commit comments