Skip to content

Commit d83710a

Browse files
authored
Add room type query param to room manager (#24)
* add room type query param * add cors, slightly refactor, test and update client
1 parent 7f2fba0 commit d83710a

File tree

18 files changed

+861
-11
lines changed

18 files changed

+861
-11
lines changed

examples/room_manager/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import logging
22

33
from flask import Flask
4+
from flask_cors import CORS
45

56
from .arguments import parse_arguments
67
from .room_service import RoomService
78
from .routes import setup_routes
89

910
app = Flask(__name__)
11+
CORS(app)
1012
app.logger.setLevel(logging.INFO)
1113

1214

examples/room_manager/room_service.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import betterproto
77

88
from fishjam import FishjamClient, PeerOptions, Room, RoomOptions
9+
from fishjam._openapi_client.models import RoomConfigRoomType
910
from fishjam.events import ServerMessagePeerCrashed as PeerCrashed
1011
from fishjam.events import ServerMessagePeerDeleted as PeerDeleted
1112
from fishjam.events import ServerMessageRoomCrashed as RoomCrashed
@@ -38,8 +39,13 @@ def __init__(self, args: Namespace, logger: Logger):
3839
self.logger = logger
3940
self.config = args
4041

41-
def get_peer_access(self, room_name: str, username: str) -> PeerAccess:
42-
room = self.__find_or_create_room(room_name)
42+
def get_peer_access(
43+
self,
44+
room_name: str,
45+
username: str,
46+
room_type: RoomConfigRoomType | None,
47+
) -> PeerAccess:
48+
room = self.__find_or_create_room(room_name, room_type)
4349
peer_access = self.peer_name_to_access.get(username)
4450
peer_in_room = self.__is_in_room(room, peer_access)
4551

@@ -63,7 +69,9 @@ def handle_notification(self, notification: betterproto.Message):
6369
case _:
6470
pass
6571

66-
def __find_or_create_room(self, room_name: str) -> Room:
72+
def __find_or_create_room(
73+
self, room_name: str, room_type: RoomConfigRoomType | None
74+
) -> Room:
6775
if room_name in self.room_name_to_room_id:
6876
self.logger.info("Room %s, already exists in the Fishjam", room_name)
6977

@@ -74,7 +82,9 @@ def __find_or_create_room(self, room_name: str) -> Room:
7482
max_peers=self.config.max_peers,
7583
webhook_url=self.config.webhook_url,
7684
peerless_purge_timeout=self.config.peerless_purge_timeout,
85+
room_type=room_type.value if room_type else "full_feature",
7786
)
87+
7888
new_room = self.fishjam_client.create_room(options=options)
7989

8090
self.room_name_to_room_id[room_name] = new_room.id

examples/room_manager/routes.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from flask import Flask, abort, jsonify, request
44

55
from fishjam import receive_binary
6+
from fishjam.room import RoomConfigRoomType
67

78
from .room_service import RoomService
89

@@ -15,7 +16,13 @@ def health_check():
1516

1617
@app.get("/api/rooms/<room_name>/users/<peer_name>")
1718
def get_room_params(room_name, peer_name):
18-
access_data = room_service.get_peer_access(room_name, peer_name)
19+
raw_room_type = request.args.get("roomType")
20+
try:
21+
room_type = RoomConfigRoomType(raw_room_type) if raw_room_type else None
22+
except ValueError:
23+
return abort(400)
24+
25+
access_data = room_service.get_peer_access(room_name, peer_name, room_type)
1926
response = asdict(access_data)
2027

2128
response["peerToken"] = response.pop("peer_token")
@@ -25,12 +32,18 @@ def get_room_params(room_name, peer_name):
2532
@app.get("/api/rooms")
2633
def get_room_query():
2734
room_name = request.args.get("roomName")
35+
raw_room_type = request.args.get("roomType")
2836
peer_name = request.args.get("peerName")
2937

3038
if not room_name or not peer_name:
3139
return abort(400)
3240

