-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add DestinationHttpClient to simplify calls to target systems #119
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
11 commits
Select commit
Hold shift + click to select a range
bc12525
feat: add get_erp_headers to Destination model
jplbrun 23315b1
feat: add DestinationHttpClient to simplify calls to target systems
jplbrun c520e3a
refactor: remove extra requests methods from DestinationHttpClient
jplbrun 449e2a5
feat: add get_headers to Destination with URL.headers.* support
jplbrun 126cf06
chore: deprecate get_instance_destination and get_subaccount_destinat…
jplbrun 175f177
docs: add calling target systems section and deprecation notices to d…
jplbrun 0d579d6
refactor(destination): remove unnecessary type validation and unused …
jplbrun a311c4a
test(destination): add DestinationHttpClient integration test
jplbrun c77e090
chore: bump version to v0.21.0
jplbrun d4f0915
fix(destination): mark deprecated methods with deprecated=True in rec…
jplbrun 2e3a031
Merge branch 'main' into feat/special-header-providers
jplbrun 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """HTTP client for calling the target system described by a Destination.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, Dict, Optional | ||
|
|
||
| import requests | ||
| from requests import Response | ||
|
|
||
| from sap_cloud_sdk.destination._models import Destination, DestinationType | ||
|
|
||
|
|
||
| class DestinationHttpClient: | ||
| """Wraps requests.Session to call the target system described by a Destination. | ||
|
|
||
| Pre-bakes headers derived from the destination — ERP headers (sap-client, | ||
| sap-language), URL.headers.* properties, and auth tokens. | ||
|
|
||
| Usage: | ||
|
|
||
| dest = client.get_destination("my-erp") | ||
| http = DestinationHttpClient(dest) | ||
| response = http.request("GET", "/api/resource") | ||
| """ | ||
|
|
||
| def __init__(self, destination: Destination) -> None: | ||
| if destination.type != DestinationType.HTTP: | ||
| raise ValueError( | ||
| f"DestinationHttpClient only supports HTTP destinations, got: {destination.type}" | ||
| ) | ||
|
|
||
| self._session = requests.Session() | ||
| self._session.headers.update(destination.get_headers()) | ||
|
NicoleMGomes marked this conversation as resolved.
|
||
| self._base_url = destination.url.rstrip("/") if destination.url else "" | ||
|
|
||
| def request( | ||
| self, | ||
| method: str, | ||
| path: str, | ||
| *, | ||
| params: Optional[Dict[str, Any]] = None, | ||
| json: Optional[Any] = None, | ||
| headers: Optional[Dict[str, str]] = None, | ||
| **kwargs: Any, | ||
| ) -> Response: | ||
| """Send an HTTP request to the target system. | ||
|
|
||
| Args: | ||
| method: HTTP verb (GET, POST, PUT, PATCH, DELETE). | ||
| path: Path relative to the destination URL. | ||
| params: Optional query parameters. | ||
| json: Optional JSON body. | ||
| headers: Optional additional headers merged on top of pre-baked ones. | ||
| **kwargs: Passed through to requests.Session.request. | ||
|
|
||
| Returns: | ||
| requests.Response from the target system. | ||
| """ | ||
| url = f"{self._base_url}/{path.lstrip('/')}" if path else self._base_url | ||
| return self._session.request( | ||
| method=method.upper(), | ||
| url=url, | ||
| params=params, | ||
| json=json, | ||
| headers=headers, | ||
| **kwargs, | ||
| ) | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.