Skip to content

Commit ca215f5

Browse files
authored
Merge pull request #78 from stealthrocket/example-gh-stats
doc: add github stats example
2 parents 0b93070 + a1b11ae commit ca215f5

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

examples/github_stats/__init__.py

Whitespace-only changes.

examples/github_stats/app.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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"

examples/github_stats/test_app.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This file is not part of the example. It is a test file to ensure the example
2+
# works as expected during the CI process.
3+
4+
5+
import os
6+
import unittest
7+
from unittest import mock
8+
9+
from fastapi.testclient import TestClient
10+
11+
from ... import function_service
12+
from ...test_client import ServerTest
13+
14+
15+
class TestGithubStats(unittest.TestCase):
16+
@mock.patch.dict(
17+
os.environ,
18+
{
19+
"DISPATCH_ENDPOINT_URL": "http://function-service",
20+
"DISPATCH_API_KEY": "0000000000000000",
21+
},
22+
)
23+
def test_foo(self):
24+
from . import app
25+
26+
server = ServerTest()
27+
servicer = server.servicer
28+
app.dispatch._client = server.client
29+
app.get_repo_info._client = server.client
30+
app.get_contributors._client = server.client
31+
app.main._client = server.client
32+
33+
http_client = TestClient(app.app, base_url="http://dispatch-service")
34+
app_client = function_service.client(http_client)
35+
36+
response = http_client.get("/")
37+
self.assertEqual(response.status_code, 200)
38+
39+
server.execute(app_client)
40+
41+
self.assertEqual(len(servicer.responses), 1)

0 commit comments

Comments
 (0)