-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
58 lines (43 loc) · 1.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
import requests
import typer
import logging
app = typer.Typer()
BASE_URL = "https://insight-ai-api.fly.dev/"
def fetch_data(endpoint: str):
"""Fetch data from the API."""
url = BASE_URL + endpoint
logging.info(f"{url=}")
headers = {"Accept": "application/json"}
response = requests.post(url, headers=headers, timeout=5)
if response.status_code == 200:
return response.json()
else:
response.raise_for_status()
@app.command()
def get_random_fact(n: int):
"""
Generate a random fact for a given number.
:param n:
"""
endpoint = f"facts/fact_to_number?n={n}"
try:
data = fetch_data(endpoint)
typer.echo(data['value'])
except requests.RequestException as e:
typer.echo(f"Error fetching data: {e}")
@app.command()
def translate_to_czech(word: str):
"""
Fetch data from the insight-ai-api endpoint.
Args:
- endpoint (str): The endpoint to fetch data from. Example: "data/123"
"""
endpoint = f"translate/translate?text={word}"
try:
data = fetch_data(endpoint)
typer.echo(data['value'])
except requests.RequestException as e:
typer.echo(f"Error fetching data: {e}")
if __name__ == "__main__":
app()