8
8
import requests
9
9
import markdown
10
10
from ce import get_ce_info , get_ce_state
11
+ from fetch import cloudapp_fetch
11
12
12
13
app = Flask (__name__ )
13
14
app .config ['ce_info' ] = None
@@ -43,53 +44,6 @@ def eph_ns() -> str:
43
44
this_eph_ns = request .cookies .get ('eph_ns' , None )
44
45
return this_eph_ns
45
46
46
- def cloudapp_fetch (session , url , timeout , prop , value , headers = {}):
47
- """
48
- Fetch data from URL
49
- Validate prop and value in the JSON response
50
- """
51
- response = session .get (url , timeout = timeout )
52
- response .raise_for_status ()
53
- data = response .json ()
54
- if data .get (prop ) != value :
55
- raise ValueError (f"Invalid { prop } : expected { value } , got { data .get (prop )} " )
56
- if data .get ("request_headers" ):
57
- clean_headers = headers_cleaner (data ['request_headers' ])
58
- data ['request_headers' ] = clean_headers
59
- return data
60
- return data
61
-
62
- def cloudapp_fetch2 (session , url , timeout , prop , key , value ):
63
- """
64
- Fetch data from URL
65
- Validate if a specific key-value pair is present in the dictionary located at `prop` in the JSON response
66
- """
67
- response = session .get (url , timeout = timeout )
68
- response .raise_for_status ()
69
- data = response .json ()
70
-
71
- prop_data = data .get (prop , {})
72
- if not isinstance (prop_data , dict ) or prop_data .get (key ) != value :
73
- raise ValueError (f"Expected { key } : { value } in { prop } , but got { key } : { prop_data .get (key )} " )
74
-
75
- if data .get ("request_headers" ):
76
- clean_headers = headers_cleaner (data ['request_headers' ])
77
- data ['request_headers' ] = clean_headers
78
-
79
- return data
80
-
81
- def headers_cleaner (headers ):
82
- """
83
- Remove headers that contain specific substrings.
84
- Use this to make responses look nicer.
85
- """
86
- unwanted_substrings = ['x-envoy' , 'cloudfront' , 'x-k8se' ]
87
- filtered_headers = {
88
- key : value for key , value in headers .items ()
89
- if not any (substring in key .lower () for substring in unwanted_substrings )
90
- }
91
- return filtered_headers
92
-
93
47
@app .errorhandler (404 )
94
48
@app .errorhandler (500 )
95
49
def return_err (err ):
@@ -248,7 +202,7 @@ def ex_test():
248
202
s = requests .Session ()
249
203
s .headers .update ({"User-Agent" : "MCN-Lab-Runner/1.0" })
250
204
url = f"https://foo.{ app .config ['base_url' ]} /"
251
- data = cloudapp_fetch2 (s , url , 5 , 'info' , 'bar' )
205
+ data = cloudapp_fetch (s , url , 7 , 'info' , { "foo" : True } )
252
206
return jsonify (status = 'success' , data = data )
253
207
except (LabException , requests .RequestException , ValueError ) as e :
254
208
return jsonify (status = 'fail' , error = str (e ))
@@ -260,7 +214,7 @@ def ex_test2():
260
214
s = requests .Session ()
261
215
s .headers .update ({"User-Agent" : "MCN-Lab-Runner/1.0" })
262
216
url = f"https://bar.{ app .config ['base_url' ]} /"
263
- data = cloudapp_fetch (s , url , 5 , 'info' , 'foo' )
217
+ data = cloudapp_fetch (s , url , 7 , 'info' , { "bar" : True } )
264
218
return jsonify (status = 'success' , data = data )
265
219
except (LabException , requests .RequestException , ValueError ) as e :
266
220
return jsonify (status = 'fail' , error = str (e ))
@@ -276,7 +230,7 @@ def lb_aws():
276
230
if not ns :
277
231
raise LabException ("Ephemeral NS not set" )
278
232
url = f"https://{ ns } .{ app .config ['base_url' ]} "
279
- data = cloudapp_fetch (s , url , 5 , 'env' , 'AWS' )
233
+ data = cloudapp_fetch (s , url , 7 , 'env' , 'AWS' , None )
280
234
return jsonify (status = 'success' , data = data )
281
235
except (LabException , requests .RequestException , ValueError ) as e :
282
236
return jsonify (status = 'fail' , error = str (e ))
0 commit comments