-
Notifications
You must be signed in to change notification settings - Fork 0
Auxiliary endpoints v2 #20
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
77c025f
test
51c8810
t
5761446
test
f308ba0
test
af0bf28
test
f76ae28
test
3e44eba
test
e9b4ad4
test
9575112
test
837abd1
test
mateusz-twarog-deepsense 5c106ce
test
mateusz-twarog-deepsense 0d6ec9d
test
mateusz-twarog-deepsense 2957e62
test
mateusz-twarog-deepsense d4af01a
test
mateusz-twarog-deepsense 2e12e57
test
mateusz-twarog-deepsense 6b90db1
test
mateusz-twarog-deepsense 2390bcc
test
mateusz-twarog-deepsense be5c819
test
mateusz-twarog-deepsense 05487fe
Tests
mateusz-twarog-deepsense be33d03
Fix
mateusz-twarog-deepsense 78df19a
Docs and fixes
mateusz-twarog-deepsense 60456bb
Update documentation and version
mateusz-twarog-deepsense File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from http import HTTPMethod | ||
| import random | ||
| from typing import Annotated, Dict, List | ||
|
|
||
| from fastapi import Body, Path, Query | ||
|
|
||
| from racetrack_job_wrapper.endpoint_config import EndpointConfig | ||
|
|
||
|
|
||
| class Job: | ||
| def perform(self, x: float, y: float) -> float: | ||
| """ | ||
| Add numbers. | ||
| :param x: First element to add. | ||
| :param y: Second element to add. | ||
| :return: Sum of the numbers. | ||
| """ | ||
| return x + y | ||
|
|
||
| def auxiliary_endpoints_v2(self) -> List[EndpointConfig]: | ||
| """ | ||
| Dict of custom endpoint paths (besides "/perform") handled by Entrypoint methods. | ||
| EndpointConfig consists of a path to the endpoint, http method(POST or GET only), | ||
| handler function for the endpoint and optional parameters dict that is passed to FastAPI. | ||
| """ | ||
| return [ | ||
| EndpointConfig('/multiply/{path}', HTTPMethod.POST, self.multiply, other_options=dict(tags=["items"])), | ||
| EndpointConfig('/random', HTTPMethod.GET, self.random, other_options=dict(tags=["items"])), | ||
| ] | ||
|
|
||
| def multiply(self, body: Annotated[float, Body(examples=[1.2])], query: Annotated[float, Query(example=2.4)], path: Annotated[float, Path(example=234.21)]) -> float: | ||
| """ | ||
| Standard FastAPI methods of documenting and configuring parameters between body, query and path work for auxiliary endpoints. | ||
| """ | ||
| return body * query * path | ||
|
|
||
| def random(self, start: float, end: float) -> float: | ||
| """Return random number within a range""" | ||
| return random.uniform(start, end) | ||
|
|
||
| def docs_input_examples(self) -> Dict[str, Dict]: | ||
| """Return mapping of Job's endpoints to corresponding exemplary inputs.""" | ||
| return { | ||
| '/perform': { | ||
| 'x': 40, | ||
| 'y': 2, | ||
| } | ||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| name = 'racetrack_job_wrapper' | ||
| __version__ = "1.17.0" # should be in sync with pyproject.toml | ||
| __version__ = "1.18.0" # should be in sync with pyproject.toml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from dataclasses import dataclass, field | ||
| from http import HTTPMethod | ||
| from typing import Any, Callable, Dict | ||
|
|
||
|
|
||
| @dataclass | ||
| class EndpointConfig: | ||
| """Definition of a single HTTP endpoint. | ||
|
|
||
| Attributes: | ||
| path: HTTP path at which the endpoint is registered. | ||
| method: HTTP verb used to bind the handler - only GET and POST are supported at moment. | ||
| handler: Callable invoked when the endpoint is hit. | ||
| other_options: Additional FastAPI route options. | ||
| """ | ||
|
|
||
| path: str | ||
| method: HTTPMethod | ||
| handler: Callable | ||
| other_options: Dict[str, Any] = field(default_factory=dict) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| from http import HTTPMethod | ||
| from typing import Annotated, Callable, Dict, List | ||
|
|
||
| from fastapi import Body | ||
| from racetrack_job_wrapper.endpoint_config import EndpointConfig | ||
| from racetrack_job_wrapper.wrapper_api import create_api_app | ||
| from racetrack_job_wrapper.health import HealthState | ||
| from fastapi.testclient import TestClient | ||
|
|
||
|
|
||
| def test_auxiliary_endpoints_v2(): | ||
| class TestEntrypoint: | ||
| def perform(self, x: float, y: float) -> float: | ||
| """ | ||
| Add numbers. | ||
| :param x: First element to add. | ||
| :param y: Second element to add. | ||
| :return: Sum of the numbers. | ||
| """ | ||
| return x + y | ||
|
|
||
| def auxiliary_endpoints_v2(self) -> List[EndpointConfig]: | ||
| return [ | ||
| EndpointConfig('/explain', HTTPMethod.POST, self.explain), | ||
| EndpointConfig('/random', HTTPMethod.GET, self.random), | ||
| ] | ||
|
|
||
| def explain(self, x: Annotated[float, Body()], y: Annotated[float, Body()]) -> Dict[str, float]: | ||
| """ | ||
| Explain feature importance of a model result. | ||
| :param x: First element to add. | ||
| :param y: Second element to add. | ||
| :return: Dict of feature importance. | ||
| """ | ||
| result = self.perform(x, y) | ||
| return {'x_importance': x / result, 'y_importance': y / result} | ||
|
|
||
| def random(self) -> float: | ||
| """Return random number""" | ||
| return 4 # chosen by fair dice roll | ||
|
|
||
| def docs_input_examples(self) -> Dict[str, Dict]: | ||
| return { | ||
| '/perform': { | ||
| 'x': 40, | ||
| 'y': 2, | ||
| }, | ||
| '/explain': { | ||
| 'x': 1, | ||
| 'y': 2, | ||
| }, | ||
| '/random': {}, | ||
| } | ||
|
|
||
| entrypoint = TestEntrypoint() | ||
| fastapi_app = create_api_app(entrypoint, HealthState(live=True, ready=True)) | ||
|
|
||
| client = TestClient(fastapi_app) | ||
|
|
||
| response = client.post( | ||
| "/api/v1/perform", | ||
| json={"x": 40, "y": 2}, | ||
| ) | ||
| assert response.status_code == 200 | ||
| assert response.json() == 42 | ||
|
|
||
| response = client.post( | ||
| "/api/v1/explain", | ||
| json={"x": 2, "y": 8}, | ||
| ) | ||
| assert response.status_code == 200 | ||
| assert response.json() == {'x_importance': 0.2, 'y_importance': 0.8} | ||
|
|
||
| response = client.get( | ||
| "/api/v1/random", | ||
| ) | ||
| assert response.status_code == 200 | ||
| assert response.json() == 4 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.