tracdap-ext-http 0.10.0.dev14__py3-none-any.whl → 0.10.0.dev15__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.
@@ -13,4 +13,4 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
 
16
- __version__ = "0.10.0.dev14"
16
+ __version__ = "0.10.0.dev15"
@@ -13,28 +13,119 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
 
16
- import typing as _tp
16
+ import http.client as _hc
17
17
 
18
+ try:
19
+ import urllib3 as _ul3 # noqa
20
+ except ModuleNotFoundError:
21
+ _ul3 = None
22
+
23
+ import tracdap.rt.config as _cfg
24
+ import tracdap.rt.exceptions as _ex
18
25
  import tracdap.rt.ext.external as _external
19
26
  import tracdap.rt.ext.plugins as _plugins
27
+ import tracdap.rt.ext.util as _util
20
28
 
21
29
 
22
30
  class HttpPlugin(_external.IExternalSystem):
23
31
 
24
- def __init__(self, properties: _tp.Dict[str, str]):
25
- self.__properties = properties
32
+ HOST_KEY = "host"
33
+ PORT_KEY = "port"
34
+ TLS_KEY = "tls"
35
+ TIMEOUT_KEY = "timeout"
36
+
37
+ def __init__(self, resource_name: str, config: _cfg.PluginConfig):
38
+
39
+ self.__resource_name = resource_name
40
+
41
+ self.__host = _util.read_plugin_config(config, self.HOST_KEY)
42
+ self.__port = _util.read_plugin_config(config, self.PORT_KEY, optional=True, convert=int)
43
+ self.__tls = _util.read_plugin_config(config, self.TLS_KEY, default=True, convert=bool)
44
+ self.__timeout = _util.read_plugin_config(config, self.TIMEOUT_KEY, optional=True, convert=int)
45
+
46
+ def supported_types(self) -> list[type]:
47
+
48
+ supported_types: list[type] = list()
49
+
50
+ if _hc:
51
+ supported_types.append(_hc.HTTPConnection)
52
+ supported_types.append(_hc.HTTPSConnection)
53
+
54
+ if _ul3:
55
+ supported_types.append(_ul3.HTTPConnectionPool)
56
+ supported_types.append(_ul3.HTTPSConnectionPool)
57
+
58
+ return supported_types
59
+
60
+ def supported_args(self) -> dict[str, type] | None:
61
+
62
+ return {
63
+ "timeout": float
64
+ }
65
+
66
+ def create_client(self, client_type: type, **client_args) -> object:
67
+
68
+ if client_type == _hc.HTTPConnection:
69
+ if self.__tls:
70
+ return self._create_client_hc_https(**client_args)
71
+ else:
72
+ return self._create_client_hc_http(**client_args)
73
+
74
+ if client_type == _hc.HTTPSConnection:
75
+ if self.__tls:
76
+ return self._create_client_hc_https(**client_args)
77
+ else:
78
+ raise self._error_tls_not_enabled()
79
+
80
+ if _ul3 and client_type == _ul3.HTTPConnectionPool:
81
+ if self.__tls:
82
+ return self._create_client_ul3_https(**client_args)
83
+ else:
84
+ return self._create_client_ul3_http(**client_args)
85
+
86
+ if _ul3 and client_type == _ul3.HTTPSConnectionPool:
87
+ if self.__tls:
88
+ return self._create_client_ul3_https(**client_args)
89
+ else:
90
+ raise self._error_tls_not_enabled()
91
+
92
+ raise _ex.EPluginNotAvailable(f"Client type [{client_type.__qualname__}] is not available in {self.__class__.__name__}")
93
+
94
+ def _create_client_hc_http(self, **client_args):
95
+ hc_args = self._build_common_args(**client_args)
96
+ return _hc.HTTPSConnection(self.__host, self.__port, **hc_args)
97
+
98
+ def _create_client_hc_https(self, **client_args):
99
+ hc_args = self._build_common_args(**client_args)
100
+ return _hc.HTTPSConnection(self.__host, self.__port, **hc_args)
101
+
102
+ def _create_client_ul3_http(self, **client_args):
103
+ ul3_args = self._build_common_args(**client_args)
104
+ return _ul3.HTTPSConnectionPool(self.__host, self.__port, **ul3_args)
105
+
106
+ def _create_client_ul3_https(self, **client_args):
107
+ ul3_args = self._build_common_args(**client_args)
108
+ return _ul3.HTTPSConnectionPool(self.__host, self.__port, **ul3_args)
109
+
110
+ def _build_common_args(self, **client_args):
111
+
112
+ args = dict()
26
113
 
