|
3 | 3 | import click |
4 | 4 | import requests |
5 | 5 |
|
6 | | -api_key_file = Path('/tmp/supersecret.txt') |
| 6 | +api_key_file = Path("/tmp/supersecret.txt") |
| 7 | + |
7 | 8 |
|
8 | 9 | @click.command() |
9 | | -@click.argument('message') |
| 10 | +@click.argument("message") |
10 | 11 | def cmd_api_client(message): |
11 | 12 | if not api_key_file.exists(): |
| 13 | + username = click.prompt("Username") |
| 14 | + password = click.prompt("Password", hide_input=True) |
12 | 15 |
|
13 | | - username = click.prompt('Username') |
14 | | - password = click.prompt('Password', hide_input=True) |
15 | | - |
16 | | - r = requests.post('http://127.0.1.1:5000/api/key', json={'username':username, 'password':password}) |
| 16 | + r = requests.post( |
| 17 | + "http://127.0.1.1:5000/api/key", |
| 18 | + json={"username": username, "password": password}, |
| 19 | + ) |
17 | 20 |
|
18 | 21 | if r.status_code != 200: |
19 | | - click.echo('Invalid authentication or other error ocurred. Status code: {}'.format(r.status_code)) |
| 22 | + click.echo( |
| 23 | + f"Invalid authentication or other error ocurred. Status code: {r.status_code}" |
| 24 | + ) |
20 | 25 | return False |
21 | 26 |
|
| 27 | + api_key = r.json()["key"] |
| 28 | + print("Received key:", api_key) |
22 | 29 |
|
23 | | - api_key = r.json()['key'] |
24 | | - print('Received key:', api_key) |
25 | | - |
26 | | - with api_key_file.open('w') as outfile: |
| 30 | + with api_key_file.open("w") as outfile: |
27 | 31 | outfile.write(api_key) |
28 | 32 |
|
29 | 33 | api_key = api_key_file.open().read() |
30 | | - r = requests.post('http://127.0.1.1:5000/api/post', json={'text':message}, headers={'X-APIKEY': api_key}) |
| 34 | + r = requests.post( |
| 35 | + "http://127.0.1.1:5000/api/post", |
| 36 | + json={"text": message}, |
| 37 | + headers={"X-APIKEY": api_key}, |
| 38 | + ) |
31 | 39 | print(r.text) |
32 | 40 |
|
33 | 41 |
|
34 | | -if __name__ == '__main__': |
| 42 | +if __name__ == "__main__": |
35 | 43 | cmd_api_client() |
0 commit comments