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

feat: add client_create.py script

+ blacken changed files
+ add client_create.py to readme
+ don't create admin client automatically

Creating a default client the user hasn't asked for could
create problems with unused clients building up big queues
2 merge requests!23Resolve "Release v0.1.0",!15Resolve "Fix error running without any clients defined"
Pipeline #108042 passed with stages
in 2 minutes and 37 seconds
...@@ -124,7 +124,19 @@ For install and usage instructions for the adapters see the READMEs. ...@@ -124,7 +124,19 @@ For install and usage instructions for the adapters see the READMEs.
- Create an adapter - Create an adapter
- Test sending and receiving via the adapter - Test sending and receiving via the adapter
#### Create some clients #### Create some clients
##### With the script
```bash
python client_create.py
# will create a default 'admin' client subscribed to all messages (#)
# OR
python client_create.py --id=[client_id] --name="Your Client Name" --sub="something.something.#"
# will create a client with your preferred name, id and subscription
```
##### Through the API
`POST` to `http://localhost:8087/client` `POST` to `http://localhost:8087/client`
......
import argparse
import json
from models.client_model import ClientModel
parser = argparse.ArgumentParser(
prog="PROG", formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--id",
type=str,
default="admin",
help="Client IDs should be human readable kebab-case and unique",
)
parser.add_argument(
"--name",
type=str,
default="Admin Client",
help="Client names are how the client will appear on screen",
)
parser.add_argument(
"--sub",
type=str,
default="#",
help="A rabbitmq topic pattern for the client to subscribe to *=one-word-wildcard #=multi-word-wildcard",
)
parser.print_help()
args = parser.parse_args()
client_model = ClientModel()
client_model.add(
{
"client_id": args.id,
"client_name": args.name,
"subscription": args.sub,
}
)
client = client_model.find(args.id)
print("Here is your credentials file:")
print(json.dumps(client, indent=2))
...@@ -12,6 +12,7 @@ mtime of the file and reloads it if newer. This means that new clients ...@@ -12,6 +12,7 @@ mtime of the file and reloads it if newer. This means that new clients
should be returned should be returned
""" """
import json import json
import logging
import os import os
import random import random
import string import string
...@@ -33,14 +34,10 @@ class ClientModel: ...@@ -33,14 +34,10 @@ class ClientModel:
with open(self.file, "r") as client_file: with open(self.file, "r") as client_file:
self.clients = json.load(client_file) self.clients = json.load(client_file)
self.mtime = mtime self.mtime = mtime
except FileNotFoundError as error: except FileNotFoundError:
os.makedirs('data', exist_ok=True) logging.debug("First time initialisation with empty client list")
os.makedirs("data", exist_ok=True)
self.clients = {} self.clients = {}
self.add({
'client_id': 'admin',
'client_name': 'Default Admin Client',
'subscription': '#'
})
self.save() self.save()
self.mtime = os.path.getmtime(self.file) self.mtime = os.path.getmtime(self.file)
......
...@@ -185,7 +185,7 @@ if __name__ == "__main__": ...@@ -185,7 +185,7 @@ if __name__ == "__main__":
time.sleep(interval) time.sleep(interval)
pingcounter += 1 pingcounter += 1
s.close() s.close()
try: try:
with concurrent.futures.ProcessPoolExecutor() as executor: with concurrent.futures.ProcessPoolExecutor() as executor:
main(executor) main(executor)
......
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