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 os
import random
import string
class ClientSchema(Schema):
client_id = fields.Str(required=True)
client_name = fields.Str(required=True)
subscription = fields.Str(required=True)
class ClientsFile:
file = "clients.json"
mtime = 0
......@@ -13,7 +20,6 @@ class ClientsFile:
def __init__(self):
self.get()
self.setup_request_parser()
def get(self):
try:
......@@ -36,7 +42,7 @@ class ClientsFile:
return client
def add(self, client):
client.secret = self.secret()
client['secret'] = self.secret()
self.clients[client["client_id"]] = client
self.save()
return client
......@@ -69,70 +75,63 @@ class ClientsFile:
)
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()
# Client
class Client(Resource):
clients_file = None
def __init__(self):
self.schema = ClientSchema()
self.clients_file = ClientsFile()
def get(self, client_id):
client = clients_file.find(client_id)
client = self.clients_file.find(client_id)
del client['secret']
if not client:
abort(404, message="No client with id: {}".format(client_id))
return client
def delete(self, todo_id):
client = clients_file.find(client_id)
client = self.clients_file.find(client_id)
if not client:
abort(404, message="No client with id: {}".format(client_id))
else:
clients_file.remove(client)
self.clients_file.remove(client)
return client, 204
def put(self, client_id):
args = clients_file.parse()
client = clients_file.find(client_id)
args = request.get_json()
errors = self.schema.validate(args)
if errors:
abort(400, message=str(errors))
client = self.clients_file.find(client_id)
if not client:
abort(404, message="No client with id: {}".format(client_id))
else:
client = clients_file.update(args)
client = self.clients_file.update(args)
return client, 201
# ClientList
class ClientList(Resource):
def __init__(self):
self.schema = ClientSchema()
self.clients_file = ClientsFile()
def get(self):
return {
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):
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"])
if client:
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
import json
......@@ -19,7 +19,7 @@ class Notify(Resource):
def post(self):
errors = self.schema.validate(request.args)
if errors:
abort(400, str(errors))
abort(400, message=str(errors))
messages = []
allow = False
......
......@@ -22,7 +22,7 @@ class Receive(Resource):
def get(self):
errors = self.schema.validate(request.args)
if errors:
abort(400, str(errors))
abort(400, message=str(errors))
messages = []
allow = False
......
from flask_restful import Resource, reqparse, abort
from flask_restful import Resource, request, abort
from marshmallow import Schema, fields
import json
......@@ -20,7 +20,7 @@ class Send(Resource):
def post(self):
errors = self.schema.validate(request.args)
if errors:
abort(400, str(errors))
abort(400, message=str(errors))
messages = []
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