def post_batch(request): requests = request.validated["body"]["requests"] request.log_context(batch_size=len(requests)) limit = request.registry.settings["batch_max_requests"] if limit and len(requests) > int(limit): error_msg = f"Number of requests is limited to {limit}" request.errors.add("body", "requests", error_msg) return if any([batch.path in req["path"] for req in requests]): error_msg = f"Recursive call on {batch.path} endpoint is forbidden." request.errors.add("body", "requests", error_msg) return responses = [] for subrequest_spec in requests: subrequest = build_request(request, subrequest_spec) log_context = { **request.log_context(), "path": subrequest.path, "method": subrequest.method, } try: # Invoke subrequest without individual transaction. resp, subrequest = request.follow_subrequest(subrequest, use_tweens=False) except httpexceptions.HTTPException as e: # Since some request in the batch failed, we need to stop the parent request # through Pyramid's transaction manager. 5XX errors are already caught by # pyramid_tm's commit_veto # https://github.com/Kinto/kinto/issues/624 if e.status_code == 409: request.tm.abort() if e.content_type == "application/json": resp = e else: # JSONify raw Pyramid errors. resp = errors.http_error(e) subrequest_logger.info("subrequest.summary", extra=log_context) dict_resp = build_response(resp, subrequest) responses.append(dict_resp) return {"responses": responses}
def post_batch(request): requests = request.validated['body']['requests'] batch_size = len(requests) limit = request.registry.settings['batch_max_requests'] if limit and len(requests) > int(limit): error_msg = 'Number of requests is limited to %s' % limit request.errors.add('body', 'requests', error_msg) return if any([batch.path in req['path'] for req in requests]): error_msg = 'Recursive call on %s endpoint is forbidden.' % batch.path request.errors.add('body', 'requests', error_msg) return responses = [] sublogger = logger.new() for subrequest_spec in requests: subrequest = build_request(request, subrequest_spec) sublogger.bind(path=subrequest.path, method=subrequest.method) try: # Invoke subrequest without individual transaction. resp, subrequest = request.follow_subrequest(subrequest, use_tweens=False) except httpexceptions.HTTPException as e: if e.content_type == 'application/json': resp = e else: # JSONify raw Pyramid errors. resp = errors.http_error(e) sublogger.bind(code=resp.status_code) sublogger.info('subrequest.summary') dict_resp = build_response(resp, subrequest) responses.append(dict_resp) # Rebing batch request for summary logger.bind(path=batch.path, method=request.method, batch_size=batch_size, agent=request.headers.get('User-Agent'),) return { 'responses': responses }
def post_batch(request): requests = request.validated['body']['requests'] request.log_context(batch_size=len(requests)) limit = request.registry.settings['batch_max_requests'] if limit and len(requests) > int(limit): error_msg = 'Number of requests is limited to {}'.format(limit) request.errors.add('body', 'requests', error_msg) return if any([batch.path in req['path'] for req in requests]): error_msg = 'Recursive call on {} endpoint is forbidden.'.format(batch.path) request.errors.add('body', 'requests', error_msg) return responses = [] for subrequest_spec in requests: subrequest = build_request(request, subrequest_spec) log_context = {**request.log_context(), 'path': subrequest.path, 'method': subrequest.method} try: # Invoke subrequest without individual transaction. resp, subrequest = request.follow_subrequest(subrequest, use_tweens=False) except httpexceptions.HTTPException as e: # Since some request in the batch failed, we need to stop the parent request # through Pyramid's transaction manager. 5XX errors are already caught by # pyramid_tm's commit_veto # https://github.com/Kinto/kinto/issues/624 if e.status_code == 409: request.tm.abort() if e.content_type == 'application/json': resp = e else: # JSONify raw Pyramid errors. resp = errors.http_error(e) subrequest_logger.info('subrequest.summary', extra=log_context) dict_resp = build_response(resp, subrequest) responses.append(dict_resp) return { 'responses': responses }
def post_batch(request): requests = request.validated['requests'] batch_size = len(requests) limit = request.registry.settings['batch_max_requests'] if limit and len(requests) > int(limit): error_msg = 'Number of requests is limited to %s' % limit request.errors.add('body', 'requests', error_msg) return if any([batch.path in req['path'] for req in requests]): error_msg = 'Recursive call on %s endpoint is forbidden.' % batch.path request.errors.add('body', 'requests', error_msg) return responses = [] sublogger = logger.new() for subrequest_spec in requests: subrequest = build_request(request, subrequest_spec) sublogger.bind(path=subrequest.path, method=subrequest.method) try: # Invoke subrequest without individual transaction. resp, subrequest = request.follow_subrequest(subrequest, use_tweens=False) except httpexceptions.HTTPException as e: if e.content_type == 'application/json': resp = e else: # JSONify raw Pyramid errors. resp = errors.http_error(e) sublogger.bind(code=resp.status_code) sublogger.info('subrequest.summary') dict_resp = build_response(resp, subrequest) responses.append(dict_resp) # Rebing batch request for summary logger.bind(path=batch.path, method=request.method, batch_size=batch_size, agent=request.headers.get('User-Agent'),) return { 'responses': responses }
def post_batch(request): requests = request.validated['body']['requests'] request.log_context(batch_size=len(requests)) limit = request.registry.settings['batch_max_requests'] if limit and len(requests) > int(limit): error_msg = 'Number of requests is limited to {}'.format(limit) request.errors.add('body', 'requests', error_msg) return if any([batch.path in req['path'] for req in requests]): error_msg = 'Recursive call on {} endpoint is forbidden.'.format( batch.path) request.errors.add('body', 'requests', error_msg) return responses = [] for subrequest_spec in requests: subrequest = build_request(request, subrequest_spec) log_context = { 'path': subrequest.path, 'method': subrequest.method, **request.log_context() } try: # Invoke subrequest without individual transaction. resp, subrequest = request.follow_subrequest(subrequest, use_tweens=False) except httpexceptions.HTTPException as e: if e.content_type == 'application/json': resp = e else: # JSONify raw Pyramid errors. resp = errors.http_error(e) subrequest_logger.info('subrequest.summary', extra=log_context) dict_resp = build_response(resp, subrequest) responses.append(dict_resp) return {'responses': responses}