foiaghost/examples/foiaghost.py

44 lines
1.1 KiB
Python
Raw Normal View History

2023-05-08 06:05:26 +00:00
from ssl import SSLCertVerificationError, SSLError
2023-05-07 23:39:46 +00:00
import httpx
2023-05-08 01:06:28 +00:00
from beakers.beakers import Beaker
from beakers.recipe import Recipe
2023-04-27 06:25:07 +00:00
async def add_response(obj_with_url):
2023-05-07 23:39:46 +00:00
print(obj_with_url["url"])
2023-04-27 06:25:07 +00:00
url = obj_with_url["url"]
2023-05-08 04:48:19 +00:00
async with httpx.AsyncClient() as client:
response = await client.get(url)
2023-04-27 06:25:07 +00:00
return {
"url": url,
"status_code": response.status_code,
"response_body": response.text,
}
2023-05-07 23:39:46 +00:00
# current thinking, beakers exist within a recipe
2023-05-08 03:55:02 +00:00
recipe = Recipe("fetch urls", "url_example.db")
2023-05-08 00:17:28 +00:00
recipe.add_beaker("agencies")
recipe.add_beaker("responses")
2023-05-08 03:55:02 +00:00
recipe.add_beaker("bad_requests")
2023-05-08 00:17:28 +00:00
recipe.add_beaker("good_urls", temp=True)
recipe.add_beaker("missing_urls", temp=True)
recipe.add_conditional(
2023-05-07 23:39:46 +00:00
"agencies",
lambda x: x["url"].startswith("http"),
if_true="good_urls",
if_false="missing_urls",
)
2023-05-08 03:55:02 +00:00
recipe.add_transform(
2023-05-08 04:48:19 +00:00
"good_urls",
"responses",
add_response,
2023-05-08 06:05:26 +00:00
error_map={
(
2023-05-08 06:11:07 +00:00
httpx.HTTPError,
2023-05-08 06:05:26 +00:00
SSLCertVerificationError,
SSLError,
): "bad_requests"
},
2023-05-08 03:55:02 +00:00
)