|
| 1 | +"""Github repository stats example. |
| 2 | +
|
| 3 | +This example demonstrates how to use async functions orchestrated by Dispatch. |
| 4 | +
|
| 5 | +Make sure to follow the setup instructions at |
| 6 | +https://docs.stealthrocket.cloud/dispatch/stateful-functions/getting-started/ |
| 7 | +
|
| 8 | +Run with: |
| 9 | +
|
| 10 | +uvicorn app:app |
| 11 | +
|
| 12 | +
|
| 13 | +Logs will show a pipeline of functions being called and their results. |
| 14 | +
|
| 15 | +""" |
| 16 | + |
| 17 | +import httpx |
| 18 | +from fastapi import FastAPI |
| 19 | + |
| 20 | +from dispatch.fastapi import Dispatch |
| 21 | + |
| 22 | +app = FastAPI() |
| 23 | + |
| 24 | +dispatch = Dispatch(app) |
| 25 | + |
| 26 | + |
| 27 | +@dispatch.function |
| 28 | +async def get_repo_info(repo_owner: str, repo_name: str): |
| 29 | + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}" |
| 30 | + api_response = httpx.get(url) |
| 31 | + api_response.raise_for_status() |
| 32 | + repo_info = api_response.json() |
| 33 | + return repo_info |
| 34 | + |
| 35 | + |
| 36 | +@dispatch.function |
| 37 | +async def get_contributors(repo_info: dict): |
| 38 | + contributors_url = repo_info["contributors_url"] |
| 39 | + response = httpx.get(contributors_url) |
| 40 | + response.raise_for_status() |
| 41 | + contributors = response.json() |
| 42 | + return contributors |
| 43 | + |
| 44 | + |
| 45 | +@dispatch.function |
| 46 | +async def main(): |
| 47 | + repo_info = await get_repo_info("stealthrocket", "coroutine") |
| 48 | + print( |
| 49 | + f"""Repository: {repo_info['full_name']} |
| 50 | +Stars: {repo_info['stargazers_count']} |
| 51 | +Watchers: {repo_info['watchers_count']} |
| 52 | +Forks: {repo_info['forks_count']}""" |
| 53 | + ) |
| 54 | + |
| 55 | + contributors = await get_contributors(repo_info) |
| 56 | + print(f"Contributors: {len(contributors)}") |
| 57 | + return |
| 58 | + |
| 59 | + |
| 60 | +@app.get("/") |
| 61 | +def root(): |
| 62 | + main.dispatch() |
| 63 | + return "OK" |
0 commit comments