-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_post.py
More file actions
43 lines (32 loc) · 1.04 KB
/
api_post.py
File metadata and controls
43 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
from pathlib import Path
import click
import requests
api_key_file = Path("/tmp/supersecret.txt")
@click.command()
@click.argument("message")
def cmd_api_client(message):
if not api_key_file.exists():
username = click.prompt("Username")
password = click.prompt("Password", hide_input=True)
r = requests.post(
"http://127.0.1.1:8080/api/key",
json={"username": username, "password": password},
)
if r.status_code != 200:
click.echo(
f"Invalid authentication or other error ocurred. Status code: {r.status_code}"
)
return False
api_key = r.json()["key"]
print("Received key:", api_key)
with api_key_file.open("w") as outfile:
outfile.write(api_key)
api_key = api_key_file.open().read()
r = requests.post(
"http://127.0.1.1:8080/api/post",
json={"text": message},
headers={"X-APIKEY": api_key},
)
print(r.text)
if __name__ == "__main__":
cmd_api_client()