def stream_n_messages(n): """Stream n JSON responses --- tags: - Dynamic data parameters: - in: path name: n type: int produces: - application/json responses: 200: description: Streamed JSON responses. """ response = get_dict("url", "args", "headers", "origin") n = min(n, 100) def generate_stream(): for i in range(n): response["id"] = i yield json.dumps(response) + "\n" return Response(generate_stream(), headers={"Content-Type": "application/json"})
def sleep(): n = request.values.get('sleep', 5) time.sleep(float(n)) hdrs = get_dict('headers') return Response(response=json.dumps(hdrs), status=200, headers={'Content-Type': 'application/json'})
def large_header(): n = request.values.get('size', 63 * 1024) req_headers = get_dict('headers') resp_headers = {'Content-Type': 'application/json', 'Cookie': 'a' * int(n)} return Response(response=json.dumps(req_headers), status=200, headers=resp_headers)
def large_header(): n = request.values.get('size', 63*1024) req_headers = get_dict('headers') resp_headers = { 'Content-Type': 'application/json', 'Cookie': 'a'*int(n) } return Response(response=json.dumps(req_headers), status=200, headers=resp_headers)
def delay_response_long_max(delay): ''' Overwrites the built-in delay endpoint, to allow much longer delays that the 10-sec max that is hard-coded into the standard implementation. ''' delay = min(delay, MAX_DELAY_SECONDS) sleep(delay) return jsonify( get_dict('url', 'args', 'form', 'data', 'origin', 'headers', 'files'))
def view_get(): """The request's query parameters. --- tags: - HTTP Methods produces: - application/json responses: 200: description: The request's query parameters. """ return jsonify(get_dict("url", "args", "headers", "origin"))
def view_headers(): """Return the incoming request's HTTP headers. --- tags: - Request inspection produces: - application/json responses: 200: description: The request's headers. """ return jsonify(get_dict('headers'))
def view_get_xml(): """The request's query parameters (in XML). --- tags: - HTTP Methods produces: - application/xml responses: 200: description: The request's query parameters. """ return get_xml(get_dict("url", "args", "headers", "origin"))
def view_brotli_encoded_content(): """Returns Brotli-encoded data. --- tags: - Response formats produces: - application/json responses: 200: description: Brotli-encoded data. """ return jsonify( get_dict("origin", "headers", method=request.method, brotli=True))
def view_deflate_encoded_content(): """Returns Deflate-encoded data. --- tags: - Response formats produces: - application/json responses: 200: description: Defalte-encoded data. """ return jsonify( get_dict("origin", "headers", method=request.method, deflated=True))
def view_delete(): """The request's DELETE parameters. --- tags: - HTTP Methods produces: - application/json responses: 200: description: The request's DELETE parameters. """ return jsonify( get_dict("url", "args", "form", "data", "origin", "headers", "files", "json"))
def view_patch_xml(): """The request's PATCH parameters (in XML). --- tags: - HTTP Methods produces: - application/xml responses: 200: description: The request's PATCH parameters. """ return get_xml( get_dict("url", "args", "form", "data", "origin", "headers", "files", "json"))
def serve_error_based_on_counter(): global COUNTER COUNTER += 1 if COUNTER % 3 == 0: req_headers = get_dict('headers') return Response(response=json.dumps(req_headers), status=200, headers={'Content-Type': 'application/json'}) else: retry_times = 3 - COUNTER % 3 msg = 'The server had an error. Try again {retry_times} more {time_p}' time_p = 'time' if retry_times == 1 else 'times' content = { 'error':msg.format(retry_times=retry_times, time_p=time_p), 'status': 500, } return Response(response=json.dumps(content), status=500, headers={'Content-Type': 'application/json'})
def serve_error_based_on_counter(): global COUNTER COUNTER += 1 if COUNTER % 3 == 0: req_headers = get_dict('headers') return Response(response=json.dumps(req_headers), status=200, headers={'Content-Type': 'application/json'}) else: retry_times = 3 - COUNTER % 3 msg = 'The server had an error. Try again {retry_times} more {time_p}' time_p = 'time' if retry_times == 1 else 'times' content = { 'error': msg.format(retry_times=retry_times, time_p=time_p), 'counter': COUNTER, 'status': 500, } return Response(response=json.dumps(content), status=500, headers={'Content-Type': 'application/json'})
def delay_response(delay): """Returns a delayed response (max of 10 seconds). --- tags: - Dynamic data parameters: - in: path name: delay type: int produces: - application/json responses: 200: description: A delayed response. """ delay = min(float(delay), 10) time.sleep(delay) return jsonify( get_dict("url", "args", "form", "data", "origin", "headers", "files"))
def view_anything(anything=None): """Returns anything passed in request data. --- tags: - Anything produces: - application/json responses: 200: description: Anything passed in request """ return jsonify( get_dict( "url", "args", "headers", "origin", "method", "form", "data", "files", "json", ))
def post(self, group_name): data_id = str(uuid.uuid4()) data.set(group_name, data_id, value=get_dict(*KEYS)) return {data_id: data.get(group_name, data_id)}
def put(self, group_name, data_id): data.update(group_name, data_id, update_dict=get_dict(*KEYS)) return data.get(group_name, data_id)