Unverified Commit 40590158 authored by Dan Jones's avatar Dan Jones
Browse files

refactor: change clients endpoint to marshmallow

parent 23cc052e
from flask_restful import Resource, reqparse, abort, fields, marshal_with from flask_restful import Resource, request, abort
from marshmallow import Schema, fields
import json import json
import os import os
import random import random
import string import string
class ClientSchema(Schema):
client_id = fields.Str(required=True)
client_name = fields.Str(required=True)
subscription = fields.Str(required=True)
class ClientsFile: class ClientsFile:
file = "clients.json" file = "clients.json"
mtime = 0 mtime = 0
...@@ -13,7 +20,6 @@ class ClientsFile: ...@@ -13,7 +20,6 @@ class ClientsFile:
def __init__(self): def __init__(self):
self.get() self.get()
self.setup_request_parser()
def get(self): def get(self):
try: try:
...@@ -36,7 +42,7 @@ class ClientsFile: ...@@ -36,7 +42,7 @@ class ClientsFile:
return client return client
def add(self, client): def add(self, client):
client.secret = self.secret() client['secret'] = self.secret()
self.clients[client["client_id"]] = client self.clients[client["client_id"]] = client
self.save() self.save()
return client return client
...@@ -69,70 +75,63 @@ class ClientsFile: ...@@ -69,70 +75,63 @@ class ClientsFile:
) )
return str(res) return str(res)
def setup_request_parser(self):
parser = reqparse.RequestParser()
parser.add_argument(
"client_id", type=str, help="A unique name to identify the client"
)
parser.add_argument(
"client_name", type=str, help="A human friendly name to identify the client"
)
parser.add_argument(
"subscription",
type=str,
help="A dot delimited string identify topics to subscribe to",
)
self.parser = parser
def parse(self):
return self.parser.parse_args()
resource_fields = {
"client_id": fields.String,
"client_name": fields.String,
"subscription": fields.String,
}
clients_file = ClientsFile() clients_file = ClientsFile()
# Client # Client
class Client(Resource): class Client(Resource):
clients_file = None
def __init__(self):
self.schema = ClientSchema()
self.clients_file = ClientsFile()
def get(self, client_id): def get(self, client_id):
client = clients_file.find(client_id) client = self.clients_file.find(client_id)
del client['secret'] del client['secret']
if not client: if not client:
abort(404, message="No client with id: {}".format(client_id)) abort(404, message="No client with id: {}".format(client_id))
return client return client
def delete(self, todo_id): def delete(self, todo_id):
client = clients_file.find(client_id) client = self.clients_file.find(client_id)
if not client: if not client:
abort(404, message="No client with id: {}".format(client_id)) abort(404, message="No client with id: {}".format(client_id))
else: else:
clients_file.remove(client) self.clients_file.remove(client)
return client, 204 return client, 204
def put(self, client_id): def put(self, client_id):
args = clients_file.parse() args = request.get_json()
client = clients_file.find(client_id) errors = self.schema.validate(args)
if errors:
abort(400, message=str(errors))
client = self.clients_file.find(client_id)
if not client: if not client:
abort(404, message="No client with id: {}".format(client_id)) abort(404, message="No client with id: {}".format(client_id))
else: else:
client = clients_file.update(args) client = self.clients_file.update(args)
return client, 201 return client, 201
# ClientList # ClientList
class ClientList(Resource): class ClientList(Resource):
def __init__(self):
self.schema = ClientSchema()
self.clients_file = ClientsFile()
def get(self): def get(self):
return { return {
client_id: (client, client.pop("secret", None))[0] client_id: (client, client.pop("secret", None))[0]
for client_id, client in clients_file.get().items() for client_id, client in self.clients_file.get().items()
} }
def post(self): def post(self):
args = clients_file.parse() args = request.get_json()
errors = self.schema.validate(args)
if errors:
abort(400, message=str(errors))
client = clients_file.find(args["client_id"]) client = clients_file.find(args["client_id"])
if client: if client:
abort(403, message="Duplicate client id: {}".format(client_id)) abort(403, message="Duplicate client id: {}".format(client_id))
......
from flask_restful import Resource, reqparse, abort from flask_restful import Resource, request, abort
from marshmallow import Schema, fields from marshmallow import Schema, fields
import json import json
...@@ -19,7 +19,7 @@ class Notify(Resource): ...@@ -19,7 +19,7 @@ class Notify(Resource):
def post(self): def post(self):
errors = self.schema.validate(request.args) errors = self.schema.validate(request.args)
if errors: if errors:
abort(400, str(errors)) abort(400, message=str(errors))
messages = [] messages = []
allow = False allow = False
......
...@@ -22,7 +22,7 @@ class Receive(Resource): ...@@ -22,7 +22,7 @@ class Receive(Resource):
def get(self): def get(self):
errors = self.schema.validate(request.args) errors = self.schema.validate(request.args)
if errors: if errors:
abort(400, str(errors)) abort(400, message=str(errors))
messages = [] messages = []
allow = False allow = False
......
from flask_restful import Resource, reqparse, abort from flask_restful import Resource, request, abort
from marshmallow import Schema, fields from marshmallow import Schema, fields
import json import json
...@@ -20,7 +20,7 @@ class Send(Resource): ...@@ -20,7 +20,7 @@ class Send(Resource):
def post(self): def post(self):
errors = self.schema.validate(request.args) errors = self.schema.validate(request.args)
if errors: if errors:
abort(400, str(errors)) abort(400, message=str(errors))
messages = [] messages = []
allow = False allow = False
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment