Skip to content

Commit

Permalink
Merge pull request #120 from iKonoTelecomunicaciones/119-update-creat…
Browse files Browse the repository at this point in the history
…e-client-endpoint

feat(api): ✨ Updated create client endpoint
  • Loading branch information
egalvis39 committed Jun 4, 2024
2 parents 70dc8ea + 3c695cc commit e23f3f5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
25 changes: 21 additions & 4 deletions menuflow/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
log: Logger = getLogger("menuflow.api.client")


async def _create_client(user_id: UserID | None, data: Dict) -> web.Response:
async def _create_client(
data: Dict, *, user_id: Optional[UserID] = None, flow_id: Optional[int] = None
) -> MenuClient | web.Response:
homeserver = data.get("homeserver", None)
access_token = data.get("access_token", None)
device_id = data.get("device_id", None)
Expand All @@ -47,7 +49,11 @@ async def _create_client(user_id: UserID | None, data: Dict) -> web.Response:
elif whoami.device_id and device_id and whoami.device_id != device_id:
return resp.device_id_mismatch(whoami.device_id)
client: MenuClient = await MenuClient.get(
whoami.user_id, homeserver=homeserver, access_token=access_token, device_id=device_id
whoami.user_id,
homeserver=homeserver,
access_token=access_token,
device_id=device_id,
flow_id=flow_id,
)
client.enabled = data.get("enabled", True)
client.autojoin = data.get("autojoin", True)
Expand All @@ -72,7 +78,14 @@ async def create_client(request: web.Request) -> web.Response:
data = await request.json()
except JSONDecodeError:
return resp.body_not_json
return await _create_client(None, data)

new_flow_id = None
if MenuClient.menuflow.config["menuflow.load_flow_from"] == "database":
example_flow = {"menu": {"flow_variables": {}, "nodes": []}}
new_flow = DBFlow(flow=example_flow)
new_flow_id = await new_flow.insert()

return await _create_client(data, flow_id=new_flow_id)


@routes.post("/room/{room_id}/set_variables")
Expand Down Expand Up @@ -119,7 +132,7 @@ async def create_flow(request: web.Request) -> web.Response:
await new_flow.insert()
message = "Flow created successfully"

return resp.ok({"error": message})
return resp.ok({"detail": {"message": message}})


@routes.get("/flow")
Expand All @@ -128,9 +141,13 @@ async def get_flow(request: web.Request) -> web.Response:
client_mxid = request.query.get("client_mxid", None)
if flow_id:
flow = await DBFlow.get_by_id(int(flow_id))
if not flow:
return resp.not_found(f"Flow with ID {flow_id} not found")
data = flow.serialize()
elif client_mxid:
flow = await DBFlow.get_by_mxid(client_mxid)
if not flow:
return resp.not_found(f"Flow with mxid {client_mxid} not found")
data = flow.serialize()
else:
flows = await DBFlow.all()
Expand Down
60 changes: 25 additions & 35 deletions menuflow/api/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,53 @@ class _Response:
@property
def body_not_json(self) -> web.Response:
return web.json_response(
{
"error": "Request body is not JSON",
"errcode": "body_not_json",
},
{"detail": {"message": "Request body is not JSON"}},
status=HTTPStatus.BAD_REQUEST,
)

@property
def bad_client_access_token(self) -> web.Response:
return web.json_response(
{
"error": "Invalid access token",
"errcode": "bad_client_access_token",
},
{"detail": {"message": "Invalid access token"}},
status=HTTPStatus.BAD_REQUEST,
)

@property
def bad_client_access_details(self) -> web.Response:
return web.json_response(
{
"error": "Invalid homeserver or access token",
"errcode": "bad_client_access_details",
},
{"detail": {"message": "Invalid homeserver or access token"}},
status=HTTPStatus.BAD_REQUEST,
)

@property
def bad_client_connection_details(self) -> web.Response:
return web.json_response(
{
"error": "Could not connect to homeserver",
"errcode": "bad_client_connection_details",
},
{"detail": {"message": "Could not connect to homeserver"}},
status=HTTPStatus.BAD_REQUEST,
)

def mxid_mismatch(self, found: str) -> web.Response:
return web.json_response(
{
"error": (
"The Matrix user ID of the client and the user ID of the access token don't "
f"match. Access token is for user {found}"
),
"errcode": "mxid_mismatch",
"detail": {
"message": f"""
The Matrix user ID of the client and the user ID of the access token don't
match. Access token is for user {found}
"""
}
},
status=HTTPStatus.BAD_REQUEST,
)

def device_id_mismatch(self, found: str) -> web.Response:
return web.json_response(
{
"error": (
"The Matrix device ID of the client and the device ID of the access token "
f"don't match. Access token is for device {found}"
),
"detail": {
"message": """
The Matrix device ID of the client and the device ID of the access token
don't match. Access token is for device {found}
"""
},
"errcode": "mxid_mismatch",
},
status=HTTPStatus.BAD_REQUEST,
Expand All @@ -74,10 +65,7 @@ def device_id_mismatch(self, found: str) -> web.Response:
@property
def user_exists(self) -> web.Response:
return web.json_response(
{
"error": "There is already a client with the user ID of that token",
"errcode": "user_exists",
},
{"detail": {"message": "There is already a client with the user ID of that token"}},
status=HTTPStatus.CONFLICT,
)

Expand All @@ -90,18 +78,20 @@ def created(data: dict) -> web.Response:

def bad_request(self, message: str) -> web.Response:
return web.json_response(
{
"error": message,
"errcode": "bad_request",
},
{"detail": {"message": message}},
status=HTTPStatus.BAD_REQUEST,
)

def client_not_found(self, user_id: str) -> web.Response:
return web.json_response(
{"detail": {"message": f"Client with user ID {user_id} not found"}},
status=HTTPStatus.NOT_FOUND,
)

def not_found(self, message: str) -> web.Response:
return web.json_response(
{
"error": f"Client with user ID {user_id} not found",
"errcode": "client_not_found",
"detail": {"message": message},
},
status=HTTPStatus.NOT_FOUND,
)
Expand Down
3 changes: 2 additions & 1 deletion menuflow/db/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ def _from_row(cls, row: Record) -> Union["Flow", None]:
def values(self) -> Dict[str, Any]:
return json.dumps(self.flow)

async def insert(self) -> str:
async def insert(self) -> int:
q = "INSERT INTO flow (flow) VALUES ($1)"
await self.db.execute(q, self.values)
return await self.db.fetchval("SELECT MAX(id) FROM flow")

async def update(self) -> None:
q = "UPDATE flow SET flow=$1 WHERE id=$2"
Expand Down
10 changes: 6 additions & 4 deletions menuflow/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import asyncio
import logging
from collections import defaultdict
from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, cast
from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Optional, cast

from aiohttp import ClientSession, TraceConfig
from mautrix.client import Client, InternalEventType
Expand Down Expand Up @@ -256,9 +256,10 @@ async def get(
cls,
user_id: UserID,
*,
homeserver: str | None = None,
access_token: str | None = None,
device_id: DeviceID | None = None,
homeserver: Optional[str] = None,
access_token: Optional[str] = None,
device_id: Optional[DeviceID] = None,
flow_id: Optional[int] = None,
) -> Client | None:
try:
return cls.cache[user_id]
Expand All @@ -276,6 +277,7 @@ async def get(
homeserver=homeserver,
access_token=access_token,
device_id=device_id or "",
flow=flow_id,
)
await user.insert()
await user.postinit()
Expand Down

0 comments on commit e23f3f5

Please sign in to comment.