Sketch of command-line registration script

main
Mike Lynch 2024-12-14 16:30:24 +11:00
parent b772f8aeaf
commit d1633d8040
2 changed files with 33 additions and 14 deletions

View File

@ -11,6 +11,9 @@ dependencies = [
"requests>=2.32.3",
]
[project.scripts]
register = "gotosocial.register:cli"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

View File

@ -1,6 +1,9 @@
# script to register an API
import requests
from pathlib import Path
import json
import argparse
class Application:
@ -10,6 +13,11 @@ class Application:
self.headers = {"Content-Type": "application/json"}
def register_app(self):
app_json = Path(f"./{self.name}_app.json")
if Path.isfile():
print(f"Found {app_json} file")
print(f"Looks like you've already registered {self.name}")
return False
response = requests.post(
f"{self.base_url}/api/v1/statuses",
data={
@ -19,19 +27,27 @@ class Application:
},
headers=self.headers,
)
response.raise_for_status()
json_r = response.json()
self.client_id = json_r["client_id"]
self.client_secret = json_r["client_secret"]
print(f"Registered {self.name} as {self.client_id}")
try:
response.raise_for_status()
json_r = response.json()
self.client_id = json_r["client_id"]
self.client_secret = json_r["client_secret"]
print(f"Registered {self.name}, client ID is {self.client_id}")
with open(app_json, "w") as jfh:
json.dump(json_r, jfh, indent=2)
print(f"Wrote response to {app_json}")
except Exception as e:
print("Something went wrong...")
print(e)
# curl \
# -X POST \
# -H 'Content-Type:application/json' \
# -d '{
# "client_name": "your_app_name",
# "redirect_uris": "urn:ietf:wg:oauth:2.0:oob",
# "scopes": "read"
# }' \
# 'https://example.org/api/v1/apps'
def cli():
ap = argparse.ArgumentParser()
ap.add_argument("-s", "--server", required=True, type=str, help="GoToSocial server")
ap.add_argument("-n", "--name", required=True, type=str, help="Application name")
ap.parse_args()
print(ap)
if __name__ == "__main__":
cli()