Skip to content

Commit

Permalink
Fixed issue with false except which always returned an error instea…
Browse files Browse the repository at this point in the history
…d 404
  • Loading branch information
Deutscher775 committed Jul 18, 2024
1 parent 51530bc commit 24b6937
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TMP
__pycache__
endpoints*
tokens*
<<<<<<< Updated upstream
tokens.json
config.py
website*
Expand All @@ -28,3 +29,18 @@ src/csdsc.py
src/Copy of Copy of api.py
src/Copy of api.py
src/Bot/config.py
=======
src/tokens.json
src/Bot/config.py
src/Bot/config.py
src/_astroidapi.log
/website
src/tokens.json
src/tokens.json
/web
website.zip
/website
src/Bot/nerimity_servers/1528027692992208896.json
>>>>>>> Stashed changes
src/Bot/config.py
src/tokens.json
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ fastapi
uvicorn
voltage=<0.1.5a8
re
surrealdb
aiohttp
34 changes: 34 additions & 0 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from typing import Annotated
import astroidapi.endpoint_update_handler
import astroidapi.errors
<<<<<<< Updated upstream
=======
import astroidapi.health_check
>>>>>>> Stashed changes
import astroidapi.read_handler
import astroidapi.surrealdb_handler
import beta_users
Expand Down Expand Up @@ -269,7 +273,11 @@ async def get_endpoint(endpoint: int,
if token == data_token or token == Bot.config.MASTER_TOKEN:
try:
return fastapi.responses.JSONResponse(status_code=200, content=await astroidapi.surrealdb_handler.get_endpoint(endpoint))
<<<<<<< Updated upstream
except astroidapi.errors.SurrealDBHandler.GetEndpointError.EndpointNotFoundError as e:
=======
except astroidapi.errors.SurrealDBHandler.EndpointNotFoundError as e:
>>>>>>> Stashed changes
return fastapi.responses.JSONResponse(status_code=404, content={"message": f"Endpoint {endpoint} not found."})
except astroidapi.errors.SurrealDBHandler.GetEndpointError as e:
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
Expand Down Expand Up @@ -422,8 +430,16 @@ async def post_endpoint(


@api.patch("/sync", description="Sync the local files with the database.")
<<<<<<< Updated upstream
async def sync_files():
await astroidapi.surrealdb_handler.sync_local_files(f"{pathlib.Path(__file__).parent.parent.resolve()}/src/endpoints")
=======
async def sync_files(endpoint: int = None):
if endpoint:
await astroidapi.surrealdb_handler.sync_local_files(f"{pathlib.Path(__file__).parent.resolve()}/endpoints/{endpoint}.json", True)
else:
await astroidapi.surrealdb_handler.sync_local_files(f"{pathlib.Path(__file__).parent.resolve()}/endpoints")
>>>>>>> Stashed changes
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Success."})


Expand Down Expand Up @@ -455,6 +471,24 @@ async def mark_read(endpoint: int,
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})


@api.get("/healthcheck/{endpoint}", description="Validate the endpoints strucuture.")
async def endpoint_healthcheck(endpoint: int, token: str):
if token == Bot.config.MASTER_TOKEN:
try:
healty = await astroidapi.health_check.HealthCheck.EndpointCheck.check(endpoint)
if healty:
return fastapi.responses.JSONResponse(status_code=200, content={"message": "This endpoint is healthy."})
else:
return fastapi.responses.JSONResponse(status_code=500, content={"message": "This endpoint is not healthy."})
except astroidapi.errors.HealtCheckError.EndpointCheckError as e:
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
except astroidapi.errors.SurrealDBHandler.EndpointNotFoundError:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This endpoint does not exist."})
except astroidapi.errors.SurrealDBHandler.GetEndpointError as e:
traceback.print_exc()
return fastapi.responses.JSONResponse(status_code=404, content={"message": f"An error occurred: {e}"})


@api.post("/create", description="Create an endpoint.",
response_description="Endpoints data.")
async def create_endpoint(endpoint: int):
Expand Down
10 changes: 9 additions & 1 deletion src/astroidapi/endpoint_update_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ async def update_endpoint(
finally:
if not updated_json["config"]["self-user"] is True:
if updated_json["meta"]["trigger"]:
<<<<<<< Updated upstream
await sending_handler.SendingHandler.distribute(endpoint)
=======
asyncio.create_task(sending_handler.SendingHandler.distribute(endpoint, updated_json))
>>>>>>> Stashed changes
print("Distributed")
waiting_secs = 0
max_secs = 10
Expand Down Expand Up @@ -341,6 +345,7 @@ async def update_endpoint(
except Exception as e:
logging.error("An error occurred: %s", e)
logging.error(traceback.format_exc())
<<<<<<< Updated upstream
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})


Expand Down Expand Up @@ -405,4 +410,7 @@ async def validate_endpoint(cls, endpoint: int, token: str = None):
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "You must provide a token."})
return fastapi.responses.JSONResponse(status_code=401, content={"message": "You must provide a token."})
=======
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
>>>>>>> Stashed changes
33 changes: 32 additions & 1 deletion src/astroidapi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,19 @@ class GetEndpointError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
<<<<<<< Updated upstream

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

