fix(agent): expose network context
This commit is contained in:
@@ -20,6 +20,7 @@ class AgentAuthContextResponse(BaseModel):
|
||||
role: str
|
||||
is_superuser: bool
|
||||
project_id: str
|
||||
network: str
|
||||
project_role: str
|
||||
token_expires_at: str | None = None
|
||||
|
||||
@@ -43,6 +44,7 @@ async def get_agent_auth_context(
|
||||
role=current_user.role,
|
||||
is_superuser=current_user.is_superuser,
|
||||
project_id=str(ctx.project_id),
|
||||
network=ctx.project_code,
|
||||
project_role=ctx.project_role,
|
||||
token_expires_at=token_expires_at,
|
||||
)
|
||||
|
||||
@@ -25,6 +25,7 @@ logger = logging.getLogger(__name__)
|
||||
@dataclass(frozen=True)
|
||||
class ProjectContext:
|
||||
project_id: UUID
|
||||
project_code: str
|
||||
user_id: UUID
|
||||
project_role: str
|
||||
|
||||
@@ -85,6 +86,7 @@ async def get_project_context(
|
||||
|
||||
return ProjectContext(
|
||||
project_id=project.id,
|
||||
project_code=project.code,
|
||||
user_id=user.id,
|
||||
project_role=membership_role,
|
||||
)
|
||||
|
||||
+16
-2
@@ -404,6 +404,14 @@ def _parse_response_body(response: requests.Response) -> Any:
|
||||
return {}
|
||||
|
||||
|
||||
def _with_network_param(params: dict[str, Any] | None, network: str) -> dict[str, Any]:
|
||||
params = dict(params or {})
|
||||
if "network" in params or "network_name" in params or "name" in params:
|
||||
return params
|
||||
params["network"] = network
|
||||
return params
|
||||
|
||||
|
||||
def request_json(
|
||||
ctx: RuntimeContext,
|
||||
*,
|
||||
@@ -417,10 +425,13 @@ def request_json(
|
||||
require_username_ctx: bool = False,
|
||||
) -> tuple[Any, int]:
|
||||
require_server(ctx)
|
||||
network = None
|
||||
if require_network_ctx:
|
||||
require_network(ctx)
|
||||
network = require_network(ctx)
|
||||
if require_username_ctx:
|
||||
require_username(ctx)
|
||||
if network and (params is not None or json_body is None):
|
||||
params = _with_network_param(params, network)
|
||||
|
||||
url = f"{require_server(ctx)}/api/v1{path}"
|
||||
headers = build_headers(ctx, require_auth=require_auth, require_project=require_project)
|
||||
@@ -474,8 +485,11 @@ def request_bytes(
|
||||
require_network_ctx: bool = False,
|
||||
) -> tuple[bytes, int]:
|
||||
require_server(ctx)
|
||||
network = None
|
||||
if require_network_ctx:
|
||||
require_network(ctx)
|
||||
network = require_network(ctx)
|
||||
if network:
|
||||
params = _with_network_param(params, network)
|
||||
|
||||
url = f"{require_server(ctx)}/api/v1{path}"
|
||||
headers = build_headers(ctx, require_auth=require_auth, require_project=require_project)
|
||||
|
||||
@@ -28,6 +28,7 @@ def test_agent_auth_context_returns_metadata_user_and_project_context():
|
||||
client = _build_client(
|
||||
project_context=ProjectContext(
|
||||
project_id=project_id,
|
||||
project_code="fengyang",
|
||||
user_id=user_id,
|
||||
project_role="editor",
|
||||
),
|
||||
@@ -50,6 +51,7 @@ def test_agent_auth_context_returns_metadata_user_and_project_context():
|
||||
"role": "user",
|
||||
"is_superuser": False,
|
||||
"project_id": str(project_id),
|
||||
"network": "fengyang",
|
||||
"project_role": "editor",
|
||||
"token_expires_at": "2026-06-11T13:10:00+00:00",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user