version fonctionnelle minimale 1er fevrier 2023

This commit is contained in:
Francois Pelletier 2023-02-02 00:34:51 -05:00
parent 2751ec8c56
commit 0dea3ce23b
10 changed files with 143 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/.idea/
/tmp/

19
Dockerfile Normal file
View file

@ -0,0 +1,19 @@
FROM python:3.10-slim
# Set the working directory
WORKDIR /app
# Copy the requirements file
COPY requirements.txt .
# Install Python dependencies
RUN pip install -r requirements.txt
# Copy the application code
COPY main.py .
# Expose the application port
EXPOSE 8000
# Run the application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

1
build-local.sh Normal file
View file

@ -0,0 +1 @@
docker build -t local/icalendar-maker .

5
docker-run.sh Normal file
View file

@ -0,0 +1,5 @@
docker stop icalendar-maker
docker rm icalendar-maker
docker network create icalendar
# Ce programme sert à lancer le icalendar-maker dans un docker localement pour tester
docker run -p 8000:8000 --name icalendar-maker --volume=${PWD}/tmp:/tmp --network icalendar local/icalendar-maker

2
install.sh Normal file
View file

@ -0,0 +1,2 @@
#!/bin/sh
pip install -r requirements.txt

72
main.py Normal file
View file

@ -0,0 +1,72 @@
import os
from typing import List
import fastapi
import uvicorn
from ics import Calendar, Event
import pydantic
from datetime import datetime
from fastapi.responses import FileResponse
# pydantic model for icalendar events
class EventData(pydantic.BaseModel):
name: str
description: str
location: str
start: datetime
end: datetime
recurrence: str
attendees: List[str]
organizer: str
uid: str
url: str
app = fastapi.FastAPI()
@app.get("/")
def read_root():
return {"Application": "ICalendar Maker"}
# this function returns an icalendar file
@app.get("/generer/icalendar")
async def generer_icalendar(event_data: EventData):
cal = Calendar()
event = Event()
event.name = event_data.name
event.begin = event_data.start
event.end = event_data.end
event.description = event_data.description
event.location = event_data.location
event.recurrence = event_data.recurrence
event.uid = event_data.uid
event.attendees = event_data.attendees
event.organizer = event_data.organizer
event.url = event_data.url
cal.events.add(event)
# filename is uid and current timestamp
filename = event.uid + datetime.now().strftime("%Y%m%dT%H%M%S") + ".ics"
# create a tmp folder if it doesn't exist
if not os.path.exists("/tmp"):
os.mkdir("/tmp")
print("tmp folder created")
# write a file named calendar.ics containing file_content
with open(f"/tmp/{filename}", "w") as f:
f.write(cal.serialize())
f.close()
print(f"file {filename} created")
# return a pydantic FileResponse
return FileResponse(filename=f"{filename}",
path=f"/tmp/{filename}",
media_type="text/calendar")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

10
requirements.txt Normal file
View file

@ -0,0 +1,10 @@
anyio==3.6.2
click==8.1.3
fastapi==0.89.1
h11==0.14.0
ics==0.7.2
pydantic==1.10.4
pytz==2022.7.1
sniffio==1.3.0
starlette==0.22.0
uvicorn==0.20.0

1
run.sh Normal file
View file

@ -0,0 +1 @@
uvicorn main:app --reload

12
setup.py Normal file
View file

@ -0,0 +1,12 @@
from setuptools import setup
setup(
name='icalendar-maker',
version='',
packages=[''],
url='',
license='',
author='francois',
author_email='',
description=''
)

19
test.http Normal file
View file

@ -0,0 +1,19 @@
# Test your FastAPI endpoints
GET http://127.0.0.1:8000/generer/icalendar
Content-Type: application/json
Accept: text/calendar
Accept-Encoding: gzip, deflate
{
"summary": "test",
"description": "test",
"location": "test",
"start": "2024-01-01T00:00:00",
"end": "2024-01-01T01:00:00",
"recurrence": "RRULE:FREQ=DAILY;COUNT=1",
"uid": "test",
"attendees": ["guest1@jevalide.ca", "guest2@jevalide.ca"],
"organizer": "francois@jevalide.ca",
"url": "https://jevalide.ca/reunion"
}