refactor(api): namespaced SDK + dedicated transport layer
Testing / remote-protocol-compat (0.9.3) (push) Successful in 42s
Testing / remote-protocol-compat (0.9.5) (push) Successful in 44s
Package Extension / package-extension (push) Successful in 43s
Build & Publish Package / publish (push) Successful in 43s
Testing / test (push) Successful in 45s
Testing / remote-protocol-compat (0.9.3) (push) Successful in 42s
Testing / remote-protocol-compat (0.9.5) (push) Successful in 44s
Package Extension / package-extension (push) Successful in 43s
Build & Publish Package / publish (push) Successful in 43s
Testing / test (push) Successful in 45s
Restructure the Python API and internals around composable namespaces and a standalone transport/endpoint layer. Bump to 0.12.0. Python API: - Replace flat methods (b.tabs_list(), b.group_list()) with namespaces: b.nav, b.tabs, b.groups, b.windows, b.dom, b.extract, b.page, b.storage, b.cookies, b.session, b.perf, b.extension. - Shrink browser_cli/__init__.py to a thin composition root; move all behaviour into browser_cli/sdk/ (one module per namespace + factories, base, routing). Internals: - Add browser_cli/transport.py and remote_transport.py to isolate IPC from command logic; client.py now delegates instead of owning transport. - Add browser_cli/endpoints.py for endpoint resolution and browser_cli/errors.py for shared error types. - Extract markdown rendering into browser_cli/markdown.py (out of extract). - Add USER_AGENT to version_manager. Tooling & tests: - Add justfile with common dev tasks. - Update CLI commands and demo to the namespaced API. - Rework tests for the new layout; add test_transport.py and test_refactor_boundaries.py to lock in module boundaries. BREAKING CHANGE: flat API methods are removed in favour of namespaces (e.g. b.tabs_list() -> b.tabs.list(), b.group_list() -> b.groups.list()).
This commit is contained in:
@@ -1,23 +1,22 @@
|
||||
import click
|
||||
from browser_cli.commands import _handle
|
||||
from browser_cli.commands import client_from_ctx, handle_errors, tab_option
|
||||
from rich.console import Console
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
@click.group("nav")
|
||||
def nav_group():
|
||||
"""Navigate — open URLs, reload, go back/forward, focus tabs."""
|
||||
|
||||
|
||||
@nav_group.command("open")
|
||||
@click.argument("url")
|
||||
@click.option("--bg", is_flag=True, help="Open in background (no focus)")
|
||||
@click.option("--window", "window_name", default=None, help="Open in named window")
|
||||
@click.option("--group", "group_name", default=None, help="Open directly into a tab group (name or ID)")
|
||||
@handle_errors
|
||||
def cmd_open(url, bg, window_name, group_name):
|
||||
"""Open URL in a new tab."""
|
||||
_handle("navigate.open", {"url": url, "background": bg, "window": window_name, "group": group_name})
|
||||
client_from_ctx().nav.open(url, background=bg, window=window_name, group=group_name)
|
||||
suffix = ""
|
||||
if group_name:
|
||||
suffix = f" in group '{group_name}'"
|
||||
@@ -25,71 +24,67 @@ def cmd_open(url, bg, window_name, group_name):
|
||||
suffix = f" in window '{window_name}'"
|
||||
console.print(f"[green]Opened:[/green] {url}{suffix}")
|
||||
|
||||
|
||||
@nav_group.command("reload")
|
||||
@click.argument("tab_id", type=int, required=False)
|
||||
@handle_errors
|
||||
def cmd_reload(tab_id):
|
||||
"""Reload the active (or specified) tab."""
|
||||
_handle("navigate.reload", {"tabId": tab_id})
|
||||
client_from_ctx().nav.reload(tab_id)
|
||||
console.print("[green]Reloaded[/green]")
|
||||
|
||||
|
||||
@nav_group.command("hard-reload")
|
||||
@click.argument("tab_id", type=int, required=False)
|
||||
@handle_errors
|
||||
def cmd_hard_reload(tab_id):
|
||||
"""Hard reload (bypass cache) the active (or specified) tab."""
|
||||
_handle("navigate.hard_reload", {"tabId": tab_id})
|
||||
client_from_ctx().nav.hard_reload(tab_id)
|
||||
console.print("[green]Hard reloaded[/green]")
|
||||
|
||||
|
||||
@nav_group.command("back")
|
||||
@click.argument("tab_id", type=int, required=False)
|
||||
@handle_errors
|
||||
def cmd_back(tab_id):
|
||||
"""Navigate back in the active (or specified) tab."""
|
||||
_handle("navigate.back", {"tabId": tab_id})
|
||||
client_from_ctx().nav.back(tab_id)
|
||||
console.print("[green]Navigated back[/green]")
|
||||
|
||||
|
||||
@nav_group.command("forward")
|
||||
@click.argument("tab_id", type=int, required=False)
|
||||
@handle_errors
|
||||
def cmd_forward(tab_id):
|
||||
"""Navigate forward in the active (or specified) tab."""
|
||||
_handle("navigate.forward", {"tabId": tab_id})
|
||||
client_from_ctx().nav.forward(tab_id)
|
||||
console.print("[green]Navigated forward[/green]")
|
||||
|
||||
|
||||
@nav_group.command("focus")
|
||||
@click.argument("pattern")
|
||||
@handle_errors
|
||||
def cmd_focus(pattern):
|
||||
"""Jump to a tab by URL pattern or tab ID."""
|
||||
result = _handle("navigate.focus", {"pattern": pattern})
|
||||
result = client_from_ctx().nav.focus(pattern)
|
||||
if result:
|
||||
console.print(f"[green]Focused:[/green] {result.get('url', result)}")
|
||||
else:
|
||||
console.print(f"[yellow]No tab found matching:[/yellow] {pattern}")
|
||||
|
||||
|
||||
@nav_group.command("open-wait")
|
||||
@click.argument("url")
|
||||
@click.option("--timeout", type=float, default=30.0, show_default=True, help="Max seconds to wait for load")
|
||||
@click.option("--bg", is_flag=True, help="Open in background (no focus)")
|
||||
@click.option("--window", "window_name", default=None, help="Open in named window")
|
||||
@click.option("--group", "group_name", default=None, help="Open in tab group")
|
||||
@handle_errors
|
||||
def cmd_open_wait(url, timeout, bg, window_name, group_name):
|
||||
"""Open URL in a new tab and wait until fully loaded."""
|
||||
result = _handle("navigate.open_wait", {
|
||||
"url": url, "timeout": int(timeout * 1000),
|
||||
"background": bg, "window": window_name, "group": group_name,
|
||||
})
|
||||
title = result.get("title", "") if isinstance(result, dict) else ""
|
||||
console.print(f"[green]Loaded:[/green] {url}" + (f" — {title}" if title else ""))
|
||||
|
||||
tab = client_from_ctx().nav.open_wait(url, timeout=timeout, background=bg, window=window_name, group=group_name)
|
||||
console.print(f"[green]Loaded:[/green] {url}" + (f" — {tab.title}" if tab.title else ""))
|
||||
|
||||
@nav_group.command("wait")
|
||||
@click.option("--tab", "tab_id", type=int, default=None, help="Tab ID (default: active tab)")
|
||||
@tab_option
|
||||
@click.option("--timeout", type=float, default=30.0, show_default=True, help="Max seconds to wait")
|
||||
@click.option("--ready-state", type=click.Choice(["complete", "interactive"]), default="complete", show_default=True, help="Target ready state")
|
||||
@handle_errors
|
||||
def cmd_wait(tab_id, timeout, ready_state):
|
||||
"""Wait until tab finishes loading."""
|
||||
result = _handle("navigate.wait", {"tabId": tab_id, "timeout": int(timeout * 1000), "readyState": ready_state})
|
||||
console.print(f"[green]Ready:[/green] {result.get('url', '')} — {result.get('title', '')}")
|
||||
tab = client_from_ctx().tabs.wait_for_load(tab_id, timeout=timeout, ready_state=ready_state)
|
||||
console.print(f"[green]Ready:[/green] {tab.url} — {tab.title}")
|
||||
|
||||
Reference in New Issue
Block a user