Skip to content

Commit

Permalink
exclude examples from build and add fastapi example
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnasism committed Mar 17, 2024
1 parent 0f25ed7 commit 67e16f7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
52 changes: 52 additions & 0 deletions examples/fastapi_snippet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import random

import requests
from fastapi import APIRouter, status
from fastapi.responses import PlainTextResponse, Response

from retry_later import retry_later

routers = APIRouter()


@routers.get(
"/health",
response_model=str,
description="health check",
name="health",
response_class=PlainTextResponse,
)
async def health() -> Response:
doesnt_exist_url: str = "https://spacehook-4-o4569495.deta.app/api/hook/doesnt-exist"
exists_url: str = "https://spacehook-4-o4569495.deta.app/api/hook/ok"
url_choices_tuple = (doesnt_exist_url, exists_url)
await call_hook_from_choice(url_choices_tuple)
return Response(
content="OK",
status_code=status.HTTP_200_OK,
headers={"content-Type": "text/plain"},
)


@retry_later(exceptions=ConnectionError, max_retries=10)
async def call_hook_from_choice(url_choices: tuple[str]):
response = requests.get(random.choice(url_choices))
if response.status_code == 404:
raise ConnectionError(f"Could not connect. Response: {response.status_code}")
else:
print("Called endpoint successfully!")


"""Console Logs:
INFO: 127.0.0.1:56600 - "GET /api/v1/health HTTP/1.1" 200 OK
ERROR:root:[retry later] Retrying in 5s due to Could not connect. Response: 404
ERROR:root:[retry later] Retrying in 5s due to Could not connect. Response: 404
Called endpoint successfully!
"""

"""
You can see that the retry happens after the /health endpoint response is sent.
It doesn't wait for call_hook_from_choice to execute.
"""
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ include-package-data = false

[tool.setuptools.packages.find]
include = ["retry_later*"]
exclude = ["docs*", "tests*"]
exclude = ["docs*", "tests*", "examples*"]

[project.optional-dependencies]
dev = ["pytest", "pytest-cov", "pytest-xdist", "pytest-asyncio", "pylint", "mypy", "typing-extensions", "pre-commit", "ruff"]
Expand Down

0 comments on commit 67e16f7

Please sign in to comment.