Giter Site home page Giter Site logo

Comments (2)

rafaelcaricio avatar rafaelcaricio commented on August 16, 2024

To cover most of the possible responses in APIs, the dictionary keys described bellow should be sufficient.

{
    "content": str,
    "json": Union[dict, List[dict]],
    "file": Union[IO[AnyStr], str],
    "redirect": str,
    "status": int,
    "headers": Dict[str, str]
}

Descriptions

Key Type Description
content str Body of HTTP request. No treatment/parsing of this value is done. The value is passed directly to the chosen framework.
json Union[dict, List[dict]] JSON value to be used in the body of the request. This is a shortcut to having the header "Content-Type: application/json" and serializing this value using the most common way done by the chosen framework.
file Union[IO[AnyStr], str] Data to be returned as byte stream. This is a shortcut for having the header "Content-Type: application/octet-stream". Uses the most common way to stream files with the chosen framework.
redirect str The path or full URL to be redirected. This is a shortcut for having the header "Location:" with HTTP status 301.
status int The HTTP status code to be used in the response. This value overrides any shortcut default status code.
headers Dict[str, str] The HTTP headers to be used in the response. This value is merged with the shortcut values with priority.

Examples

We have exposed here some examples of using different configurations of the dict we've defined above to describe the HTTP response of API handlers. The actual HTTP response value generated will vary depending on the framework chosen as runtime. The examples are a minimal illustration of what to expect to be the HTTP response.

"content" key example

When it's desired to return a "Hello world!" string with status 200:

def say_hello(params):
    return {"content": "Hello world!"}

Results in the HTTP response similar to:

HTTP/1.1 200 OK
Content-Type: text/plain

Hello world!

"json" key example

It's desired to return an JSON response with status 201:

def create(params):
    data = {
        "id": "uhHuehuE",
        "value": "something"
    }
    return {"json": data, "status": 201}

The HTTP response generated will be similar to:

HTTP/1.1 201 Created
Content-Type: application/json

{"id":"uhHuehuE","value":"something"}

from sticker.

rafaelcaricio avatar rafaelcaricio commented on August 16, 2024

Done. Basic support is done.

from sticker.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.