Skip to content

Commit

Permalink
feat: add apispec for messages api. fix #146
Browse files Browse the repository at this point in the history
  • Loading branch information
xdanielsb committed Jul 31, 2020
1 parent fcf6db0 commit 9a0ba62
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
79 changes: 79 additions & 0 deletions remoulade/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from collections import defaultdict
from operator import itemgetter

from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from flask import Flask, request
from marshmallow import ValidationError
from werkzeug.exceptions import HTTPException, NotFound
Expand All @@ -15,6 +17,8 @@

app = Flask(__name__)

spec = APISpec(title="Remoulade Messages API", version="1.0.0", openapi_version="3.0.2", plugins=[MarshmallowPlugin()],)


def sort_dicts(data, column, reverse=False):
""" Sort an array of dicts by a given column """
Expand All @@ -31,6 +35,16 @@ def dict_has(item, keys, value):

@app.route("/messages/states")
def get_states():
"""Get Messages
---
get:
description: get messages in remoulade
responses:
200:
content:
application/json:
schema: PageSchema
"""
args = PageSchema().load(request.args.to_dict())
backend = remoulade.get_broker().get_state_backend()
data = [s.as_dict(exclude_keys=("args", "kwargs", "options")) for s in backend.get_states()]
Expand All @@ -49,6 +63,15 @@ def get_states():

@app.route("/messages/state/<message_id>")
def get_state(message_id):
"""Get a Message
---
get:
description: get message associated with a message_id
responses:
200:
content:
application/json
"""
backend = remoulade.get_broker().get_state_backend()
data = backend.get_state(message_id)
if data is None:
Expand All @@ -58,13 +81,31 @@ def get_state(message_id):

@app.route("/messages/cancel/<message_id>", methods=["POST"])
def cancel_message(message_id):
"""Cancel a Message
---
post:
description: Cancel a message asociated with a given message_id
responses:
200:
content:
application/json
"""
backend = remoulade.get_broker().get_cancel_backend()
backend.cancel([message_id])
return {"result": "ok"}


@app.route("/messages/requeue/<message_id>")
def requeue_message(message_id):
"""Requeue a Message
---
post:
description: Requeue a given message associated with a message_id
responses:
200:
content:
application/json
"""
broker = remoulade.get_broker()
backend = broker.get_state_backend()
state = backend.get_state(message_id)
Expand All @@ -80,6 +121,15 @@ def requeue_message(message_id):

@app.route("/scheduled/jobs")
def get_scheduled_jobs():
"""Get scheduled jobs
---
post:
description: Get scheduled jobs in remoulade
responses:
200:
content:
application/json
"""
try:
scheduler = get_scheduler()
except NoScheduler:
Expand All @@ -90,6 +140,16 @@ def get_scheduled_jobs():

@app.route("/messages", methods=["POST"])
def enqueue_message():
"""Enqueue a message
---
post:
description: enqueue a message
responses:
200:
content:
application/json:
schema: MessageSchema
"""
payload = MessageSchema().load(request.json)
actor = get_broker().get_actor(payload.pop("actor_name"))
options = payload.pop("options") or {}
Expand All @@ -99,11 +159,30 @@ def enqueue_message():

@app.route("/actors")
def get_actors():
"""Get actors
---
post:
description: Get actors in remoulade
responses:
200:
content:
application/json
"""
return {"result": [actor.as_dict() for actor in get_broker().actors.values()]}


@app.route("/groups")
def get_groups():
"""Get Groups
---
get:
description: get groups in remoulade
responses:
200:
content:
application/json:
schema: PageSchema
"""
args = PageSchema().load(request.args.to_dict())
backend = remoulade.get_broker().get_state_backend()
groups = defaultdict(list)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def rel(*xs):
"rabbitmq": ["amqpstorm>=2.6,<3",],
"redis": ["redis>=3.0.1,<4.0",],
"watch": ["watchdog>=0.8,<0.9", "watchdog_gevent==0.1",],
"server": ["flask>=1.1,<2", "marshmallow>=3"],
"server": ["flask>=1.1,<2", "marshmallow>=3", "apispec>=3"],
}

extra_dependencies["all"] = list(set(sum(extra_dependencies.values(), [])))
Expand Down

0 comments on commit 9a0ba62

Please sign in to comment.