How to use _build_response method in autotest

Best Python code snippet using autotest_python

ContactDatabase.py

Source:ContactDatabase.py Github

copy

Full Screen

...4contact_lists = {}5ongoing_ims = {}6def register_contact(contact_name, ip, port):7 if (contact_name in contacts):8 return _build_response(Constants.FAILURE_CODE_DUPLICATE_CONTACT)9 if (len(contacts) + 1 > Constants.MAX_NUM_OF_CONTACTS):10 return _build_response(Constants.FAILURE_CODE_MAX_NUM_CONTACTS)11 contacts[contact_name] = { 12 Constants.DB_NAME_KEY: contact_name,13 Constants.DB_IP_KEY: ip.strip('"').strip("'"),14 Constants.DB_PORT_KEY: int(port)15 }16 return _build_response(Constants.SUCCESS_CODE)17def create_contact_list(list_name):18 if (list_name in contact_lists):19 return _build_response(Constants.FAILURE_CODE_DUPLICATE_LIST)20 if (len(contact_lists) + 1 > Constants.MAX_NUM_OF_CONTACT_LISTS):21 return _build_response(Constants.FAILURE_CODE_MAX_NUM_LISTS)22 23 contact_lists[list_name] = []24 ongoing_ims[list_name] = []25 return _build_response(Constants.SUCCESS_CODE)26def get_lists():27 num_lists = len(contact_lists)28 list_names = list(contact_lists.keys())29 res_obj = {30 'num_lists': num_lists,31 'list_names': list_names,32 }33 return _build_response(Constants.SUCCESS_CODE, res_obj)34def add_contact_to_list(list_name, contact_name):35 if (not contact_name in contacts):36 return _build_response(Constants.FAILURE_CODE_UNREGISTERED_CONTACT)37 if (not list_name in contact_lists):38 return _build_response(Constants.FAILURE_CODE_UNCREATED_LIST)39 if (contact_name in contact_lists[list_name]):40 return _build_response(Constants.FAILURE_CODE_DUPLICATE_CONTACT_IN_LIST)41 if (_is_ongoing_im(list_name)):42 return _build_response(Constants.FAILURE_CODE_ONGOING_IM)43 44 contact_lists[list_name].append(contact_name)45 return _build_response(Constants.SUCCESS_CODE)46def remove_contact_from_list(list_name, contact_name):47 if (not contact_name in contacts):48 return _build_response(Constants.FAILURE_CODE_UNREGISTERED_CONTACT)49 if (not list_name in contact_lists):50 return _build_response(Constants.FAILURE_CODE_UNCREATED_LIST)51 if (not contact_name in contact_lists[list_name]):52 return _build_response(Constants.FAILURE_CODE_MISSING_CONTACT_IN_LIST)53 if (_is_ongoing_im(list_name)):54 return _build_response(Constants.FAILURE_CODE_ONGOING_IM)55 contact_lists[list_name].remove(contact_name)56 return _build_response(Constants.SUCCESS_CODE)57def exit(contact_name):58 if (not contact_name in contacts):59 return _build_response(Constants.FAILURE_CODE_UNREGISTERED_CONTACT)60 61 # check if contact is in any ongoing im's62 for list_name, list_contacts in contact_lists.items():63 if (contact_name in list_contacts and _is_ongoing_im(list_name)):64 return _build_response(Constants.FAILURE_CODE_ONGOING_IM)65 # delete contact from all associated contact lists66 for list_contacts in contact_lists.values():67 if (contact_name in list_contacts):68 list_contacts.remove(contact_name)69 70 # delete contact from list of currently active contacts71 del contacts[contact_name]72 return _build_response(Constants.SUCCESS_CODE)73def begin_instant_message(list_name, contact_name):74 if (not contact_name in contacts):75 return _build_response(Constants.FAILURE_CODE_UNREGISTERED_CONTACT)76 if (not list_name in contact_lists):77 return _build_response(Constants.FAILURE_CODE_UNCREATED_LIST)78 if (not contact_name in contact_lists[list_name]):79 return _build_response(Constants.FAILURE_CODE_MISSING_CONTACT_IN_LIST)80 81 full_contact_list = []82 for contact in contact_lists[list_name]:83 if (contact == contact_name):84 full_contact_list.insert(0, contacts[contact])85 else:86 full_contact_list.append(contacts[contact])87 res_obj = {88 'num_contacts_in_list': len(contact_lists[list_name]),89 'contact_list': full_contact_list,90 }91 ongoing_ims[list_name].append(contact_name)92 return _build_response(Constants.SUCCESS_CODE, res_obj)93def end_instant_message(list_name, contact_name):94 if (not contact_name in contacts):95 return _build_response(Constants.FAILURE_CODE_UNREGISTERED_CONTACT)96 if (not list_name in contact_lists):97 return _build_response(Constants.FAILURE_CODE_UNCREATED_LIST)98 if (not contact_name in contact_lists[list_name]):99 return _build_response(Constants.FAILURE_CODE_MISSING_CONTACT_IN_LIST)100 if (not contact_name in ongoing_ims[list_name]):101 return _build_response(Constants.FAILURE_CODE_INVALID_IM_COMPLETE)102 ongoing_ims[list_name].remove(contact_name)103 return _build_response(Constants.SUCCESS_CODE)104def save_to_file(file_name):105 try:106 file = FileWriter(file_name)107 file.clear_contents() # overwrite same file with each save108 109 # write line containing number of contacts110 file.append_to_end(str(len(contacts)), False)111 # for each contact, append a line containing the contact's info112 for contact_name, contact_info in contacts.items():113 _save_contact_info(114 file,115 contact_name, 116 contact_info[Constants.DB_IP_KEY], 117 contact_info[Constants.DB_PORT_KEY])118 119 # append line containing number of contact lists120 file.append_line(str(len(contact_lists)))121 # for each list, append its name, number of contacts, and their respective contact info122 for list_name, list_contacts in contact_lists.items():123 # append line containing contact list name124 file.append_line(list_name)125 # append line containing number of contacts in current list126 file.append_line(str(len(list_contacts)))127 # for each contact in the current list, append a line containing the contact's info128 # skips first index of contact list129 for contact in list_contacts:130 _save_contact_info(131 file,132 contact, 133 contacts[contact][Constants.DB_IP_KEY], 134 contacts[contact][Constants.DB_PORT_KEY])135 file.close()136 except Exception as err:137 print('[CONTACT DB ERROR]: Occurred while saving to file\n' + str(err))138 return _build_response(Constants.FAILURE_CODE_FILE_SAVE_ERROR)139 return _build_response(Constants.SUCCESS_CODE)140# appends contact name, IP address, and port number on one line to the end of a file141def _save_contact_info(file, name, ip, port):142 file.append_line(name)143 file.append_to_end(ip)144 file.append_to_end(str(port))145def _build_response(return_code, data = None):146 return (return_code, data)147def _is_ongoing_im(list_name):...

