@@ -54,6 +54,8 @@ SYN_APP = "app" # short for application
5454POSITION_PARAMS = "commands"
5555OPTIONS_PARAMS = "options"
5656
57+ NOT_APPLICABLE = "N/A"
58+
5759
5860def show_version ():
5961 """show synctl version"""
@@ -104,13 +106,15 @@ synctl create test -t 0 --label simple-ping-test --url <url> --location <id> --f
104106synctl create test -t 1 --label script-test --from-file script-name.js --location <id>
105107
106108# create an API script bundle type
109+ synctl create test -t 1 --label script-bundle-test --bundle file.zip --script-file index.js --location <id>
107110synctl create test -t 1 --label script-bundle-test --bundle <base64> --script-file index.js --location <id>
108111
109112# create browserscript
110113synctl create test -t 2 --label browserscript-test --from-file api-sample.js --browser firefox --location <id>
111114
112115# create browserscript bundle
113- synctl create test -t 2 --label "browserscript-bundle-test" --bundle "base64" --script-file mytest.js --browser chrome --location <id>
116+ synctl create test -t 2 --label "browserscript-bundle-test" --bundle "file.zip" --script-file mytest.js --browser chrome --location <id>
117+ synctl create test -t 2 --label "browserscript-bundle-test" --bundle "<base64>" --script-file mytest.js --browser chrome --location <id>
114118
115119# create webpagescript
116120synctl create test -t 3 --label "webpagescript-test" --from-file side/browser.side --browser chrome --location <id>"""
@@ -181,7 +185,7 @@ class Base:
181185
182186 def set_host_token (self , new_host = None , new_token = None ):
183187 if new_host is not None and new_token is not None :
184- self .auth ["host" ] = new_host
188+ self .auth ["host" ] = new_host . rstrip ( '/' )
185189 self .auth ["token" ] = new_token
186190 else :
187191 print ("both --host and --token are required" )
@@ -306,6 +310,10 @@ class ConfigurationFile(Base):
306310
307311 return False
308312
313+ def __remove_right_slash (self , host_name ):
314+ """remove right slash of a host"""
315+ return host_name .rstrip ('/' )
316+
309317 def add_an_item_to_config (self , name , host , token , set_default = False ):
310318 """add a new config"""
311319 if name is None or host is None or token is None :
@@ -314,11 +322,12 @@ class ConfigurationFile(Base):
314322 print ("name, host, and token must not be none" )
315323 elif self .__check_if_already_in_config (name ):
316324 # update it
317- self .update_an_item (name , host , token , set_default = set_default )
325+ self .update_an_item (name , self .__remove_right_slash (
326+ host ), token , set_default = set_default )
318327 else :
319328 self .config_json .append ({
320329 "name" : name ,
321- "host" : host ,
330+ "host" : self . __remove_right_slash ( host ) ,
322331 "token" : token ,
323332 "default" : False
324333 })
@@ -376,12 +385,12 @@ class ConfigurationFile(Base):
376385 for item in self .config_json :
377386 if item ["default" ] is True :
378387 return {
379- "host" : item ["host" ],
388+ "host" : self . __remove_right_slash ( item ["host" ]) ,
380389 "token" : item ["token" ]
381390 }
382391 if len (self .config_json ) > 0 :
383392 return {
384- "host" : self .config_json [0 ]["host" ],
393+ "host" : self .__remove_right_slash ( self . config_json [0 ]["host" ]) ,
385394 "token" : self .config_json [0 ]["token" ]
386395 }
387396 else :
@@ -393,7 +402,7 @@ class ConfigurationFile(Base):
393402 for item in self .config_json :
394403 if item ["name" ] == name :
395404 return {
396- "host" : item ["host" ],
405+ "host" : self . __remove_right_slash ( item ["host" ]) ,
397406 "token" : item ["token" ]
398407 }
399408 raise ValueError (f"no config named { name } " )
@@ -802,12 +811,19 @@ class SyntheticConfiguration(Base):
802811 print (not_found_e )
803812 sys .exit (- 1 )
804813
805- def read_zip_file (self , file_name ):
814+ def is_zip_file (self , file_name ):
815+ """check zip file name"""
816+ if file_name is not None and isinstance (file_name , str ):
817+ return file_name .endswith ('.zip' )
818+
819+ def read_zip_file_to_base64 (self , file_name ):
820+ """read zip file and encode with base64"""
806821 with open (file_name , 'rb' ) as file1 :
807822 zip_content_byte = file1 .read ()
808823 zip_content_byte_base64 = b64encode (zip_content_byte )
809- self .set_api_bundle_script (zip_content_byte_base64 )
810- return zip_content_byte_base64
824+ zip_content_base64_str = zip_content_byte_base64 .decode ('utf-8' )
825+ # self.set_api_bundle_script(zip_content_base64_str)
826+ return zip_content_base64_str
811827
812828 def get_json (self ):
813829 """return payload as json"""
@@ -1361,7 +1377,7 @@ class SyntheticTest(Base):
13611377 active_length = 6
13621378 success_rate_length = 12
13631379 response_time_length = 12
1364- location_str = "no locations"
1380+ location_str = NOT_APPLICABLE
13651381
13661382 test_type = "" if test_type is None else test_type
13671383 # show title
@@ -1393,7 +1409,7 @@ class SyntheticTest(Base):
13931409 # locations,
13941410 location_str = ',' .join (t ['locationDisplayLabels' ])
13951411 else :
1396- location_str = "no locations"
1412+ location_str = NOT_APPLICABLE
13971413 if test_type == "" or (test_type != "" and current_type == test_type ):
13981414 print (self .fill_space (t ["id" ], id_length ),
13991415 self .fill_space (t ['label' ], max_label_length ),
@@ -1413,7 +1429,7 @@ class SyntheticTest(Base):
14131429 if len (t ['locations' ]) > 0 :
14141430 location_str = ',' .join (t ['locationDisplayLabels' ])
14151431 else :
1416- location_str = "no locations"
1432+ location_str = NOT_APPLICABLE
14171433 if test_type == "" or (test_type != "" and current_type == test_type ):
14181434 print (self .fill_space (t ["id" ], id_length ),
14191435 self .fill_space (t ['label' ], max_label_length ),
@@ -1810,7 +1826,6 @@ class SyntheticResult(Base):
18101826 if summary_res .status_code == 200 :
18111827 # extracting data in json format
18121828 data = summary_res .json ()
1813- # print("summary data:", data)
18141829 return data
18151830 elif summary_res .status_code == 400 :
18161831 print (f'Bad Request: status code: { summary_res .status_code } ' )
@@ -2147,7 +2162,7 @@ class ParseParameter:
21472162
21482163 # options for bundle script
21492164 self .parser_create .add_argument (
2150- '--bundle' , type = str , help = 'synthetic script encoded with base64' )
2165+ '--bundle' , type = str , help = 'specify a zip file or use synthetic script encoded with base64' )
21512166 self .parser_create .add_argument (
21522167 '--script-file' , type = str , help = 'bundle script entry file, e.g, myscript.js' )
21532168 # [0, 2]
@@ -2188,7 +2203,7 @@ class ParseParameter:
21882203
21892204 # set auth
21902205 self .parser_create .add_argument (
2191- '--use-env' , type = str , default = None , help = 'use a specified configuration' )
2206+ '--use-env' , '-e' , type = str , default = None , help = 'use a specified configuration' )
21922207 self .parser_create .add_argument (
21932208 '--host' , type = str , help = 'set hostname' )
21942209 self .parser_create .add_argument (
@@ -2201,7 +2216,7 @@ class ParseParameter:
22012216 # parser_get.add_argument('type_id', type=str,
22022217 # required=False, help='test id or location id')
22032218 self .parser_get .add_argument (
2204- '--type' , '-t' , type = int , metavar = 'synthetic type' , help = 'specify synthetic type' )
2219+ '--type' , '-t' , type = int , choices = [ 0 , 1 , 2 , 3 ], metavar = 'synthetic type' , help = 'specify synthetic type' )
22052220 self .parser_get .add_argument (
22062221 'id' , type = str , nargs = "?" , help = 'synthetic id' )
22072222 self .parser_get .add_argument (
@@ -2235,7 +2250,7 @@ class ParseParameter:
22352250 # self.parser_get.add_argument(
22362251 # '--token', type=str, help='set token')
22372252 host_token_group .add_argument (
2238- '--use-env' , type = str , default = None , help = 'use a specified config' )
2253+ '--use-env' , '-e' , type = str , default = None , help = 'use a specified config' )
22392254 host_token_group .add_argument (
22402255 '--host' , type = str , help = 'set hostname' )
22412256 host_token_group .add_argument (
@@ -2274,7 +2289,7 @@ class ParseParameter:
22742289
22752290 # parser_patch.add_mutually_exclusive_group
22762291 self .parser_patch .add_argument (
2277- '--use-env' , type = str , default = None , help = 'use a config hostname' )
2292+ '--use-env' , '-e' , type = str , default = None , help = 'use a config hostname' )
22782293 self .parser_patch .add_argument (
22792294 '--host' , type = str , help = 'set hostname' )
22802295 self .parser_patch .add_argument (
@@ -2292,7 +2307,7 @@ class ParseParameter:
22922307 '--from-data' , type = str , help = 'new json payload' )
22932308
22942309 self .parser_update .add_argument (
2295- '--use-env' , type = str , default = None , help = 'use a config hostname' )
2310+ '--use-env' , '-e' , type = str , default = None , help = 'use a config hostname' )
22962311 self .parser_update .add_argument (
22972312 '--host' , type = str , help = 'set hostname' )
22982313 self .parser_update .add_argument (
@@ -2315,7 +2330,7 @@ class ParseParameter:
23152330 '--no-locations' , action = "store_true" , help = "delete test with no locations" )
23162331
23172332 self .parser_delete .add_argument (
2318- '--use-env' , type = str , default = None , help = 'use a config hostname' )
2333+ '--use-env' , '-e' , type = str , default = None , help = 'use a config hostname' )
23192334 self .parser_delete .add_argument (
23202335 '--host' , type = str , help = 'set hostname' )
23212336 self .parser_delete .add_argument (
@@ -2427,7 +2442,10 @@ def main():
24272442 syn_type_t = None
24282443 syn_window_size = get_args .window_size
24292444 if get_args .type is not None :
2430- syn_type_t = synthetic_type [get_args .type ]
2445+ try :
2446+ syn_type_t = synthetic_type [get_args .type ]
2447+ except IndexError :
2448+ print ("synthetic type only support 0 1 2 3" , syn_type_t )
24312449
24322450 if get_args .id is None :
24332451 out_list = syn_instance .retrieve_all_synthetic_tests (
@@ -2552,13 +2570,18 @@ def main():
25522570 syn_type_t = synthetic_type [get_args .type ]
25532571 payload = SyntheticConfiguration (syn_type_t , bundle_type = True )
25542572
2573+ if payload .is_zip_file (get_args .bundle ):
2574+ bundle_base64_str = payload .read_zip_file_to_base64 (
2575+ get_args .bundle )
2576+ else :
2577+ bundle_base64_str = get_args .bundle
25552578 # entry file
25562579 if get_args .script_file is not None :
25572580 payload .set_api_bundle_script (
2558- get_args . bundle , script_file = get_args .script_file )
2581+ bundle_base64_str , script_file = get_args .script_file )
25592582 else :
25602583 # script file use index.js
2561- payload .set_api_bundle_script (get_args . bundle )
2584+ payload .set_api_bundle_script (bundle_base64_str )
25622585 # BrowserScript 2, WebpageScript 3
25632586 if get_args .type in (2 , 3 ):
25642587 payload .set_browser_type (get_args .browser )
0 commit comments