class EndpointNotFoundError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
>>>>>>> Stashed changes

class SyncLocalFilesError(Exception):
def __init__(self, message):
Expand All @@ -129,4 +137,27 @@ def __init__(self, message):
class AlreadyReadError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
<<<<<<< Updated upstream
super().__init__(self.message)
=======
super().__init__(self.message)



class HealtCheckError:

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

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

class EndpointMetaDataError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
>>>>>>> Stashed changes
69 changes: 69 additions & 0 deletions src/astroidapi/health_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import astroidapi.surrealdb_handler as surrealdb_handler
import asyncio
import astroidapi.errors as errors
import json


class HealthCheck:

class EndpointCheck:

@classmethod
async def check(cls, endpoint):
healthy_endpoint_data = {
"config": {
"self-user": False,
"webhooks": {
"discord": [],
"guilded": [],
"revolt": [],
"nerimity": []
},
"channels": {
"discord": [],
"guilded": [],
"revolt": [],
"nerimity": []
},
"logs": {
"discord": None,
"guilded": None,
"revolt": None,
"nerimity": None
},
"blacklist": [],
"allowed-ids": [],
"isbeta": False
},
"meta": {
"sender-channel": None,
"trigger": False,
"sender": None,
"read": {
"discord": False,
"guilded": False,
"revolt": False,
"nerimity": False
},
"message": {
"author": {
"name": None,
"avatar": None,
"id": None
},
"content": None,
"attachments": []
}
}
}
try:
endpoint_data = await surrealdb_handler.get_endpoint(endpoint)
for key in healthy_endpoint_data["config"].keys():
if key not in endpoint_data["config"]:
raise errors.HealtCheckError.EndpointCheckError.EndpointConfigError(f"'{key}' not found in endpoint '{endpoint}'")
for key in healthy_endpoint_data["meta"].keys():
if key not in endpoint_data["meta"]:
raise errors.HealtCheckError.EndpointCheckError.EndpointMetaDataError(f"'{key}' not found in endpoint '{endpoint}'")
return True
except IndexError as e:
raise errors.HealtCheckError.EndpointCheckError(e)
8 changes: 8 additions & 0 deletions src/astroidapi/read_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ class ReadHandler:
@classmethod
async def mark_read(cls, endpoint, platform):
try:
<<<<<<< Updated upstream
=======
print(f"Marking {platform} read")
>>>>>>> Stashed changes
endpoint_data = await surrealdb_handler.get_endpoint(endpoint)
if endpoint_data is None:
raise errors.SurrealDBHandler.GetEndpointError.EndpointNotFoundError(f"'{endpoint}' not found")
if await cls.check_read(endpoint, platform):
raise errors.ReadHandlerError.AlreadyReadError(f"'{endpoint}' already marked '{platform}' as read")
<<<<<<< Updated upstream
endpoint_data["meta"]["read"][platform] = True
asyncio.create_task(surrealdb_handler.update(endpoint, endpoint_data))
=======
await surrealdb_handler.mark_read(endpoint, platform)
>>>>>>> Stashed changes
return True
except errors.ReadHandlerError.AlreadyReadError as e:
return True
Expand Down
5 changes: 5 additions & 0 deletions src/astroidapi/sending_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ def __init__(self):
pass

