-
Notifications
You must be signed in to change notification settings - Fork 137
fix(client): resolve REST endpoint from discovery profile #90
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
Open
ynachiket
wants to merge
1
commit into
Universal-Commerce-Protocol:main
Choose a base branch
from
ynachiket:fix/endpoint-resolution-from-discovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Copyright 2026 UCP Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """UCP endpoint resolution utilities. | ||
|
|
||
| Implements the Endpoint Resolution logic from the UCP specification: | ||
| https://ucp.dev/2026-04-08/specification/overview#endpoint-resolution | ||
|
|
||
| "The endpoint field provides the base URL for API calls. OpenAPI paths | ||
| are appended to this endpoint to form the complete URL." | ||
| """ | ||
|
|
||
|
|
||
| def resolve_rest_endpoint(discovery_data: dict) -> str | None: | ||
| """Extract the REST endpoint from a UCP discovery response. | ||
|
|
||
| Looks up ``services["dev.ucp.shopping"]`` in the discovery payload, | ||
| finds the entry with ``transport == "rest"``, and returns its | ||
| ``endpoint`` value. All subsequent API paths (e.g. | ||
| ``/checkout-sessions``) should be appended to this base URL. | ||
|
|
||
| Args: | ||
| discovery_data: Parsed JSON body from ``GET /.well-known/ucp``. | ||
|
|
||
| Returns: | ||
| The resolved endpoint URL, or ``None`` if no REST service was found. | ||
|
|
||
| """ | ||
| shopping_services = discovery_data.get("services", {}).get( | ||
| "dev.ucp.shopping", [] | ||
| ) | ||
| rest_service = next( | ||
| (s for s in shopping_services if s.get("transport") == "rest"), None | ||
| ) | ||
| if rest_service and rest_service.get("endpoint"): | ||
| return rest_service["endpoint"] | ||
| return None |
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
176 changes: 176 additions & 0 deletions
176
rest/python/client/flower_shop/simple_happy_path_client_test.py
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,176 @@ | ||
| # Copyright 2026 UCP Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for endpoint resolution in the happy path client. | ||
|
|
||
| Verifies that resolve_rest_endpoint correctly extracts the REST endpoint | ||
| from a UCP discovery response, per the spec's Endpoint Resolution section: | ||
| https://ucp.dev/2026-04-08/specification/overview#endpoint-resolution | ||
| """ | ||
|
|
||
| import unittest | ||
|
|
||
| from endpoint_resolution import resolve_rest_endpoint | ||
|
|
||
|
|
||
| class ResolveRestEndpointTest(unittest.TestCase): | ||
| """Tests for resolve_rest_endpoint.""" | ||
|
|
||
| def test_returns_endpoint_with_path_prefix(self): | ||
| """Merchant mounts API under a path prefix (the bug scenario).""" | ||
| discovery = { | ||
| "services": { | ||
| "dev.ucp.shopping": [ | ||
| { | ||
| "version": "2026-04-08", | ||
| "transport": "rest", | ||
| "endpoint": "https://merchant.example.com/buy/v1", | ||
| "schema": "https://ucp.dev/2026-04-08/services/shopping/rest.openapi.json", | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| self.assertEqual( | ||
| resolve_rest_endpoint(discovery), | ||
| "https://merchant.example.com/buy/v1", | ||
| ) | ||
|
|
||
| def test_returns_endpoint_at_root(self): | ||
| """Merchant serves API at domain root (sample server scenario).""" | ||
| discovery = { | ||
| "services": { | ||
| "dev.ucp.shopping": [ | ||
| { | ||
| "version": "2026-04-08", | ||
| "transport": "rest", | ||
| "endpoint": "http://localhost:8182", | ||
| "schema": "https://ucp.dev/2026-04-08/services/shopping/rest.openapi.json", | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| self.assertEqual( | ||
| resolve_rest_endpoint(discovery), | ||
| "http://localhost:8182", | ||
| ) | ||
|
|
||
| def test_selects_rest_transport_among_multiple(self): | ||
| """Discovery lists multiple transports; only REST is selected.""" | ||
| discovery = { | ||
| "services": { | ||
| "dev.ucp.shopping": [ | ||
| { | ||
| "version": "2026-04-08", | ||
| "transport": "mcp", | ||
| "endpoint": "https://merchant.example.com/ucp/mcp", | ||
| "schema": "https://ucp.dev/2026-04-08/services/shopping/mcp.openrpc.json", | ||
| }, | ||
| { | ||
| "version": "2026-04-08", | ||
| "transport": "rest", | ||
| "endpoint": "https://merchant.example.com/api/v2", | ||
| "schema": "https://ucp.dev/2026-04-08/services/shopping/rest.openapi.json", | ||
| }, | ||
| { | ||
| "version": "2026-04-08", | ||
| "transport": "a2a", | ||
| "endpoint": "https://merchant.example.com/.well-known/agent-card.json", | ||
| }, | ||
| ] | ||
| } | ||
| } | ||
| self.assertEqual( | ||
| resolve_rest_endpoint(discovery), | ||
| "https://merchant.example.com/api/v2", | ||
| ) | ||
|
|
||
| def test_returns_none_when_no_services(self): | ||
| """Discovery response has no services key at all.""" | ||
| discovery = {"payment_handlers": {}} | ||
| self.assertIsNone(resolve_rest_endpoint(discovery)) | ||
|
|
||
| def test_returns_none_when_services_empty(self): | ||
| """Discovery response has empty services.""" | ||
| discovery = {"services": {}} | ||
| self.assertIsNone(resolve_rest_endpoint(discovery)) | ||
|
|
||
| def test_returns_none_when_no_rest_transport(self): | ||
| """Shopping service exists but only non-REST transports are listed.""" | ||
| discovery = { | ||
| "services": { | ||
| "dev.ucp.shopping": [ | ||
| { | ||
| "version": "2026-04-08", | ||
| "transport": "mcp", | ||
| "endpoint": "https://merchant.example.com/ucp/mcp", | ||
| "schema": "https://ucp.dev/2026-04-08/services/shopping/mcp.openrpc.json", | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| self.assertIsNone(resolve_rest_endpoint(discovery)) | ||
|
|
||
| def test_returns_none_when_rest_has_no_endpoint(self): | ||
| """REST transport exists but endpoint field is missing.""" | ||
| discovery = { | ||
| "services": { | ||
| "dev.ucp.shopping": [ | ||
| { | ||
| "version": "2026-04-08", | ||
| "transport": "rest", | ||
| "schema": "https://ucp.dev/2026-04-08/services/shopping/rest.openapi.json", | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| self.assertIsNone(resolve_rest_endpoint(discovery)) | ||
|
|
||
| def test_returns_none_when_endpoint_is_empty_string(self): | ||
| """REST transport exists but endpoint is an empty string.""" | ||
| discovery = { | ||
| "services": { | ||
| "dev.ucp.shopping": [ | ||
| { | ||
| "version": "2026-04-08", | ||
| "transport": "rest", | ||
| "endpoint": "", | ||
| "schema": "https://ucp.dev/2026-04-08/services/shopping/rest.openapi.json", | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| self.assertIsNone(resolve_rest_endpoint(discovery)) | ||
|
|
||
| def test_handles_deeply_nested_path_prefix(self): | ||
| """Endpoint has multiple path segments.""" | ||
| discovery = { | ||
| "services": { | ||
| "dev.ucp.shopping": [ | ||
| { | ||
| "version": "2026-04-08", | ||
| "transport": "rest", | ||
| "endpoint": "https://api.merchant.com/ucp/shopping/v1", | ||
| "schema": "https://ucp.dev/2026-04-08/services/shopping/rest.openapi.json", | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| self.assertEqual( | ||
| resolve_rest_endpoint(discovery), | ||
| "https://api.merchant.com/ucp/shopping/v1", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If api_base_url happens to have a trailing slash, this can result in double slashes in the logged curl command (e.g., https://example.com/api/v1//checkout-sessions), even though httpx resolves it correctly internally.
A minor improvement would be to use str(response.url) to get the actual resolved URL from the response object, instead of passing the constructed URL string.