https://prefect.io logo
j

Javier Buitrago

08/16/2023, 10:09 PM
Hi there! I really appreciate your help concerning fetch data from Web APIs, in Airflow I understand this can be done via HTTPSensor, HttpOperator... so, what it would be the equivalent in Prefect?, thanks in advance!
n

Nate

08/16/2023, 10:50 PM
hi @Javier Buitrago - we don't have a specific prebuilt task for making http requests you could implement a task that does this pretty simply with an http client like
httpx
Copy code
import httpx
from prefect import flow, task

@task
def http_request(method, url, **kwargs):
    with httpx.Client() as client:
        response = client.request(method, url, **kwargs)
        return response.json()

@flow(log_prints=True)
def web_flow():
    data = http_request("GET", "<https://jsonplaceholder.typicode.com/todos/1>")
    print(data)

if __name__ == "__main__":
    web_flow()
🙌 1
did you have something more specific in mind?
j

Javier Buitrago

08/16/2023, 11:25 PM
thank you for your help @Nate!, yes I have a similar code but I was thinking I was missing something about fetch API data,,,
n

Nate

08/16/2023, 11:28 PM
is
fetch API data
something specific you're talking about or do you just mean fetching data from some API? if the latter, you could replace the
httpx
call with a call to your API of interest
j

Javier Buitrago

08/16/2023, 11:35 PM
yes Nate, is the last option you say... I need fetch data from an API, which has several security restrictions...
n

Nate

08/16/2023, 11:36 PM
cool! let me know if you need any more help implementing your task in a way that respects your restrictions!
j

Javier Buitrago

08/17/2023, 12:25 AM
thank you @Nate!