Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ptmcg committed Apr 6, 2021
0 parents commit 79df2db
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
Empty file added client/__init__.py
Empty file.
Empty file added client/rq_client.py
Empty file.
11 changes: 11 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.9.2-alpine

WORKDIR /usr/src/app

COPY src .

RUN pip install --no-cache-dir -r requirements.txt

CMD [ "uvicorn", "--host", "0.0.0.0", "rest_queue:app" ]


Empty file added rest_queue/src/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions rest_queue/src/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
uvicorn
fastapi
125 changes: 125 additions & 0 deletions rest_queue/src/rest_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#
# simple_rest_queue.py
#
# A simple FastAPI web service that maintains multiple named FIFO queues
# that can be created, pushed, and popped using REST calls
#
from collections import deque
from typing import Dict

import fastapi

# registry for REST-accessible queues
queues: Dict[str, deque] = {}


app = fastapi.FastAPI()


@app.post("/queues/{queue_name}", status_code=fastapi.status.HTTP_201_CREATED)
async def queue_new(queue_name: str):
"""
Create new queue with given queue name.
Raises an exception if the queue already exists.
"""
if queue_name in queues:
raise fastapi.HTTPException(status_code=fastapi.status.HTTP_409_CONFLICT,
detail=f"Queue {queue_name!r} already exists")

# create new deque and add to registry under the given name
queues[queue_name] = deque()

return {"message": f"created {queue_name!r}"}


@app.get("/queues/{queue_name}", status_code=fastapi.status.HTTP_200_OK)
async def queue_pop(queue_name: str):
"""
Pop oldest value from queue, or None if queue is empty.
"""
if queue_name not in queues:
raise fastapi.HTTPException(status_code=fastapi.status.HTTP_404_NOT_FOUND,
detail=f"No such queue {queue_name!r}")

q = queues[queue_name]
if q:
value = q.popleft()
else:
value = None

return {"message": f"get from queue {queue_name!r}",
"data": value,
"remaining_queue_size": len(q),
}


@app.put("/queues/{queue_name}", status_code=fastapi.status.HTTP_200_OK)
async def queue_push(queue_name: str, value: str):
"""
Push new value onto queue.
"""
if queue_name not in queues:
raise fastapi.HTTPException(status_code=fastapi.status.HTTP_404_NOT_FOUND,
detail=f"No such queue {queue_name!r}")

q = queues[queue_name]
q.append(value)

return {
"message": f"put to queue {queue_name!r}",
"remaining_queue_size": len(q),
}


@app.delete("/queues/{queue_name}", status_code=fastapi.status.HTTP_200_OK)
async def queue_delete(queue_name: str,
safe_delete: bool = True):
"""
Delete queue.
"""
if queue_name not in queues:
raise fastapi.HTTPException(status_code=fastapi.status.HTTP_404_NOT_FOUND,
detail=f"No such queue {queue_name!r}")

q = queues[queue_name]

if safe_delete and q:
raise fastapi.HTTPException(status_code=fastapi.status.HTTP_409_CONFLICT,
detail=f"Queue {queue_name!r} not empty (contains {len(q)} items)")

# remove queue from registry
del queues[queue_name]

return {
"message": f"deleted queue {queue_name!r}",
"remaining_queue_size": len(q),
}


@app.get("/queues/{queue_name}/list", status_code=fastapi.status.HTTP_200_OK)
async def queue_list(queue_name: str):
"""
List out contents of queue.
"""
if queue_name not in queues:
raise fastapi.HTTPException(status_code=fastapi.status.HTTP_404_NOT_FOUND,
detail=f"No such queue {queue_name!r}")

q = queues[queue_name]

return {
"message": f"items in queue {queue_name!r}",
"items": list(q),
}


@app.get("/queues", status_code=fastapi.status.HTTP_200_OK)
async def queue_list_all():
"""
List out all queue names.
"""
return {
"message": f"list of queue names",
"items": list(queues),
}

0 comments on commit 79df2db

Please sign in to comment.