clients.py 3.75 KB
Newer Older
1 2
from flask_restful import Resource, request, abort
from marshmallow import Schema, fields
3 4 5 6 7 8
import json
import os
import random
import string


9 10 11 12 13 14
class ClientSchema(Schema):
    client_id = fields.Str(required=True)
    client_name =  fields.Str(required=True)
    subscription = fields.Str(required=True)


15
class ClientsFile:
16
    file = "./data/clients.json"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    mtime = 0
    clients = {}
    parser = None

    def __init__(self):
        self.get()

    def get(self):
        try:
            mtime = os.path.getmtime(self.file)
            if mtime > self.mtime:
                with open(self.file, "r") as client_file:
                    self.clients = json.load(client_file)
        except FileNotFoundError as error:
            self.clients = {}
            self.save()

        return self.clients

    def find(self, client_id):
        self.get()
        if client_id in self.clients:
            client = self.clients[client_id]
        else:
            client = None
        return client

    def add(self, client):
45
        client['secret'] = self.secret()
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        self.clients[client["client_id"]] = client
        self.save()
        return client

    def remove(self, client):
        del self.clients[client["client_id"]]
        self.save()

    def update(self, client_updates):
        client = self.find(client_updates["client_id"])
        client.update(client_updates)
        self.clients[client["client_id"]] = client
        self.save()
        return client

    def save(self):
        try:
            with open(self.file, "w") as client_file:
                client_file.write(json.dumps(self.clients, indent=2))
                return True
        except OSError as error:
            print(str(error))
            return False

    def secret(self, chars=36):
        res = "".join(
            random.choices(
                string.ascii_lowercase + string.ascii_uppercase + string.digits, k=chars
            )
        )
        return str(res)

clients_file = ClientsFile()

# Client
class Client(Resource):
82 83 84 85 86
    clients_file = None
    def __init__(self): 
        self.schema = ClientSchema()
        self.clients_file = ClientsFile()

87
    def get(self, client_id):
88
        client = self.clients_file.find(client_id)
Dan Jones's avatar
Dan Jones committed
89
        del client['secret']
90 91 92 93 94
        if not client:
            abort(404, message="No client with id: {}".format(client_id))
        return client

    def delete(self, todo_id):
95
        client = self.clients_file.find(client_id)
96 97 98
        if not client:
            abort(404, message="No client with id: {}".format(client_id))
        else:
99
            self.clients_file.remove(client)
100 101 102
        return client, 204

    def put(self, client_id):
103 104 105 106 107 108
        args = request.get_json()
        errors = self.schema.validate(args)
        if errors: 
            abort(400, message=str(errors))
        
        client = self.clients_file.find(client_id)
109 110 111
        if not client:
            abort(404, message="No client with id: {}".format(client_id))
        else:
112
            client = self.clients_file.update(args)
113 114 115 116 117
        return client, 201


# ClientList
class ClientList(Resource):
118 119 120 121
    def __init__(self): 
        self.schema = ClientSchema()
        self.clients_file = ClientsFile()

122 123 124
    def get(self):
        return {
            client_id: (client, client.pop("secret", None))[0]
125
            for client_id, client in self.clients_file.get().items()
126 127 128
        }

    def post(self):
129 130 131 132 133 134
        args = request.get_json()

        errors = self.schema.validate(args)
        if errors: 
            abort(400, message=str(errors))
        
Dan Jones's avatar
Dan Jones committed
135
        client = self.clients_file.find(args["client_id"])
136 137 138
        if client:
            abort(403, message="Duplicate client id: {}".format(client_id))
        else:
Dan Jones's avatar
Dan Jones committed
139
            client = self.clients_file.add(args)
140
        return client, 201