Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cli to test other (custom) endpoints #46

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 75 additions & 45 deletions encord_agents/cli/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,37 @@
"""

import os
import re
import sys

import requests
import rich
import typer
from rich.progress import Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
from rich.table import Table
from typer import Argument, Option, Typer
from typing_extensions import Annotated

from encord_agents import FrameData

app = Typer(
name="test",
help="Utility for testing agents",
rich_markup_mode="rich",
no_args_is_help=True,
)

EDITOR_URL_PARTS_REGEX = r"https:\/\/app.encord.com\/label_editor\/(?P<projectHash>.*?)\/(?P<dataHash>[\w\d]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12})(/(?P<frame>\d+))?\??"
frederik-encord marked this conversation as resolved.
Show resolved Hide resolved

@app.command(
"local",
short_help="Hit a localhost agents endpoint for testing",
)
def local(
target: Annotated[
str,
Argument(help="Name of the localhost endpoint to hit ('http://localhost/{target}')"),
],
url: Annotated[str, Argument(help="Url copy/pasted from label editor")],
port: Annotated[int, Option(help="Local host port to hit")] = 8080,
) -> None:
"""Hit a localhost agents endpoint for testing an agent by copying the url from the Encord Label Editor over.

Given

- A url of the form [blue]`https://app.encord.com/label_editor/[green]{project_hash}[/green]/[green]{data_hash}[/green]/[green]{frame}[/green]`[/blue]
- A [green]target[/green] endpoint
- A [green]port[/green] (optional)

The url [blue]http://localhost:[green]{port}[/green]/[green]{target}[/green][/blue] will be hit with a post request containing:
{
"projectHash": "[green]{project_hash}[/green]",
"dataHash": "[green]{data_hash}[/green]",
"frame": [green]frame[/green] or 0
}
"""
import re
import sys
from pprint import pprint

import requests
import rich
import typer

parts_regex = r"https:\/\/app.encord.com\/label_editor\/(?P<projectHash>.*?)\/(?P<dataHash>[\w\d]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12})(/(?P<frame>\d+))?\??"

def parse_editor_url(editor_url: str) -> FrameData:
try:
match = re.match(parts_regex, url)
match = re.match(EDITOR_URL_PARTS_REGEX, editor_url)
if match is None:
raise typer.Abort()

payload = match.groupdict()
payload["frame"] = payload["frame"] or 0
return FrameData.model_validate(payload)
except Exception:
rich.print(
"""Could not match url to the expected format.
Expand All @@ -70,14 +43,13 @@ def local(
)
raise typer.Abort()

if target and not target[0] == "/":
target = f"/{target}"

def hit_endpoint(endpoint: str, payload: FrameData) -> None:
with requests.Session() as sess:
request = requests.Request(
"POST",
f"http://localhost:{port}{target}",
json=payload,
endpoint,
json=payload.model_dump(mode="json", by_alias=True),
headers={"Content-type": "application/json"},
)
prepped = request.prepare()
Expand Down Expand Up @@ -109,9 +81,7 @@ def local(

table.add_section()
table.add_row("[green]Utilities[/green]")
editor_url = (
f"https://app.encord.com/label_editor/{payload['projectHash']}/{payload['dataHash']}/{payload['frame']}"
)
editor_url = f"https://app.encord.com/label_editor/{payload.project_hash}/{payload.data_hash}/{payload.frame}"
frederik-encord marked this conversation as resolved.
Show resolved Hide resolved
table.add_row("label editor", editor_url)

headers = ["'{0}: {1}'".format(k, v) for k, v in prepped.headers.items()]
Expand All @@ -120,3 +90,63 @@ def local(
table.add_row("curl", curl_command)

rich.print(table)


@app.command("custom", short_help="Hit a custom endpoint for testing purposes")
def custom(
endpoint: Annotated[str, Argument(help="Endpoint to hit with json payload")],
editor_url: Annotated[str, Argument(help="Url copy/pasted from label editor")],
) -> None:
"""
Hit a custom agents endpoint for testing an editor agent by copying the url from the Encord Label Editor.

Given

- The endpoint you wish to test
- An editor url of the form [blue]`https://app.encord.com/label_editor/[green]{project_hash}[/green]/[green]{data_hash}[/green]/[green]{frame}[/green]`[/blue]
- A [green]port[/green] (optional)

The url [blue]http://localhost:[green]{port}[/green]/[green]{target}[/green][/blue] will be hit with a post request containing:
{
"projectHash": "[green]{project_hash}[/green]",
"dataHash": "[green]{data_hash}[/green]",
"frame": [green]frame[/green] or 0
}
"""
payload = parse_editor_url(editor_url)
hit_endpoint(endpoint, payload)


@app.command(
"local",
short_help="Hit a localhost agents endpoint for testing",
)
def local(
target: Annotated[
str,
Argument(help="Name of the localhost endpoint to hit ('http://localhost/{target}')"),
],
editor_url: Annotated[str, Argument(help="Url copy/pasted from label editor")],
port: Annotated[int, Option(help="Local host port to hit")] = 8080,
) -> None:
"""Hit a localhost agents endpoint for testing an agent by copying the url from the Encord Label Editor over.

Given

- An editor url of the form [blue]`https://app.encord.com/label_editor/[green]{project_hash}[/green]/[green]{data_hash}[/green]/[green]{frame}[/green]`[/blue]
- A [green]port[/green] (optional)

The url [blue]http://localhost:[green]{port}[/green]/[green]{target}[/green][/blue] will be hit with a post request containing:
{
"projectHash": "[green]{project_hash}[/green]",
"dataHash": "[green]{data_hash}[/green]",
"frame": [green]frame[/green] or 0
}
"""
payload = parse_editor_url(editor_url)

if target and not target[0] == "/":
target = f"/{target}"
endpoint = f"http://localhost:{port}{target}"

hit_endpoint(endpoint, payload)
4 changes: 2 additions & 2 deletions encord_agents/core/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class FrameData(BaseModel):
Holds the data sent from the Encord Label Editor at the time of triggering the agent.
"""

project_hash: UUID = Field(validation_alias="projectHash")
project_hash: UUID = Field(alias="projectHash")
"""
The identifier of the given project.
"""
data_hash: UUID = Field(validation_alias="dataHash")
data_hash: UUID = Field(alias="dataHash")
"""
The identifier of the given data asset.
"""
Expand Down
Loading