salesforce-data-customcode 6.1.0.dev3__py3-none-any.whl → 6.1.0.dev5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- datacustomcode/__init__.py +5 -0
- datacustomcode/cli.py +30 -5
- datacustomcode/client.py +267 -192
- datacustomcode/config.py +5 -0
- datacustomcode/config.yaml +0 -6
- datacustomcode/constants.py +9 -1
- datacustomcode/deploy.py +58 -24
- datacustomcode/function/runtime.py +0 -16
- datacustomcode/io/reader/base.py +42 -0
- datacustomcode/io/writer/base.py +45 -0
- datacustomcode/io/writer/csv.py +8 -0
- datacustomcode/io/writer/print.py +7 -0
- datacustomcode/run.py +11 -7
- datacustomcode/scan.py +164 -29
- datacustomcode/template.py +13 -1
- datacustomcode/templates/script/examples/streaming_deltas/entrypoint.py +62 -0
- datacustomcode/templates/script/jupyterlab.sh +18 -4
- {salesforce_data_customcode-6.1.0.dev3.dist-info → salesforce_data_customcode-6.1.0.dev5.dist-info}/METADATA +42 -5
- {salesforce_data_customcode-6.1.0.dev3.dist-info → salesforce_data_customcode-6.1.0.dev5.dist-info}/RECORD +22 -44
- datacustomcode/named_credential/__init__.py +0 -28
- datacustomcode/named_credential/base.py +0 -54
- datacustomcode/named_credential/default.py +0 -93
- datacustomcode/named_credential/direct/__init__.py +0 -19
- datacustomcode/named_credential/direct/auth.py +0 -63
- datacustomcode/named_credential/direct/credentials.py +0 -121
- datacustomcode/named_credential/direct/transport.py +0 -110
- datacustomcode/named_credential/direct/url_resolver.py +0 -112
- datacustomcode/named_credential/errors.py +0 -36
- datacustomcode/named_credential/spark_base.py +0 -93
- datacustomcode/named_credential/spark_default.py +0 -154
- datacustomcode/named_credential/types/__init__.py +0 -14
- datacustomcode/named_credential/types/http_method.py +0 -29
- datacustomcode/named_credential/types/http_request.py +0 -63
- datacustomcode/named_credential/types/http_request_builder.py +0 -55
- datacustomcode/named_credential/types/http_response.py +0 -43
- datacustomcode/named_credential/types/http_response_builder.py +0 -24
- datacustomcode/named_credential_config.py +0 -105
- datacustomcode/templates/function/example/chunking_with_external_callout/README.md +0 -119
- datacustomcode/templates/function/example/chunking_with_external_callout/config.json +0 -3
- datacustomcode/templates/function/example/chunking_with_external_callout/entrypoint.py +0 -161
- datacustomcode/templates/function/example/chunking_with_external_callout/external_callout_config.json +0 -11
- datacustomcode/templates/function/example/chunking_with_external_callout/tests/test.json +0 -16
- {salesforce_data_customcode-6.1.0.dev3.dist-info → salesforce_data_customcode-6.1.0.dev5.dist-info}/WHEEL +0 -0
- {salesforce_data_customcode-6.1.0.dev3.dist-info → salesforce_data_customcode-6.1.0.dev5.dist-info}/entry_points.txt +0 -0
- {salesforce_data_customcode-6.1.0.dev3.dist-info → salesforce_data_customcode-6.1.0.dev5.dist-info}/licenses/LICENSE.txt +0 -0
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025, Salesforce, Inc.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
from __future__ import annotations
|
|
16
|
-
|
|
17
|
-
from abc import ABC, abstractmethod
|
|
18
|
-
from typing import TYPE_CHECKING, Optional
|
|
19
|
-
|
|
20
|
-
from datacustomcode.mixin import UserExtendableNamedConfigMixin
|
|
21
|
-
|
|
22
|
-
if TYPE_CHECKING:
|
|
23
|
-
from datacustomcode.named_credential.types.http_request import HTTPRequest
|
|
24
|
-
from datacustomcode.named_credential.types.http_response import HTTPResponse
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class NamedCredential(ABC, UserExtendableNamedConfigMixin):
|
|
28
|
-
CONFIG_NAME: str
|
|
29
|
-
|
|
30
|
-
def __init__(self, **kwargs):
|
|
31
|
-
pass
|
|
32
|
-
|
|
33
|
-
@abstractmethod
|
|
34
|
-
def request(
|
|
35
|
-
self,
|
|
36
|
-
request: HTTPRequest,
|
|
37
|
-
body: Optional[str] = None,
|
|
38
|
-
) -> HTTPResponse:
|
|
39
|
-
"""Make an external callout through a Named Credential.
|
|
40
|
-
|
|
41
|
-
The endpoint and its authentication are resolved server-side from the
|
|
42
|
-
Named Credential referenced by ``request.url``; the function never sees
|
|
43
|
-
the external credential.
|
|
44
|
-
|
|
45
|
-
Args:
|
|
46
|
-
request: The callout request
|
|
47
|
-
body: Optional request body. Set the
|
|
48
|
-
``Content-Type`` header to match the format; the SDK
|
|
49
|
-
does not assume or inject one.
|
|
50
|
-
|
|
51
|
-
Returns:
|
|
52
|
-
The external service's response.
|
|
53
|
-
"""
|
|
54
|
-
...
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025, Salesforce, Inc.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
|
|
16
|
-
from typing import (
|
|
17
|
-
Any,
|
|
18
|
-
Dict,
|
|
19
|
-
Optional,
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
from datacustomcode.named_credential.base import NamedCredential
|
|
23
|
-
from datacustomcode.named_credential.types.http_request import HTTPRequest
|
|
24
|
-
from datacustomcode.named_credential.types.http_response import HTTPResponse
|
|
25
|
-
from datacustomcode.named_credential.types.http_response_builder import (
|
|
26
|
-
HTTPResponseBuilder,
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
class DefaultNamedCredential(NamedCredential):
|
|
31
|
-
"""
|
|
32
|
-
Executes the callout directly via :class:`DirectCalloutTransport`, resolving
|
|
33
|
-
the URL from the Named Credential Connect API (falling back to
|
|
34
|
-
``external_callout_config.json``) and injecting auth from the same file.
|
|
35
|
-
"""
|
|
36
|
-
|
|
37
|
-
CONFIG_NAME = "DefaultNamedCredential"
|
|
38
|
-
|
|
39
|
-
def __init__(
|
|
40
|
-
self,
|
|
41
|
-
credentials_profile: str = "default",
|
|
42
|
-
sf_cli_org: Optional[str] = None,
|
|
43
|
-
credential_file: Optional[str] = None,
|
|
44
|
-
**kwargs: Any,
|
|
45
|
-
) -> None:
|
|
46
|
-
super().__init__(**kwargs)
|
|
47
|
-
self._credentials_profile = credentials_profile
|
|
48
|
-
self._sf_cli_org = sf_cli_org
|
|
49
|
-
self._credential_file = credential_file
|
|
50
|
-
self._transport: Optional[Any] = None
|
|
51
|
-
|
|
52
|
-
def request(
|
|
53
|
-
self,
|
|
54
|
-
request: HTTPRequest,
|
|
55
|
-
body: Optional[str] = None,
|
|
56
|
-
) -> HTTPResponse:
|
|
57
|
-
# Body and response are treated as opaque strings; the SDK makes no
|
|
58
|
-
# assumption about their format (JSON, XML, text, ...).
|
|
59
|
-
callout_request = {
|
|
60
|
-
"path": request.url,
|
|
61
|
-
"method": request.method,
|
|
62
|
-
"headers": dict(request.headers),
|
|
63
|
-
"body": body if body is not None else "",
|
|
64
|
-
}
|
|
65
|
-
callout_response = self._callout(callout_request)
|
|
66
|
-
return HTTPResponseBuilder.build(
|
|
67
|
-
{
|
|
68
|
-
"status_code": callout_response.get("status_code"),
|
|
69
|
-
"headers": callout_response.get("headers", {}),
|
|
70
|
-
"body": callout_response.get("body") or "",
|
|
71
|
-
}
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
def _callout(self, callout_request: Dict[str, Any]) -> Dict[str, Any]:
|
|
75
|
-
"""Execute the callout via the transport.
|
|
76
|
-
|
|
77
|
-
Returns a dict with ``status_code``, ``headers``, and ``body``.
|
|
78
|
-
"""
|
|
79
|
-
result: Dict[str, Any] = self._get_transport().callout(callout_request)
|
|
80
|
-
return result
|
|
81
|
-
|
|
82
|
-
def _get_transport(self) -> Any:
|
|
83
|
-
if self._transport is None:
|
|
84
|
-
from datacustomcode.named_credential.direct.transport import (
|
|
85
|
-
DirectCalloutTransport,
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
self._transport = DirectCalloutTransport(
|
|
89
|
-
credentials_profile=self._credentials_profile,
|
|
90
|
-
sf_cli_org=self._sf_cli_org,
|
|
91
|
-
credential_file=self._credential_file,
|
|
92
|
-
)
|
|
93
|
-
return self._transport
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025, Salesforce, Inc.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
"""Named Credential callout path.
|
|
16
|
-
|
|
17
|
-
This resolves the endpoint via the NamedCredential Connect API
|
|
18
|
-
and injects auth from a developer-provided ``external_callout_config.json``
|
|
19
|
-
"""
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025, Salesforce, Inc.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
"""
|
|
16
|
-
Inject external credential auth into an outgoing request.
|
|
17
|
-
"""
|
|
18
|
-
|
|
19
|
-
from __future__ import annotations
|
|
20
|
-
|
|
21
|
-
import base64
|
|
22
|
-
from typing import (
|
|
23
|
-
TYPE_CHECKING,
|
|
24
|
-
Any,
|
|
25
|
-
Dict,
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
from requests.auth import AuthBase
|
|
29
|
-
|
|
30
|
-
from datacustomcode.named_credential.direct.credentials import AuthType
|
|
31
|
-
|
|
32
|
-
if TYPE_CHECKING:
|
|
33
|
-
from requests.models import PreparedRequest
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
class DynamicAuthHandler(AuthBase):
|
|
37
|
-
def __init__(self, cred_config: Dict[str, Any]) -> None:
|
|
38
|
-
self.config = cred_config
|
|
39
|
-
self.auth_type = cred_config.get("auth_type")
|
|
40
|
-
|
|
41
|
-
def __call__(self, request: PreparedRequest) -> PreparedRequest:
|
|
42
|
-
if self.auth_type == AuthType.BASIC.value:
|
|
43
|
-
user = self.config.get("username", "")
|
|
44
|
-
pwd = self.config.get("password", "")
|
|
45
|
-
token = base64.b64encode(f"{user}:{pwd}".encode()).decode()
|
|
46
|
-
request.headers["Authorization"] = f"Basic {token}"
|
|
47
|
-
|
|
48
|
-
elif self.auth_type == AuthType.CUSTOM.value:
|
|
49
|
-
for name, value in self.config.get("custom_headers", {}).items():
|
|
50
|
-
request.headers[name] = value
|
|
51
|
-
|
|
52
|
-
elif self.auth_type in (AuthType.OAUTH.value, AuthType.JWT.value):
|
|
53
|
-
bearer = self.config.get("access_token") or self.config.get("token")
|
|
54
|
-
if not bearer:
|
|
55
|
-
raise ValueError(
|
|
56
|
-
f"'{self.auth_type}' auth requires an 'access_token' or 'token'."
|
|
57
|
-
)
|
|
58
|
-
request.headers["Authorization"] = f"Bearer {bearer}"
|
|
59
|
-
|
|
60
|
-
else:
|
|
61
|
-
raise ValueError(f"Unsupported auth_type '{self.auth_type}'.")
|
|
62
|
-
|
|
63
|
-
return request
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025, Salesforce, Inc.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
"""Load developer credentials for direct callouts from ``external_callout_config.json``.
|
|
16
|
-
|
|
17
|
-
The file lives in the parent of the payload folder so it is never packaged in
|
|
18
|
-
the deployment zip. Its ``credentials`` section is keyed by callout reference
|
|
19
|
-
(``callout:<NamedCredential>``); each value carries a mandatory ``auth_type``
|
|
20
|
-
and an optional ``target_url``.
|
|
21
|
-
"""
|
|
22
|
-
|
|
23
|
-
from __future__ import annotations
|
|
24
|
-
|
|
25
|
-
from enum import Enum
|
|
26
|
-
import json
|
|
27
|
-
import os
|
|
28
|
-
from pathlib import Path
|
|
29
|
-
from typing import (
|
|
30
|
-
Any,
|
|
31
|
-
Dict,
|
|
32
|
-
Optional,
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
# Default file name; discovered in the parent of the payload folder.
|
|
36
|
-
EXTERNAL_CALLOUT_CREDENTIAL = "external_callout_config.json"
|
|
37
|
-
# Absolute-path override, primarily for tests and non-standard layouts.
|
|
38
|
-
CREDENTIAL_FILE_ENV_VAR = "DATACUSTOMCODE_EXTERNAL_CALLOUT_CONFIG"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
class AuthType(str, Enum):
|
|
42
|
-
"""External Credential auth types supported by External Services."""
|
|
43
|
-
|
|
44
|
-
BASIC = "Basic"
|
|
45
|
-
CUSTOM = "Custom"
|
|
46
|
-
JWT = "Jwt"
|
|
47
|
-
OAUTH = "OAuth"
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
class CredentialError(RuntimeError):
|
|
51
|
-
"""Raised when credentials cannot be found or are invalid."""
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def _discover_credential_file() -> Optional[Path]:
|
|
55
|
-
"""Find the config file via env override, then by walking up from cwd."""
|
|
56
|
-
override = os.environ.get(CREDENTIAL_FILE_ENV_VAR)
|
|
57
|
-
if override:
|
|
58
|
-
return Path(override)
|
|
59
|
-
|
|
60
|
-
for directory in (Path.cwd(), *Path.cwd().parents):
|
|
61
|
-
candidate = directory / EXTERNAL_CALLOUT_CREDENTIAL
|
|
62
|
-
if candidate.is_file():
|
|
63
|
-
return candidate
|
|
64
|
-
return None
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
class CredentialStore:
|
|
68
|
-
"""Reads ``external_callout_config.json`` and returns per-callout config."""
|
|
69
|
-
|
|
70
|
-
def __init__(self, credential_file: Optional[str] = None) -> None:
|
|
71
|
-
self._explicit_path = Path(credential_file) if credential_file else None
|
|
72
|
-
self._credentials: Optional[Dict[str, Dict[str, Any]]] = None
|
|
73
|
-
|
|
74
|
-
def _load(self) -> Dict[str, Dict[str, Any]]:
|
|
75
|
-
if self._credentials is not None:
|
|
76
|
-
return self._credentials
|
|
77
|
-
|
|
78
|
-
path = self._explicit_path or _discover_credential_file()
|
|
79
|
-
if path is None or not path.is_file():
|
|
80
|
-
raise CredentialError(
|
|
81
|
-
f"Could not find '{EXTERNAL_CALLOUT_CREDENTIAL}'. Place it in the "
|
|
82
|
-
f"parent of your payload folder, or set "
|
|
83
|
-
f"${CREDENTIAL_FILE_ENV_VAR} to its path."
|
|
84
|
-
)
|
|
85
|
-
try:
|
|
86
|
-
with open(path, "r") as f:
|
|
87
|
-
data = json.load(f)
|
|
88
|
-
except (OSError, json.JSONDecodeError) as exc:
|
|
89
|
-
raise CredentialError(f"Failed to read '{path}': {exc}") from exc
|
|
90
|
-
|
|
91
|
-
# Per-callout entries live under the ``credentials`` section, leaving
|
|
92
|
-
# room for other config sections alongside them in the future.
|
|
93
|
-
credentials = data.get("credentials") if isinstance(data, dict) else None
|
|
94
|
-
if not isinstance(credentials, dict):
|
|
95
|
-
raise CredentialError(
|
|
96
|
-
f"'{path}' must be a JSON object with a 'credentials' section "
|
|
97
|
-
f"keyed by callout reference."
|
|
98
|
-
)
|
|
99
|
-
self._credentials = credentials
|
|
100
|
-
return credentials
|
|
101
|
-
|
|
102
|
-
def get(self, callout_key: str) -> Dict[str, Any]:
|
|
103
|
-
"""Return the config for a callout key (e.g. ``callout:AWS_S3_Service``).
|
|
104
|
-
|
|
105
|
-
Raises:
|
|
106
|
-
CredentialError: if the key is missing or has no ``auth_type``.
|
|
107
|
-
"""
|
|
108
|
-
credentials = self._load()
|
|
109
|
-
config = credentials.get(callout_key)
|
|
110
|
-
if config is None:
|
|
111
|
-
raise CredentialError(
|
|
112
|
-
f"No credential configuration found for '{callout_key}'. "
|
|
113
|
-
f"Add it to the 'credentials' section of "
|
|
114
|
-
f"'{EXTERNAL_CALLOUT_CREDENTIAL}'."
|
|
115
|
-
)
|
|
116
|
-
if not isinstance(config, dict) or not config.get("auth_type"):
|
|
117
|
-
raise CredentialError(
|
|
118
|
-
f"Credential for '{callout_key}' is missing the mandatory "
|
|
119
|
-
f"'auth_type' field."
|
|
120
|
-
)
|
|
121
|
-
return config
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025, Salesforce, Inc.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
"""Send a Named Credential callout over HTTP.
|
|
16
|
-
|
|
17
|
-
Resolves the ``callout:<NamedCredential>/<path>`` reference to a real endpoint
|
|
18
|
-
(via the Named Credential Connect API, falling back to ``target_url`` in
|
|
19
|
-
``external_callout_config.json``), attaches the credential's auth, and returns the raw
|
|
20
|
-
``{status_code, headers, body}`` response.
|
|
21
|
-
"""
|
|
22
|
-
|
|
23
|
-
from __future__ import annotations
|
|
24
|
-
|
|
25
|
-
from typing import (
|
|
26
|
-
Any,
|
|
27
|
-
Dict,
|
|
28
|
-
Optional,
|
|
29
|
-
)
|
|
30
|
-
|
|
31
|
-
import requests
|
|
32
|
-
|
|
33
|
-
from datacustomcode.named_credential.direct.auth import DynamicAuthHandler
|
|
34
|
-
from datacustomcode.named_credential.direct.credentials import CredentialStore
|
|
35
|
-
from datacustomcode.named_credential.direct.url_resolver import resolve_base_url
|
|
36
|
-
from datacustomcode.named_credential.errors import NamedCredentialCallError
|
|
37
|
-
from datacustomcode.token_provider import (
|
|
38
|
-
CredentialsTokenProvider,
|
|
39
|
-
SFCLITokenProvider,
|
|
40
|
-
TokenProvider,
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
class DirectCalloutTransport:
|
|
45
|
-
def __init__(
|
|
46
|
-
self,
|
|
47
|
-
credentials_profile: str = "default",
|
|
48
|
-
sf_cli_org: Optional[str] = None,
|
|
49
|
-
credential_file: Optional[str] = None,
|
|
50
|
-
) -> None:
|
|
51
|
-
self._store = CredentialStore(credential_file)
|
|
52
|
-
self._token_provider = self._build_token_provider(
|
|
53
|
-
credentials_profile, sf_cli_org
|
|
54
|
-
)
|
|
55
|
-
# Resolved base URL per callout key. Stable for the transport's life, so
|
|
56
|
-
# cache it to avoid a token fetch + Connect API call on every row
|
|
57
|
-
self._base_url_cache: Dict[str, str] = {}
|
|
58
|
-
|
|
59
|
-
@staticmethod
|
|
60
|
-
def _build_token_provider(
|
|
61
|
-
credentials_profile: str, sf_cli_org: Optional[str]
|
|
62
|
-
) -> TokenProvider:
|
|
63
|
-
if sf_cli_org:
|
|
64
|
-
return SFCLITokenProvider(sf_cli_org)
|
|
65
|
-
return CredentialsTokenProvider(credentials_profile)
|
|
66
|
-
|
|
67
|
-
def callout(self, callout_request: Dict[str, Any]) -> Dict[str, Any]:
|
|
68
|
-
raw_url = callout_request["path"]
|
|
69
|
-
if not raw_url.startswith("callout:"):
|
|
70
|
-
raise NamedCredentialCallError(
|
|
71
|
-
f"Callout URL must start with 'callout:', got '{raw_url}'."
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
# Split the named credential reference at the first '/' or '?'; the remainder
|
|
75
|
-
# path or query string is appended to the resolved base URL verbatim.
|
|
76
|
-
sep_idx = min(
|
|
77
|
-
(i for i in (raw_url.find("/"), raw_url.find("?")) if i != -1),
|
|
78
|
-
default=len(raw_url),
|
|
79
|
-
)
|
|
80
|
-
callout_key = raw_url[:sep_idx]
|
|
81
|
-
path_suffix = raw_url[sep_idx:]
|
|
82
|
-
|
|
83
|
-
if callout_key == "callout:":
|
|
84
|
-
raise NamedCredentialCallError(
|
|
85
|
-
f"Named Credential name is empty in URL '{raw_url}'."
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
cred_config = self._store.get(callout_key)
|
|
89
|
-
base_url = self._base_url_cache.get(callout_key)
|
|
90
|
-
if base_url is None:
|
|
91
|
-
base_url = resolve_base_url(callout_key, cred_config, self._token_provider)
|
|
92
|
-
self._base_url_cache[callout_key] = base_url
|
|
93
|
-
|
|
94
|
-
body = callout_request.get("body") or None
|
|
95
|
-
# Headers are passed; the SDK assumes no Content-Type.
|
|
96
|
-
headers = dict(callout_request.get("headers", {}))
|
|
97
|
-
|
|
98
|
-
response = requests.request(
|
|
99
|
-
method=callout_request["method"],
|
|
100
|
-
url=base_url + path_suffix,
|
|
101
|
-
headers=headers,
|
|
102
|
-
data=body,
|
|
103
|
-
auth=DynamicAuthHandler(cred_config),
|
|
104
|
-
timeout=30,
|
|
105
|
-
)
|
|
106
|
-
return {
|
|
107
|
-
"status_code": response.status_code,
|
|
108
|
-
"headers": dict(response.headers),
|
|
109
|
-
"body": response.text,
|
|
110
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025, Salesforce, Inc.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
"""Resolve a ``callout:<NamedCredential>`` reference to a real base URL.
|
|
16
|
-
|
|
17
|
-
Prefers the NamedCredential Connect API ``calloutUrl``; falls back to the
|
|
18
|
-
developer-supplied ``target_url`` when the API is unavailable or the Named
|
|
19
|
-
Credential is not yet onboarded.
|
|
20
|
-
"""
|
|
21
|
-
|
|
22
|
-
from __future__ import annotations
|
|
23
|
-
|
|
24
|
-
import logging
|
|
25
|
-
from typing import (
|
|
26
|
-
TYPE_CHECKING,
|
|
27
|
-
Any,
|
|
28
|
-
Dict,
|
|
29
|
-
Optional,
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
import requests
|
|
33
|
-
|
|
34
|
-
from datacustomcode.named_credential.direct.credentials import CredentialError
|
|
35
|
-
|
|
36
|
-
if TYPE_CHECKING:
|
|
37
|
-
from datacustomcode.token_provider import TokenProvider
|
|
38
|
-
|
|
39
|
-
logger = logging.getLogger(__name__)
|
|
40
|
-
|
|
41
|
-
NAMED_CREDENTIAL_PATH = (
|
|
42
|
-
"services/data/v63.0/named-credentials/named-credential-setup/{name}"
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def _callout_url_from_connect_api(
|
|
47
|
-
developer_name: str, token_provider: TokenProvider
|
|
48
|
-
) -> Optional[str]:
|
|
49
|
-
"""Fetch ``calloutUrl`` for a Named Credential, or None on any failure."""
|
|
50
|
-
try:
|
|
51
|
-
token = token_provider.get_token()
|
|
52
|
-
except Exception as exc:
|
|
53
|
-
# No usable token (e.g. not logged in) — expected; fall back quietly.
|
|
54
|
-
logger.debug("Could not obtain a token for %s: %s", developer_name, exc)
|
|
55
|
-
return None
|
|
56
|
-
|
|
57
|
-
try:
|
|
58
|
-
path = NAMED_CREDENTIAL_PATH.format(name=developer_name)
|
|
59
|
-
url = f"{token.instance_url.rstrip('/')}/{path}"
|
|
60
|
-
response = requests.get(
|
|
61
|
-
url,
|
|
62
|
-
headers={"Authorization": f"Bearer {token.access_token}"},
|
|
63
|
-
timeout=30,
|
|
64
|
-
)
|
|
65
|
-
response.raise_for_status()
|
|
66
|
-
callout_url = response.json().get("calloutUrl")
|
|
67
|
-
return callout_url or None
|
|
68
|
-
except Exception as exc:
|
|
69
|
-
# A token was obtained but the Connect API call failed — likely a
|
|
70
|
-
# misconfiguration (named credential not onboarded, missing
|
|
71
|
-
# permission, wrong org). Surface it before falling back to target_url.
|
|
72
|
-
logger.warning(
|
|
73
|
-
"Connect API URL resolution failed for %s: %s. "
|
|
74
|
-
"Falling back to 'target_url' from external_callout_config.json if set.",
|
|
75
|
-
developer_name,
|
|
76
|
-
exc,
|
|
77
|
-
)
|
|
78
|
-
return None
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def resolve_base_url(
|
|
82
|
-
callout_key: str,
|
|
83
|
-
cred_config: Dict[str, Any],
|
|
84
|
-
token_provider: Optional[TokenProvider],
|
|
85
|
-
) -> str:
|
|
86
|
-
"""Resolve the base URL for a callout key.
|
|
87
|
-
|
|
88
|
-
Args:
|
|
89
|
-
callout_key: e.g. ``callout:Nominatim_Geocoding``.
|
|
90
|
-
cred_config: The callout's ``external_callout_config.json`` entry.
|
|
91
|
-
token_provider: Provides a token/instance URL for the Connect API; when
|
|
92
|
-
None, only ``target_url`` is used.
|
|
93
|
-
|
|
94
|
-
Raises:
|
|
95
|
-
CredentialError: if no URL can be resolved.
|
|
96
|
-
"""
|
|
97
|
-
developer_name = callout_key.split(":", 1)[1] if ":" in callout_key else callout_key
|
|
98
|
-
|
|
99
|
-
base_url: Optional[str] = None
|
|
100
|
-
if token_provider is not None:
|
|
101
|
-
base_url = _callout_url_from_connect_api(developer_name, token_provider)
|
|
102
|
-
|
|
103
|
-
if not base_url:
|
|
104
|
-
base_url = cred_config.get("target_url") or None
|
|
105
|
-
|
|
106
|
-
if not base_url:
|
|
107
|
-
raise CredentialError(
|
|
108
|
-
f"Could not resolve a URL for '{callout_key}'. Ensure the Named "
|
|
109
|
-
f"Credential exists (sf login) or set 'target_url' in "
|
|
110
|
-
f"external_callout_config.json."
|
|
111
|
-
)
|
|
112
|
-
return base_url.rstrip("/")
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025, Salesforce, Inc.
|
|
2
|
-
# SPDX-License-Identifier: Apache-2
|
|
3
|
-
#
|
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
# you may not use this file except in compliance with the License.
|
|
6
|
-
# You may obtain a copy of the License at
|
|
7
|
-
#
|
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
#
|
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
# See the License for the specific language governing permissions and
|
|
14
|
-
# limitations under the License.
|
|
15
|
-
"""Exceptions raised by Named Credential implementations."""
|
|
16
|
-
|
|
17
|
-
from __future__ import annotations
|
|
18
|
-
|
|
19
|
-
from typing import Optional
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
class NamedCredentialCallError(RuntimeError):
|
|
23
|
-
"""Raised when a Named Credential external callout fails."""
|
|
24
|
-
|
|
25
|
-
def __init__(
|
|
26
|
-
self,
|
|
27
|
-
message: str,
|
|
28
|
-
*,
|
|
29
|
-
status: Optional[object] = None,
|
|
30
|
-
error_code: Optional[str] = None,
|
|
31
|
-
error_message: Optional[str] = None,
|
|
32
|
-
) -> None:
|
|
33
|
-
super().__init__(message)
|
|
34
|
-
self.status = status
|
|
35
|
-
self.error_code = error_code
|
|
36
|
-
self.error_message = error_message
|