Full Screen

Full Screen

error_handlers.py

Source:error_handlers.py Github

copy

Full Screen

...3 4import urllib5import sys6import json7def _build_response(request, context):8 if 'uri' not in context: context['uri'] = request.build_absolute_uri()9 if 'subheader' not in context: context['subheader'] = request.path10 if 'comment' not in context:11 context['comment'] = ("\n\n------ Issue details -----\n{status} error on accessing:\n{uri}"12 .format(**context))13 if 'issue_query' not in context:14 context['issue_query'] = urllib.urlencode(dict(comment=context['comment']))15 16 #TODO: should check http accept17 if request.path.startswith("/api"):18 content = json.dumps({"error": True, 19 "status" : context['status'],20 "message" : context['header'],21 "details" : context['subheader'],22 "description" : context['description']}, indent=2)23 content_type = "application/json"24 else:25 content = render(request, "error.html", context)26 content_type = "text/html"27 28 return HttpResponse(content, status=context['status'], content_type=content_type)29def handler500(request):30 """31 500 error handler which includes a normal request context and some extra info.32 """33 status = 50034 title = header = "500 : An Error has occurred"35 exc_type, exc_value, exc_tb = sys.exc_info()36 subheader = "{exc_type.__name__}: {exc_value}".format(**locals())37 description = """The server encountered an internal error or misconfiguration and38 was unable to complete your request."""39 return _build_response(request, locals())40 41def handler404(request):42 """43 404 error handler which includes a normal request context.44 """45 status = 40446 title = header = "404 : Page not Found"47 description = """The requested location could not be found"""48 return _build_response(request, locals())49 50def handler503(request):51 """52 503 error handler which includes a normal request context.53 """54 status = 50355 title = header = "AmCAT is down for maintenance"56 subheader = "We are working on the problem and hopefully AmCAT will be available again soon!"57 description = """The server is temporarily down for maintenance. Please check back later or contact the administrator"""58 return _build_response(request, locals())59def handler403(request):60 """61 403 Forbidden handler which includes a normal request context.62 """63 status = 40364 title = header = "403 : Forbidden"65 exc_type, exc_value, exc_tb = sys.exc_info()66 if exc_value:67 subheader = "{request.path}: {exc_value}".format(**locals())68 description = """You have insufficient rights to accesss the requested resource.69 To gain rights, please contact an administrator of the project you70 are trying to use (for project-related resources). For global71 permissions, please contact the server administrator"""...

Full Screen

Full Screen

build_setup.py

Source:build_setup.py Github

copy

Full Screen

...14"""Build setup (untrusted side)."""15from build_management import build_manager16from protos import untrusted_runner_pb217from system import environment18def _build_response(result):19 if not result:20 return untrusted_runner_pb2.SetupBuildResponse(result=False)21 return untrusted_runner_pb2.SetupBuildResponse(22 result=True,23 app_path=environment.get_value('APP_PATH'),24 app_path_debug=environment.get_value('APP_PATH_DEBUG'),25 app_dir=environment.get_value('APP_DIR'),26 build_dir=environment.get_value('BUILD_DIR'),27 build_url=environment.get_value('BUILD_URL'),28 fuzz_target=environment.get_value('FUZZ_TARGET'))29def setup_regular_build(request):30 """Setup a regular build."""31 build = build_manager.RegularBuild(request.base_build_dir, request.revision,32 request.build_url, request.build_dir_name,33 request.target_weights)34 return _build_response(build.setup())35def setup_symbolized_build(request):36 """Setup a symbolized build."""37 build = build_manager.SymbolizedBuild(38 request.base_build_dir, request.revision, request.release_build_url,39 request.debug_build_url)40 return _build_response(build.setup())41def setup_production_build(request):42 """Setup a production build."""43 build = build_manager.ProductionBuild(request.base_build_dir, request.version,44 request.build_url, request.build_type)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful