Пример #1
0
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"})
Пример #2
0
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'})
Пример #3
0
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)
Пример #4
0
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)
Пример #5
0
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'))
Пример #6
0
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"))
Пример #7
0
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'))
Пример #8
0
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"))
Пример #9
0
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))
Пример #10
0
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))
Пример #11
0
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"))
Пример #12
0
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"))
Пример #13
0
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'})
Пример #14
0
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'})
Пример #15
0
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"))
Пример #16
0
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",
        ))
Пример #17
0
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'})
Пример #18
0
 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)}
Пример #19
0
 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)