How to use format_request_info method in yandex-tank

Best Python code snippet using yandex-tank

api_view.py

Source:api_view.py Github

copy

Full Screen

...11from django import forms12from jrdbnntt_com.util import acl13from jrdbnntt_com.util.exceptions import InternalServerError, ExternalUserError14import logging15def format_request_info(request: HttpRequest, input_data: dict, req, res):16 return '\tRequest: {}\n\tRaw Input: {}\n\tReq: {}\n\tRes: {}'.format(17 str(request), str(input_data), str(req), str(res)18 )19class ApiView(View):20 http_method_names = ['post'] # Override to allow GET21 request_form_class = forms.Form # Override each time22 response_form_class = forms.Form # Override each time23 access_manager = acl.AccessManager()24 allowed_after_current_hackathon_ends = True25 validate_response = False26 def __init__(self, **kwargs):27 super().__init__(**kwargs)28 self.kwargs = list()29 self.args = list()30 def get(self, request: HttpRequest, *args, **kwargs):31 self.args = args32 self.kwargs = kwargs33 return self.process(request, request.GET)34 def post(self, request: HttpRequest, *args, **kwargs):35 self.args = args36 self.kwargs = kwargs37 return self.process(request, request.POST)38 def process(self, request: HttpRequest, input_data: dict):39 """ Validates input and attempts to preform work() logic. Returns the correct JsonResponse """40 # Authenticate Access41 if not self.authenticate(request):42 return JsonResponse({43 'cause': _('Unauthorized')44 }, status=401)45 req = None46 res = None47 # Preform request48 try:49 # Validate & clean request50 request_form = self.request_form_class(input_data, request.FILES)51 if not request_form.is_valid():52 raise ExternalUserError(ValidationError(request_form.errors.as_data()))53 req = request_form.cleaned_data54 res = {}55 # Preform desired api task and populate response (res) object56 try:57 self.work(request, req, res)58 except ValidationError as e:59 raise ExternalUserError(e)60 # Validate response61 response_form = self.response_form_class(res)62 if not response_form.is_valid() and self.validate_response:63 raise InternalServerError(ValidationError(response_form.errors.as_data()))64 res = response_form.cleaned_data65 # Successful api call!66 return JsonResponse(res)67 except ExternalUserError as error:68 if settings.DEBUG:69 logging.error(format_request_info(request, input_data, res, req))70 error.log()71 return error.json_response()72 except InternalServerError as error:73 request_info = format_request_info(request, input_data, res, req)74 logging.error(request_info)75 error.log()76 if not settings.DEBUG:77 error.email_log_to_dev(request_info=request_info, user=request.user)78 return error.json_response(include_message=False)79 except Exception as e:80 error = InternalServerError(e)81 request_info = format_request_info(request, input_data, res, req)82 logging.error(request_info)83 error.log()84 if not settings.DEBUG:85 error.email_log_to_dev(request_info=request_info, user=request.user)86 return error.json_response(include_message=False)87 def work(self, request: HttpRequest, req: dict, res: dict):88 """89 Preforms api request logic90 :param request Django request object (only use this if necessary)91 :param req cleaned and valid request data92 :param res response data to be checked after this call93 """94 pass95 def authenticate(self, request):...

Full Screen

Full Screen

middleware.py

Source:middleware.py Github

copy

Full Screen

...13 return request.path_info.startswith(settings.MEDIA_URL)14 shared_dict = {'ignores': [lambda request: is_media(request)]}15 shared_dict.update(**kwargs)16 return shared_dict17def format_request_info(request, response):18 template = "(%(method)s%(ajax_info)s %(status_code)s %(path_info)s)"19 values = {20 'method': request.method,21 'ajax_info': '' if not request.is_ajax() else '(ajax)',22 'status_code': response.status_code,23 'path_info': request.path_info,24 }25 return template % values26# TODO: We could skip this whole thing e.g. with a meta class solution. Later.27request_queries_shared_dict = create_shared_dict(query_count={})28class RequestQueryCounterMiddleware(object):29 def __init__(self):30 self.__dict__ = request_queries_shared_dict31 def process_request(self, request):32 if not any(ignore_test(request) for ignore_test in self.ignores):33 from django.db import connection34 self.query_count[id(request)] = len(connection.queries)35 return None36 def process_response(self, request, response):37 if not any(ignore_test(request) for ignore_test in self.ignores):38 from django.db import connection39 query_count = len(connection.queries) - self.query_count[id(request)]40 logger.debug('Queries for request #%s %s: %d' % (id(request), format_request_info(request, response), query_count))41 return response42durations_shared_dict = create_shared_dict(durations={})43class RequestDurationMiddleware(object):44 def __init__(self):45 self.__dict__ = durations_shared_dict46 def process_request(self, request):47 if not any(ignore_test(request) for ignore_test in self.ignores):48 from django.db import connection49 self.durations[id(request)] = time.time()50 return None51 def process_response(self, request, response):52 if not any(ignore_test(request) for ignore_test in self.ignores):53 duration = (time.time() - self.durations[id(request)]) * 1000 # in ms54 logger.debug('Duration for request #%s %s: %.3f ms' % (id(request), format_request_info(request, response), duration))...

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 yandex-tank 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