-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
125 lines (94 loc) · 3.85 KB
/
app.py
File metadata and controls
125 lines (94 loc) · 3.85 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from fastapi import FastAPI, Request, HTTPException
from typing import Optional
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from azure.cosmos import CosmosClient
import os
from dotenv import load_dotenv
import random
load_dotenv()
# Initialize FastAPI
app = FastAPI(
title="Stoic Quotes API",
summary="The very best Stoic quotes from the three great Roman Stoics: Marcus Aurelius, Seneca, and Epictetus.",
version="0.0.1",
contact={
"name": "Rishab Kumar",
"url": "http://rishabkumar.com",
},
license_info={
"name": "MIT License",
"url": "https://opensource.org/license/mit",
},
)
# Initialize Cosmos Client
url = os.getenv("AZURE_COSMOSDB_URL")
key = os.getenv("AZURE_COSMOSDB_KEY")
client = CosmosClient(url, credential=key)
# Select database
database_name = 'Quotes'
database = client.get_database_client(database_name)
# Select container
container_name = 'stoic-quotes'
container = database.get_container_client(container_name)
# Query for all documents
documents = list(container.read_all_items())
templates = Jinja2Templates(directory="templates")
# Load Browser Favicon Icon
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get('/', description="Return the home page with a random quote.")
async def home(request: Request):
"""
Return the home page with a random quote.
- **request**: The request object.
"""
random_document = random.choice(documents)
random_quote = random.choice(random_document['quotes'])
return templates.TemplateResponse('index.html', {"request": request, "quote": random_quote['quote'], "author": random_quote['author']})
@app.get('/api/random', description="Return a random quote.")
async def get_quote():
"""
Return a random quote.
This endpoint returns a random quote from the database in JSON format.
"""
# Select a random document
random_document = random.choice(documents)
# Select a random quote from the 'quotes' array in the document
random_quote = random.choice(random_document['quotes'])
return {"quote": random_quote['quote'], "author": random_quote['author']}
@app.get('/api/search', description="Search for quotes containing a specific word.")
async def search(word: Optional[str] = None):
"""
Search for quotes containing a specific word.
- **word**: The word to search for in the quotes.
This endpoint returns all quotes that contain the word in JSON format.
"""
if not word:
return HTTPException(status_code=400, detail="No word provided for search.")
# Query for all documents
documents = list(container.read_all_items())
# Filter the quotes where the word appears
matching_quotes = [quote for document in documents for quote in document['quotes'] if word.lower() in quote['quote'].lower()]
if not matching_quotes:
return {"response": 200, "message": "No quotes matched the query."}
return {"response": 200, "results": matching_quotes}
# Build Pagination endpoint
@app.get('/api/quotes', description="Return a paginated list of quotes.")
async def pagination(page: Optional[int] = 1, limit: Optional[int] = 10):
"""
Return a paginated list of quotes.
- **page**: The page number to return.
- **limit**: The number of quotes to return per page.
This endpoint returns a paginated list of quotes in JSON format.
"""
# Calculate the start and end index
start_index = (page - 1) * limit
end_index = page * limit
# Query for all documents
documents = list(container.read_all_items())
# Extract all quotes from the documents
all_quotes = [quote for document in documents for quote in document['quotes']]
# Return the paginated list of quotes
return {"response": 200, "results": all_quotes[start_index:end_index]}
if __name__ == '__main__':
app.run()