Skip to content

Commit a737780

Browse files
committed
update main with gist
1 parent 4b673df commit a737780

File tree

2 files changed

+78
-35
lines changed

2 files changed

+78
-35
lines changed
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
from fastapi import FastAPI # import FastApi class
2-
from fastapi import HTTPException # import HHTPExeption class
3-
from storage import TaskStorage # import TaskStorage class
4-
app = FastAPI() # create copy of FastAPi class
5-
storage = TaskStorage() # create copy of TaskStorage class
1+
from fastapi import FastAPI
2+
from fastapi import HTTPException
3+
from storage_gist import GistStorage
4+
app = FastAPI()
5+
storage = GistStorage()
66
# we display a list of all received tasks.
7-
@app.get("/tasks") # use standart FastApi get decorator
8-
def get_tasks(): # get task method
9-
return storage.load() # return task lsit
7+
@app.get("/tasks")
8+
def get_tasks():
9+
return storage.load()
1010
# We add a new task, add it to the task list, and increment the id counter.
11-
@app.post("/tasks") # use standart FastAPI post decorator
12-
def create_task(name: str, condition: str = 'new'): # create new task method
13-
tasks = storage.load() # task list now in tasks
14-
new_id = max((task['id'] for task in tasks), default = 0) + 1 # crate new id fo task
15-
task = {'id': new_id, 'name': name, 'condition': condition} # task dict
16-
tasks.append(task) # append task on list
17-
storage.save(tasks) # save task
18-
return task # return new task
11+
@app.post("/tasks")
12+
def create_task(name: str, condition: str = 'new'):
13+
tasks = storage.load()
14+
new_id = max((task['id'] for task in tasks), default = 0) + 1
15+
task = {'id': new_id, 'name': name, 'condition': condition}
16+
tasks.append(task)
17+
storage.save(tasks)
18+
return task
1919
# We update the task; if the task is missing, we raise an error.
20-
@app.put("/tasks/{task_id}") # use standart FastAPI put decorator
21-
def update_task(task_id: int, name: str, condition: str): # update task meyhod
22-
tasks = storage.load() # loads all tasks list
23-
for task in tasks: # finding task cicle
24-
if task['id'] == task_id: # check task id in list
25-
task['name'] = name # update name if id in list
26-
task['condition'] = condition # update status if id in list
27-
storage.save(tasks) # save task in list
28-
return task # return updated task
29-
raise HTTPException(status_code = 404, detail = 'task not found') # raise if id not in list
20+
@app.put("/tasks/{task_id}")
21+
def update_task(task_id: int, name: str, condition: str):
22+
tasks = storage.load()
23+
for task in tasks:
24+
if task['id'] == task_id:
25+
task['name'] = name
26+
task['condition'] = condition
27+
storage.save(tasks)
28+
return task
29+
raise HTTPException(status_code = 404, detail = 'task not found')
3030
# We search for a task by ID, if we find it, we delete it, if not, we raise an error
31-
@app.delete("/tasks/{task_id}") # use standart FastApi delete decorator
32-
def delete_task(task_id: int): # delete task method
33-
tasks = storage.load() # loads all tasks list
34-
for task in tasks: # finding task cicle
35-
if task['id'] == task_id: # check task id in list
36-
tasks.remove(task) # remove task if id in list
37-
storage.save(tasks) # save tasks list
38-
return 'task deleted' # return delete message
39-
raise HTTPException(status_code = 404, detail = 'task not found') # raise if id not in list
31+
@app.delete("/tasks/{task_id}")
32+
def delete_task(task_id: int):
33+
tasks = storage.load()
34+
for task in tasks:
35+
if task['id'] == task_id:
36+
tasks.remove(task)
37+
storage.save(tasks)
38+
return 'task deleted'
39+
raise HTTPException(status_code = 404, detail = 'task not found')
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import requests
3+
import json
4+
GITHUB_API = 'https://api.github.com'
5+
class GistStorage:
6+
def __init__(self, token: str = None, gist_id: str = None, filename: str = 'tasks.json'):
7+
self.token: str = token or os.getenv('GITHUB_TOKEN')
8+
self.gist_id: str = gist_id or os.getenv('GIST_ID')
9+
self.filename: str = filename
10+
if not self.token or not self.gist_id:
11+
raise RuntimeError('GITHUB_TOKEN and GIST_ID must be given')
12+
self.headers: dict = {
13+
'Authorization': f'token {self.token}',
14+
'Accept': 'application/vnd.github+json'
15+
}
16+
17+
def _gist_url(self) -> str:
18+
return f'{GITHUB_API}/gists/{self.gist_id}'
19+
# loading task list from gist
20+
def load(self) -> list[dict]:
21+
r = requests.get(self._gist_url(), headers = self.headers, timeout = 10)
22+
r.raise_for_status()
23+
gist_data: dict = r.json()
24+
files: dict = gist_data.get('files', {})
25+
if self.filename not in files:
26+
return []
27+
content: str = files[self.filename]['content']
28+
try:
29+
data: dict = json.loads(content)
30+
return data.get('tasks', [])
31+
except json.JSONDecodeError:
32+
return []
33+
# saving task list on gist
34+
def save(self, tasks: list[dict]) -> None:
35+
body: dict = {
36+
'files': {
37+
self.filename: {
38+
'content': json.dumps({'tasks': tasks})
39+
}
40+
}
41+
}
42+
r = requests.patch(self._gist_url(), headers = self.headers, json = body, timeout = 10)
43+
r.raise_for_status()

0 commit comments

Comments
 (0)