27
- def supported_types(self) -> _tp.List[type]:
28
- pass
114
+ if self.TIMEOUT_KEY in client_args and self.__timeout is not None:
115
+ args[self.TIMEOUT_KEY] = min(client_args[self.TIMEOUT_KEY], self.__timeout)
116
+ elif self.TIMEOUT_KEY in client_args:
117
+ args[self.TIMEOUT_KEY] = client_args[self.TIMEOUT_KEY]
118
+ elif self.__timeout is not None:
119
+ args[self.TIMEOUT_KEY] = self.__timeout
29
120
 
30
- def supported_args(self) -> _tp.Optional[_tp.Dict[str, type]]:
31
- pass
121
+ return args
32
122
 
33
- def create_client(self, client_type: type, **client_args) -> _tp.Any:
34
- pass
123
+ def _error_tls_not_enabled(self):
124
+ detail = f"The resource [{self.__resource_name }] does not have TLS enabled"
125
+ return _ex.ERuntimeValidation(f"Cannot create HTTPS connection: {detail}")
35
126
 
36
- def close_client(self, client: _tp.Any):
37
- pass
127
+ def close_client(self, client: object):
128
+ client.close() # noqa
38
129
 
39
130
 
40
131
  _plugins.PluginManager.register_plugin(
@@ -0,0 +1,172 @@
1
+ Metadata-Version: 2.4
2
+ Name: tracdap-ext-http
3
+ Version: 0.10.0.dev15
4
+ Summary: An extension for TRAC D.A.P. that lets models connect to HTTP endpoints
5
+ Author-email: Martin Traverse <martin@fintrac.co.uk>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://tracdap.finos.org/
8
+ Project-URL: Documentation, https://tracdap.readthedocs.io/
9
+ Project-URL: Source Code, https://github.com/finos/tracdap
10
+ Project-URL: Issue Tracker, https://github.com/finos/tracdap/issues
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Intended Audience :: Developers
18
+ Classifier: Intended Audience :: End Users/Desktop
19
+ Classifier: Intended Audience :: Financial and Insurance Industry
20
+ Classifier: Topic :: Office/Business
21
+ Classifier: Topic :: Office/Business :: Financial
22
+ Classifier: Topic :: Software Development
23
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
24
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
+ Requires-Python: >=3.10
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Requires-Dist: tracdap-runtime>=0.10.0-dev.15
29
+ Dynamic: license-file
30
+
31
+ <h1 align="center">
32
+
33
+ ![tracdap](https://github.com/finos/tracdap/raw/main/doc/_images/tracmmp_horizontal_400.png)
34
+
35
+ </h1>
36
+
37
+ <p align="center">
38
+ <a href="https://pypi.org/project/tracdap-ext-http"><img alt="PyPI Version" src="https://img.shields.io/pypi/v/tracdap-ext-http.svg?maxAge=86400" /></a>
39
+ <a href="https://pypi.org/project/tracdap-ext-http"><img alt="Python Versions" src="https://img.shields.io/pypi/pyversions/tracdap-ext-http.svg?maxAge=86400" /></a>
40
+ <a href="https://github.com/finos/tracdap/actions/workflows/packaging.yaml?query=branch%3Amain"><img alt="Packaging status" src="https://github.com/finos/tracdap/actions/workflows/packaging.yaml/badge.svg?branch:main&workflow:CI" /></a>
41
+ <a href="https://github.com/finos/tracdap/actions/workflows/compliance.yaml?query=branch%3Amain"><img alt="Compliance status" src="https://github.com/finos/tracdap/actions/workflows/compliance.yaml/badge.svg?branch:main&workflow:CI" /></a>
42
+ <a href="https://community.finos.org/docs/governance/software-projects/stages/incubating/"><img alt="FINOS - Incubating" src="https://cdn.jsdelivr.net/gh/finos/contrib-toolbox@master/images/badge-incubating.svg" /></a>
43
+ </p>
44
+
45
+
46
+ # HTTP Extension for the TRAC Model Runtime
47
+
48
+ This extension allows TRAC models to make calls to external systems using HTTP and HTTPS.
49
+
50
+ - Make HTTP endpoints available to models, to use directly in model code
51
+ - Connection settings managed by TRAC for both local and deployed models
52
+ - Supports http.client and urllib3
53
+ - Supports
54
+
55
+ Models that make external calls are not considered repeatable,
56
+ and will be flagged as non-repeatable when they run on the TRAC platform.
57
+
58
+ This extension is a pre-release and will be finalized inTRAC 0.10.
59
+
60
+
61
+ ## Installing
62
+
63
+ The HTTP extension can be installed with [pip](https://pip.pypa.io):
64
+
65
+ ```shell
66
+ $ pip install tracdap-ext-http
67
+ ```
68
+
69
+ The package has the following dependencies:
70
+
71
+ - tracdap-runtime (version 0.10.0-beta1 or later)
72
+ - urllib3 (optional, version 2.x)
73
+
74
+
75
+ ## Using the http.client API
76
+
77
+ Here is a minimum working example of a TRAC model using the http.client API:
78
+
79
+ ```python
80
+ import tracdap.rt.api as trac
81
+ import http.client as hc
82
+
83
+ class TestModel(trac.TracModel):
84
+
85
+ # ... define parameters, inputs and outputs
86
+
87
+ def define_resources(self):
88
+
89
+ return {
90
+ "github": trac.define_external_system("http", hc.HTTPSConnection)
91
+ }
92
+
93
+ def run_model(self, ctx: trac.TracContext):
94
+
95
+ with ctx.get_external_system("github", hc.HTTPSConnection) as github:
96
+
97
+ github.connect()
98
+ github.request("GET", "finos/tracdap/refs/heads/main/README.md")
99
+
100
+ response = github.getresponse()
101
+ response_text = response.read().decode("utf-8")
102
+ first_line = response_text.splitlines()[0]
103
+
104
+ ctx.log.info(first_line)
105
+
106
+ if __name__ == '__main__':
107
+ import tracdap.rt.launch as launch
108
+ launch.launch_model(TestModel, "config/job_config.yaml", "config/sys_config.yaml")
109
+ ```
110
+
111
+ To make this example work, you will need to add ``github`` as a resource in the system config file:
112
+
113
+ ```yaml
114
+ resources:
115
+
116
+ github:
117
+ resourceType: EXTERNAL_SYSTEM
118
+ protocol: http
119
+ properties:
120
+ host: raw.githubusercontent.com
121
+ port: 443
122
+ tls: true
123
+ ```
124
+
125
+ The following configuration properties are supported:
126
+
127
+ - host, string, required
128
+ - port, int, optional
129
+ - tls, bool, default = true
130
+ - timeout, float, optional
131
+
132
+ Models using the client type ``HTTPSConnection`` will only work if tls = true is set in the configuration.
133
+ Models requesting ``HTTPConnection`` will work with tls = true or tls = false,
134
+ and will receive an ``HTTPSConnection`` if tls = true.
135
+
136
+
137
+ ## Using the urllib3 API
138
+
139
+ Here is a minimum working example of a TRAC model using the urllib3 API.
140
+ In order to use this API, the urllib3 package must be installed.
141
+
142
+ ```python
143
+ import tracdap.rt.api as trac
144
+ import urllib3
145
+
146
+ class TestModel(trac.TracModel):
147
+
148
+ # ... define parameters, inputs and outputs
149
+
150
+ def define_resources(self):
151
+
152
+ return {
153
+ "github": trac.define_external_system("http", urllib3.HTTPSConnectionPool)
154
+ }
155
+
156
+ def run_model(self, ctx: trac.TracContext):
157
+
158
+ with ctx.get_external_system("github", urllib3.HTTPSConnectionPool, timeout=10.0) as github:
159
+
160
+ response = github.request("GET", "/finos/tracdap/refs/heads/main/README.md")
161
+
162
+ response_text = response.data.decode("utf-8")
163
+ first_line = response_text.splitlines()[0]
164
+
165
+ ctx.log.info(first_line)
166
+
167
+ if __name__ == '__main__':
168
+ import tracdap.rt.launch as launch
169
+ launch.launch_model(TestModel, "config/job_config.yaml", "config/sys_config.yaml")
170
+ ```
171
+
172
+ The resource configuration for the urllib3 API is identical to the http.client API.
@@ -0,0 +1,7 @@
1
+ tracdap/ext/http/__init__.py,sha256=Lqktwp14D9743PLhaZ90bcmoT5SKO3XW7cexWxthRLE,825
2
+ tracdap/ext/http/http_plugin.py,sha256=YxPxtZbu2Xwz5nVjAA2FdRlcTzrmzwyYMMD4lCSnplQ,4955
3
+ tracdap_ext_http-0.10.0.dev15.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
4
+ tracdap_ext_http-0.10.0.dev15.dist-info/METADATA,sha256=hjdN5JNRaGB3SoZwWZrzcQDIJrAv7tSWdy9VgP4cI68,6086
5
+ tracdap_ext_http-0.10.0.dev15.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
+ tracdap_ext_http-0.10.0.dev15.dist-info/top_level.txt,sha256=Uv0JfaE1Lp4JnCzqW8lqXNJAEcsAFpAUGOghJolVNdM,8
7
+ tracdap_ext_http-0.10.0.dev15.dist-info/RECORD,,
@@ -1,37 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: tracdap-ext-http
3
- Version: 0.10.0.dev14
4
- Summary: An extension for TRAC D.A.P. that lets models connect to HTTP endpoints
5
- Author-email: Martin Traverse <martin@fintrac.co.uk>
6
- License-Expression: Apache-2.0
7
- Project-URL: Homepage, https://tracdap.finos.org/
8
- Project-URL: Documentation, https://tracdap.readthedocs.io/
9
- Project-URL: Source Code, https://github.com/martin-traverse/tracdap
10
- Project-URL: Issue Tracker, https://github.com/martin-traverse/tracdap/issues
11
- Classifier: Operating System :: OS Independent
12
- Classifier: Programming Language :: Python :: 3
13
- Classifier: Programming Language :: Python :: 3.9
14
- Classifier: Programming Language :: Python :: 3.10
15
- Classifier: Programming Language :: Python :: 3.11
16
- Classifier: Programming Language :: Python :: 3.12
17
- Classifier: Programming Language :: Python :: 3.13
18
- Classifier: Framework :: Trac
19
- Classifier: Intended Audience :: Developers
20
- Classifier: Intended Audience :: End Users/Desktop
21
- Classifier: Intended Audience :: Financial and Insurance Industry
22
- Classifier: Topic :: Office/Business
23
- Classifier: Topic :: Office/Business :: Financial
24
- Classifier: Topic :: Software Development
25
- Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
26
- Classifier: Topic :: Software Development :: Libraries :: Python Modules
27
- Requires-Python: >=3.9
28
- Description-Content-Type: text/markdown
29
- License-File: LICENSE
30
- Dynamic: license-file
31
-
32
- # HTTP Extension for the TRAC Model Runtime
33
-
34
- An extension for TRAC D.A.P. that lets models connect to HTTP endpoints.
35
- The extension depends on the tracdap-runtime package with the same version.
36
-
37
- This extension is a pre-release, full documentation will be provided TRAC 0.10.
@@ -1,7 +0,0 @@
1
- tracdap/ext/http/__init__.py,sha256=g9nFNk6JqQjYEFNgAR_7VaQelKzCcypHGGitTl38WLs,825
2
- tracdap/ext/http/http_plugin.py,sha256=apXj_bPk4NTneZ2cx3-1ERVpfvIKOxSvqhG2aL9A2YA,1456
3
- tracdap_ext_http-0.10.0.dev14.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
4
- tracdap_ext_http-0.10.0.dev14.dist-info/METADATA,sha256=Ssfozq4s1wxQHUiSGnxhnrdtEhVzzrdj2JvXYzi4lcw,1695
5
- tracdap_ext_http-0.10.0.dev14.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
- tracdap_ext_http-0.10.0.dev14.dist-info/top_level.txt,sha256=Uv0JfaE1Lp4JnCzqW8lqXNJAEcsAFpAUGOghJolVNdM,8
7
- tracdap_ext_http-0.10.0.dev14.dist-info/RECORD,,