Skip to content

Commit

Permalink
Update to surrealdb.py v0.4.1 and moved token to db
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Dec 29, 2024
1 parent ed24c87 commit fb29896
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 117 deletions.
Empty file removed src/.tokens.json
Empty file.
13 changes: 6 additions & 7 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def parameters():
def responses():
return json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/responses.json", "r"))


@api.get("/getserverstructure", description="Get a server structure.")
def get_server_structure(id: int, token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
global data_token
Expand Down Expand Up @@ -254,7 +253,7 @@ async def get_endpoint(endpoint: int,

global data_token
try:
data_token = json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/tokens.json", "r"))[f"{endpoint}"]
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
except:
data_token = None
pass
Expand Down Expand Up @@ -283,7 +282,7 @@ async def get_bridges(endpoint: int,
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
global data_token
try:
data_token = json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/tokens.json", "r"))[f"{endpoint}"]
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
except:
data_token = None
pass
Expand Down Expand Up @@ -423,7 +422,7 @@ async def post_endpoint(
if not token:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "You must provide a token."})
try:
data_token = json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/tokens.json", "r"))[f"{endpoint}"]
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
if token != data_token and token != Bot.config.MASTER_TOKEN:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
except KeyError:
Expand Down Expand Up @@ -631,7 +630,7 @@ async def create_endpoint(endpoint: int):
async def delete_endpoint(endpoint: int,
token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
try:
data_token = json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/tokens.json", "r"))[f"{endpoint}"]
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
if token is not None:
if token == data_token or token == Bot.config.MASTER_TOKEN:
try:
Expand Down Expand Up @@ -685,7 +684,7 @@ async def delete_enpoint_data(endpoint: int,
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})

data_token = json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/tokens.json", "r"))[f"{endpoint}"]
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
if token is not None:
if token == data_token or token == Bot.config.MASTER_TOKEN:
try:
Expand Down Expand Up @@ -866,4 +865,4 @@ async def add_contributor(id: int, username: str = None, avatar: str = None, tok

logging.info("[CORE] API started.")

uvicorn.run(api, host="localhost", port=9921)
uvicorn.run(api, host="localhost", port=9921, reload=False)
5 changes: 4 additions & 1 deletion src/astroidapi/endpoint_update_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,13 @@ async def update_endpoint(
updated_json = updated_json[0]["result"][0]
updated_json.pop("id")
except:
print("No result found.")
pass
finally:
if not updated_json["config"]["self-user"] is True:
# print("Updated endpoint data: ", updated_json)
if not updated_json["config"]["self-user"]:
if updated_json["meta"]["trigger"]:
print("Triggered.")
asyncio.create_task(queue_processor.QueueProcessor.handleUpdatedEndpointData(endpoint, updated_json))
else:
return fastapi.responses.JSONResponse(
Expand Down
20 changes: 20 additions & 0 deletions src/astroidapi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,26 @@ class GetMessageCacheError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class GetTokenError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class CreateTokenError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class DeleteTokenError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)

class UpdateTokenError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)


class ReadHandlerError:
Expand Down
Loading

0 comments on commit fb29896

Please sign in to comment.