@classmethod
<<<<<<< Updated upstream
async def distribute(cls, endpoint):
try:
updated_json = await surrealdb_handler.get_endpoint(endpoint)
=======
async def distribute(cls, endpoint, updated_json: dict):
try:
>>>>>>> Stashed changes
sender = updated_json["meta"]["sender"]

if sender == "guilded":
Expand Down
55 changes: 55 additions & 0 deletions src/astroidapi/surrealdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,48 @@
import json
from Bot import config
import astroidapi.errors as errors
<<<<<<< Updated upstream

async def sync_local_files(folderpath: str):
=======
import traceback

async def sync_local_files(folderpath: str, specific: bool = False):
>>>>>>> Stashed changes
try:
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE)
<<<<<<< Updated upstream
=======
if specific:
with open(f"{folderpath}", "r") as file:
data = json.load(file)
print(file.name.replace(".json", "").split("/")[-1])
try:
print("Updating")
await db.update(f'endpoints:`{file.name.replace(".json", "").split("/")[-1]}`', data)
except:
print("Creating")
await db.create(f'endpoints:`{file.name.replace(".json", "").split("/")[-1]}`', data)
return True
>>>>>>> Stashed changes
for file in os.listdir(folderpath):
with open(f"{folderpath}/{file}", "r") as file:
data = json.load(file)
print(file.name)
try:
<<<<<<< Updated upstream
await db.query(f'CREATE endpoints:`{file.name.replace(".json", "").split("/")[-1]}` CONTENT {data}')
except:
await db.query(f'UPDATE endpoints:`{file.name.replace(".json", "").split("/")[-1]}` CONTENT {data}')
=======
print("Updating")
await db.update(f'endpoints:`{file.name.replace(".json", "").split("/")[-1]}`', data)
except:
print("Creating")
await db.create(f'endpoints:`{file.name.replace(".json", "").split("/")[-1]}`', data)
>>>>>>> Stashed changes
return True
except Exception as e:
raise errors.SurrealDBHandler.SyncLocalFilesError(e)
Expand All @@ -28,6 +56,7 @@ async def get_endpoint(endpoint: int):
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE)
data = await db.query(f"SELECT * FROM endpoints:`{endpoint}`")
<<<<<<< Updated upstream
try:
data[0]["result"][0].pop("id") # Remove the id field from the data
return data[0]["result"][0]
Expand All @@ -36,6 +65,16 @@ async def get_endpoint(endpoint: int):
except IndexError as e:
raise errors.SurrealDBHandler.GetEndpointError.EndpointNotFoundError(e)
except Exception as e:
=======
if not data:
raise errors.SurrealDBHandler.EndpointNotFoundError(f"Endpints exists, but data was found for endpoint {endpoint}")
data[0]["result"][0].pop("id") # Remove the id field from the data
return data[0]["result"][0]
except IndexError as e:
raise errors.SurrealDBHandler.EndpointNotFoundError(e)
except Exception as e:
traceback.print_exc()
>>>>>>> Stashed changes
raise errors.SurrealDBHandler.GetEndpointError(f"[{endpoint}] {e}")

async def create(endpoint: int, data: dict):
Expand Down Expand Up @@ -69,3 +108,19 @@ async def delete(endpoint: int):
raise errors.SurrealDBHandler.DeleteEndpointError(e)


<<<<<<< Updated upstream
=======
async def mark_read(endpoint, platform):
try:
print(f"Marking {platform} read")
async with Surreal(config.SDB_URL) as db:
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE)
await db.query(f"UPDATE endpoints:`{endpoint}` SET meta.read.{platform} = true")
return True
except errors.ReadHandlerError.AlreadyReadError as e:
return True
except Exception as e:
raise errors.ReadHandlerError.MarkReadError(e)

>>>>>>> Stashed changes

0 comments on commit 24b6937

Please sign in to comment.