33-
access_data = room_service.get_peer_access(room_name, peer_name)
41+
try:
42+
room_type = RoomConfigRoomType(raw_room_type) if raw_room_type else None
43+
except ValueError:
44+
return abort(400)
45+
46+
access_data = room_service.get_peer_access(room_name, peer_name, room_type)
3447
response = asdict(access_data)
3548

3649
response["peerToken"] = response.pop("peer_token")

fishjam/_openapi_client/api/default/__init__.py

Whitespace-only changes.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
from http import HTTPStatus
2+
from typing import Any, Dict, Optional, Union, cast
3+
4+
import httpx
5+
6+
from ... import errors
7+
from ...client import AuthenticatedClient, Client
8+
from ...models.error import Error
9+
from ...types import Response
10+
11+
12+
def _get_kwargs() -> Dict[str, Any]:
13+
return {
14+
"method": "post",
15+
"url": "/admin/shutdown/drain",
16+
}
17+
18+
19+
def _parse_response(
20+
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
21+
) -> Optional[Union[Any, Error]]:
22+
if response.status_code == HTTPStatus.OK:
23+
response_200 = cast(Any, None)
24+
return response_200
25+
if response.status_code == HTTPStatus.UNAUTHORIZED:
26+
response_401 = Error.from_dict(response.json())
27+
28+
return response_401
29+
if client.raise_on_unexpected_status:
30+
raise errors.UnexpectedStatus(response.status_code, response.content)
31+
else:
32+
return None
33+
34+
35+
def _build_response(
36+
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
37+
) -> Response[Union[Any, Error]]:
38+
return Response(
39+
status_code=HTTPStatus(response.status_code),
40+
content=response.content,
41+
headers=response.headers,
42+
parsed=_parse_response(client=client, response=response),
43+
)
44+
45+
46+
def sync_detailed(
47+
*,
48+
client: AuthenticatedClient,
49+
) -> Response[Union[Any, Error]]:
50+
"""Marks node as draining, making it the last in the load balancing order.
51+
52+
Raises:
53+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
54+
httpx.TimeoutException: If the request takes longer than Client.timeout.
55+
56+
Returns:
57+
Response[Union[Any, Error]]
58+
"""
59+
60+
kwargs = _get_kwargs()
61+
62+
response = client.get_httpx_client().request(
63+
**kwargs,
64+
)
65+
66+
return _build_response(client=client, response=response)
67+
68+
69+
def sync(
70+
*,
71+
client: AuthenticatedClient,
72+
) -> Optional[Union[Any, Error]]:
73+
"""Marks node as draining, making it the last in the load balancing order.
74+
75+
Raises:
76+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
77+
httpx.TimeoutException: If the request takes longer than Client.timeout.
78+
79+
Returns:
80+
Union[Any, Error]
81+
"""
82+
83+
return sync_detailed(
84+
client=client,
85+
).parsed
86+
87+
88+
async def asyncio_detailed(
89+
*,
90+
client: AuthenticatedClient,
91+
) -> Response[Union[Any, Error]]:
92+
"""Marks node as draining, making it the last in the load balancing order.
93+
94+
Raises:
95+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
96+
httpx.TimeoutException: If the request takes longer than Client.timeout.
97+
98+
Returns:
99+
Response[Union[Any, Error]]
100+
"""
101+
102+
kwargs = _get_kwargs()
103+
104+
response = await client.get_async_httpx_client().request(**kwargs)
105+
106+
return _build_response(client=client, response=response)
107+
108+
109+
async def asyncio(
110+
*,
111+
client: AuthenticatedClient,
112+
) -> Optional[Union[Any, Error]]:
113+
"""Marks node as draining, making it the last in the load balancing order.
114+
115+
Raises:
116+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
117+
httpx.TimeoutException: If the request takes longer than Client.timeout.
118+
119+
Returns:
120+
Union[Any, Error]
121+
"""
122+
123+
return (
124+
await asyncio_detailed(
125+
client=client,
126+
)
127+
).parsed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from http import HTTPStatus
2+
from typing import Any, Dict, Optional, Union
3+
4+
import httpx
5+
6+
from ... import errors
7+
from ...client import AuthenticatedClient, Client
8+
from ...models.error import Error
9+
from ...models.shutdown_status_response import ShutdownStatusResponse
10+
from ...types import Response
11+
12+
13+
def _get_kwargs() -> Dict[str, Any]:
14+
return {
15+
"method": "get",
16+
"url": "/admin/shutdown/status",
17+
}
18+
19+
20+
def _parse_response(
21+
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
22+
) -> Optional[Union[Error, ShutdownStatusResponse]]:
23+
if response.status_code == HTTPStatus.OK:
24+
response_200 = ShutdownStatusResponse.from_dict(response.json())
25+
26+
return response_200
27+
if response.status_code == HTTPStatus.UNAUTHORIZED:
28+
response_401 = Error.from_dict(response.json())
29+
30+
return response_401
31+
if client.raise_on_unexpected_status:
32+
raise errors.UnexpectedStatus(response.status_code, response.content)
33+
else:
34+
return None
35+
36+
37+
def _build_response(
38+
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
39+
) -> Response[Union[Error, ShutdownStatusResponse]]:
40+
return Response(
41+
status_code=HTTPStatus(response.status_code),
42+
content=response.content,
43+
headers=response.headers,
44+
parsed=_parse_response(client=client, response=response),
45+
)
46+
47+
48+
def sync_detailed(
49+
*,
50+
client: AuthenticatedClient,
51+
) -> Response[Union[Error, ShutdownStatusResponse]]:
52+
"""Returns status information for the shutdown process of Fishjam.
53+
54+
Raises:
55+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
56+
httpx.TimeoutException: If the request takes longer than Client.timeout.
57+
58+
Returns:
59+
Response[Union[Error, ShutdownStatusResponse]]
60+
"""
61+
62+
kwargs = _get_kwargs()
63+
64+
response = client.get_httpx_client().request(
65+
**kwargs,
66+
)
67+
68+
return _build_response(client=client, response=response)
69+
70+
71+
def sync(
72+
*,
73+
client: AuthenticatedClient,
74+
) -> Optional[Union[Error, ShutdownStatusResponse]]:
75+
"""Returns status information for the shutdown process of Fishjam.
76+
77+
Raises:
78+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
79+
httpx.TimeoutException: If the request takes longer than Client.timeout.
80+
81+
Returns:
82+
Union[Error, ShutdownStatusResponse]
83+
"""
84+
85+
return sync_detailed(
86+
client=client,
87+
).parsed
88+
89+
90+
async def asyncio_detailed(
91+
*,
92+
client: AuthenticatedClient,
93+
) -> Response[Union[Error, ShutdownStatusResponse]]:
94+
"""Returns status information for the shutdown process of Fishjam.
95+
96+
Raises:
97+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
98+
httpx.TimeoutException: If the request takes longer than Client.timeout.
99+
100+
Returns:
101+
Response[Union[Error, ShutdownStatusResponse]]
102+
"""
103+
104+
kwargs = _get_kwargs()
105+
106+
response = await client.get_async_httpx_client().request(**kwargs)
107+
108+
return _build_response(client=client, response=response)
109+
110+
111+
async def asyncio(
112+
*,
113+
client: AuthenticatedClient,
114+
) -> Optional[Union[Error, ShutdownStatusResponse]]:
115+
"""Returns status information for the shutdown process of Fishjam.
116+
117+
Raises:
118+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
119+
httpx.TimeoutException: If the request takes longer than Client.timeout.
120+
121+
Returns:
122+
Union[Error, ShutdownStatusResponse]
123+
"""
124+
125+
return (
126+
await asyncio_detailed(
127+
client=client,
128+
)
129+
).parsed

fishjam/_openapi_client/api/health/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)