Skip to content

Commit

Permalink
stream_settings are now optional in Inbound model
Browse files Browse the repository at this point in the history
* Make stream_settings optional in Inbound model

* Add type ignore comment for stream_settings in Inbound model

* Handle stream_settings as either StreamSettings or str in Inbound model

* Update tests to allow stream_settings to be either StreamSettings or str

* Fixed for pylint

* Fixed comment
  • Loading branch information
F1bos authored Nov 19, 2024
1 parent 2a6b149 commit 0efbf74
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
17 changes: 12 additions & 5 deletions py3xui/inbound/inbound.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Inbound(BaseModel):
port (int): The port number for the inbound connection. Required.
protocol (str): The protocol for the inbound connection. Required.
settings (Settings): The settings for the inbound connection. Required.
stream_settings (StreamSettings): The stream settings for the inbound connection. Required.
stream_settings (StreamSettings): The stream settings for the inbound connection. Optional.
sniffing (Sniffing): The sniffing settings for the inbound connection. Required.
listen (str): The listen address for the inbound connection. Optional.
remark (str): The remark for the inbound connection. Optional.
Expand All @@ -59,7 +59,9 @@ class Inbound(BaseModel):
port: int
protocol: str
settings: Settings
stream_settings: StreamSettings = Field(alias=InboundFields.STREAM_SETTINGS) # type: ignore
stream_settings: StreamSettings | str = Field( # type: ignore
default="", alias=InboundFields.STREAM_SETTINGS
)
sniffing: Sniffing

listen: str = ""
Expand Down Expand Up @@ -103,11 +105,16 @@ def to_json(self) -> dict[str, Any]:
result.update(
{
InboundFields.SETTINGS: self.settings.model_dump_json(by_alias=True),
InboundFields.STREAM_SETTINGS: self.stream_settings.model_dump_json( # pylint: disable=no-member
by_alias=True
),
InboundFields.SNIFFING: self.sniffing.model_dump_json(by_alias=True),
}
)

# Handle stream_settings which can be either StreamSettings or str
if isinstance(self.stream_settings, StreamSettings):
result[InboundFields.STREAM_SETTINGS] = self.stream_settings.model_dump_json(
by_alias=True
)
else:
result[InboundFields.STREAM_SETTINGS] = self.stream_settings

return result
5 changes: 3 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def test_get_inbounds():
inbound = inbounds[0]
assert isinstance(inbound, Inbound), f"Expected Inbound, got {type(inbound)}"
assert isinstance(
inbound.stream_settings, StreamSettings
), f"Expected StreamSettings, got {type(inbound.stream_settings)}"
inbound.stream_settings, (StreamSettings, str)
), f"Expected StreamSettings or str, got {type(inbound.stream_settings)}"

assert isinstance(
inbound.sniffing, Sniffing
), f"Expected Sniffing, got {type(inbound.sniffing)}"
Expand Down
5 changes: 3 additions & 2 deletions tests/test_async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ async def test_get_inbounds():
inbound = inbounds[0]
assert isinstance(inbound, Inbound), f"Expected Inbound, got {type(inbound)}"
assert isinstance(
inbound.stream_settings, StreamSettings
), f"Expected StreamSettings, got {type(inbound.stream_settings)}"
inbound.stream_settings, (StreamSettings, str)
), f"Expected StreamSettings or str, got {type(inbound.stream_settings)}"

assert isinstance(
inbound.sniffing, Sniffing
), f"Expected Sniffing, got {type(inbound.sniffing)}"
Expand Down

0 comments on commit 0efbf74

Please sign in to comment.