invent-framework / invent

Express yourself with code.
https://invent-framework.github.io/
Apache License 2.0
109 stars 12 forks source link

It should be possible to make HTTP requests to and endpoint and process the result #30

Closed ntoll closed 1 month ago

ntoll commented 1 year ago

Use JavaScript's underlying fetch.

When in flight a "pending" card is shown. Two possible transitions can be called upon completion of the request:

ntoll commented 1 year ago

Depends upon #38

ntoll commented 1 year ago

First draft of example straw-man code:

from pypercard import web

@my_app.transition("my_card", "click", "button")
def get_data(card, datastore):
    request = web.get("https://my_url.com/some/path/", ok="it_worked_transition", error="error_transition")
    # request.ok = ...
    # request.error = ...
    return "waiting_card"

The it_worked_transition and error_transition have a call signature like this:

def it_worked_transition(card, datastore, response):
    result = response.json()
    # ... do stuff ...
    return "next_card"

Under the hood we're using JavaScript's fetch and the response is a JS proxy object representing the actual response from the fetch call, once that call resolves.

We ought to pass through arguments like timeout, headers, and other stuff they may need. The web object has HTTP get, post, put, delete and info methods a la requests.