pubsub-fxmacrodata 0.1.0__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.
- fxmacrodata_pubsub/__init__.py +157 -0
- fxmacrodata_pubsub/py.typed +0 -0
- pubsub_fxmacrodata-0.1.0.dist-info/METADATA +122 -0
- pubsub_fxmacrodata-0.1.0.dist-info/RECORD +7 -0
- pubsub_fxmacrodata-0.1.0.dist-info/WHEEL +5 -0
- pubsub_fxmacrodata-0.1.0.dist-info/licenses/LICENSE +21 -0
- pubsub_fxmacrodata-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Publish public FXMacroData responses to a customer-owned Google Cloud Pub/Sub topic."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
import hashlib
|
|
7
|
+
import json
|
|
8
|
+
import math
|
|
9
|
+
import re
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from fxmacrodata_platform_core import read
|
|
13
|
+
|
|
14
|
+
__version__ = "0.1.0"
|
|
15
|
+
|
|
16
|
+
COLUMNS = (
|
|
17
|
+
"operation", "source_url", "record_id", "currency", "indicator", "date",
|
|
18
|
+
"announcement_datetime", "value", "value_text", "record_json", "response_json",
|
|
19
|
+
"content_hash",
|
|
20
|
+
)
|
|
21
|
+
ATTRIBUTES = ("operation", "record_id", "content_hash", "source_url")
|
|
22
|
+
MAX_MESSAGE_BYTES = 16 * 1024 * 1024
|
|
23
|
+
_TOPIC = re.compile(
|
|
24
|
+
r"projects/[a-z][a-z0-9-]{4,28}[a-z0-9]/topics/(?!goog)[A-Za-z][A-Za-z0-9._~%+-]{2,254}\Z"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class PublishError(RuntimeError):
|
|
29
|
+
"""A destination publish failed; inspect the destination before retrying."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class PublishResult:
|
|
34
|
+
destination: str
|
|
35
|
+
messages: int
|
|
36
|
+
batches: int
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _topic(topic: str) -> str:
|
|
40
|
+
if not isinstance(topic, str) or not _TOPIC.fullmatch(topic):
|
|
41
|
+
raise ValueError("destination must be a full topic path: projects/<project>/topics/<topic>")
|
|
42
|
+
return topic
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _bounds(batch_size: int, max_message_bytes: int) -> None:
|
|
46
|
+
if isinstance(batch_size, bool) or not isinstance(batch_size, int) or not 1 <= batch_size <= 10000:
|
|
47
|
+
raise ValueError("batch_size must be an integer between 1 and 10000")
|
|
48
|
+
if isinstance(max_message_bytes, bool) or not isinstance(max_message_bytes, int) or not 1 <= max_message_bytes <= MAX_MESSAGE_BYTES:
|
|
49
|
+
raise ValueError(f"max_message_bytes must be an integer between 1 and {MAX_MESSAGE_BYTES}")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _rows(batch: Any) -> list[dict[str, Any]]:
|
|
53
|
+
rows = []
|
|
54
|
+
for source in batch.rows():
|
|
55
|
+
row = {name: source.get(name) for name in COLUMNS[:-1]}
|
|
56
|
+
if not isinstance(row["operation"], str) or not isinstance(row["record_id"], str):
|
|
57
|
+
raise ValueError("every row requires string operation and record_id")
|
|
58
|
+
for name, value in row.items():
|
|
59
|
+
if name == "value":
|
|
60
|
+
if value is not None and (
|
|
61
|
+
isinstance(value, bool) or not isinstance(value, (int, float)) or
|
|
62
|
+
not math.isfinite(value)
|
|
63
|
+
):
|
|
64
|
+
raise ValueError("value must be a finite number or null")
|
|
65
|
+
elif value is not None and not isinstance(value, str):
|
|
66
|
+
raise ValueError(f"{name} must be a string or null")
|
|
67
|
+
encoded = json.dumps(row, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
|
|
68
|
+
row["content_hash"] = hashlib.sha256(encoded.encode("utf-8")).hexdigest()
|
|
69
|
+
rows.append(row)
|
|
70
|
+
return rows
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def message_key(row: dict[str, Any]) -> str:
|
|
74
|
+
"""Return the idempotency key consumers can use to drop redelivered messages."""
|
|
75
|
+
return f"{row['operation']}:{row['record_id']}:{row['content_hash']}"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def encode_message(row: dict[str, Any]) -> bytes:
|
|
79
|
+
"""Return the canonical UTF-8 JSON body for one envelope row."""
|
|
80
|
+
return json.dumps(row, sort_keys=True, separators=(",", ":"), ensure_ascii=False,
|
|
81
|
+
allow_nan=False).encode("utf-8")
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _attributes(row: dict[str, Any]) -> dict[str, str]:
|
|
85
|
+
return {name: row[name] for name in ATTRIBUTES if row.get(name) is not None}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _blocks(rows: list[dict[str, Any]], batch_size: int,
|
|
89
|
+
max_message_bytes: int) -> list[list[tuple[str, bytes, dict[str, str]]]]:
|
|
90
|
+
_bounds(batch_size, max_message_bytes)
|
|
91
|
+
output: list[list[tuple[str, bytes, dict[str, str]]]] = []
|
|
92
|
+
block: list[tuple[str, bytes, dict[str, str]]] = []
|
|
93
|
+
for row in rows:
|
|
94
|
+
body = encode_message(row)
|
|
95
|
+
if len(body) > max_message_bytes:
|
|
96
|
+
raise ValueError("one envelope exceeds max_message_bytes; narrow the source date range")
|
|
97
|
+
if len(block) >= batch_size:
|
|
98
|
+
output.append(block)
|
|
99
|
+
block = []
|
|
100
|
+
block.append((message_key(row), body, _attributes(row)))
|
|
101
|
+
if block:
|
|
102
|
+
output.append(block)
|
|
103
|
+
return output
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def publish(batch: Any, client: Any, destination: str, *, batch_size: int = 1000,
|
|
107
|
+
max_message_bytes: int = MAX_MESSAGE_BYTES, ordered: bool = True) -> PublishResult:
|
|
108
|
+
"""Publish one message per envelope row through PublisherClient.publish.
|
|
109
|
+
|
|
110
|
+
Each message carries the canonical JSON body, the attributes `operation`,
|
|
111
|
+
`record_id`, `content_hash` and `source_url`, and (when `ordered` is true)
|
|
112
|
+
an ordering key of `operation:record_id:content_hash`. Futures are awaited
|
|
113
|
+
per block, so a block is complete before the next block starts. Delivery
|
|
114
|
+
is at-least-once; a failed block raises PublishError and is not retried.
|
|
115
|
+
"""
|
|
116
|
+
topic = _topic(destination)
|
|
117
|
+
if not isinstance(ordered, bool):
|
|
118
|
+
raise ValueError("ordered must be a boolean")
|
|
119
|
+
blocks = _blocks(_rows(batch), batch_size, max_message_bytes)
|
|
120
|
+
count, messages = 0, 0
|
|
121
|
+
for block in blocks:
|
|
122
|
+
try:
|
|
123
|
+
futures = [
|
|
124
|
+
client.publish(topic, body, ordering_key=key if ordered else "", **attributes)
|
|
125
|
+
for key, body, attributes in block
|
|
126
|
+
]
|
|
127
|
+
for future in futures:
|
|
128
|
+
future.result()
|
|
129
|
+
except Exception:
|
|
130
|
+
raise PublishError(
|
|
131
|
+
f"Pub/Sub publish block {count + 1} failed; earlier blocks may be delivered. "
|
|
132
|
+
"Inspect the destination and deduplicate by ordering key on retry."
|
|
133
|
+
) from None
|
|
134
|
+
count += 1
|
|
135
|
+
messages += len(block)
|
|
136
|
+
return PublishResult(topic, messages, count)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def publish_operation(operation: str, args: dict[str, Any] | None, client: Any, destination: str,
|
|
140
|
+
*, api_key_env: str | None = None, complete_history: bool = True,
|
|
141
|
+
max_pages: int = 10000, client_factory: Any = None,
|
|
142
|
+
batch_size: int = 1000, max_message_bytes: int = MAX_MESSAGE_BYTES,
|
|
143
|
+
ordered: bool = True) -> PublishResult:
|
|
144
|
+
"""Read one public operation and publish its complete response envelope."""
|
|
145
|
+
_topic(destination)
|
|
146
|
+
_bounds(batch_size, max_message_bytes)
|
|
147
|
+
if not isinstance(ordered, bool):
|
|
148
|
+
raise ValueError("ordered must be a boolean")
|
|
149
|
+
batch = read(operation, args, api_key_env=api_key_env,
|
|
150
|
+
complete_history=complete_history, max_pages=max_pages,
|
|
151
|
+
client_factory=client_factory)
|
|
152
|
+
return publish(batch, client, destination, batch_size=batch_size,
|
|
153
|
+
max_message_bytes=max_message_bytes, ordered=ordered)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
__all__ = ["ATTRIBUTES", "COLUMNS", "MAX_MESSAGE_BYTES", "PublishError", "PublishResult",
|
|
157
|
+
"encode_message", "message_key", "publish", "publish_operation"]
|
|
File without changes
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pubsub-fxmacrodata
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Publish public FXMacroData responses to Google Cloud Pub/Sub
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Documentation, https://fxmacrodata.com/documentation/reference?utm_source=pypi&utm_medium=referral&utm_campaign=pubsub-fxmacrodata&utm_content=package
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: fxmacrodata-platform-core==0.1.0
|
|
11
|
+
Requires-Dist: google-cloud-pubsub<3,>=2.41.0
|
|
12
|
+
Provides-Extra: test
|
|
13
|
+
Requires-Dist: pytest<9,>=8; extra == "test"
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# FXMacroData for Google Cloud Pub/Sub
|
|
17
|
+
|
|
18
|
+
Publish FXMacroData public responses to a Pub/Sub topic with the official
|
|
19
|
+
`google-cloud-pubsub` Python client. Fan macro announcements out to your own
|
|
20
|
+
subscribers while retaining the source payload and revision metadata.
|
|
21
|
+
|
|
22
|
+
[FXMacroData documentation](https://fxmacrodata.com/documentation/reference?utm_source=github&utm_medium=referral&utm_campaign=pubsub-fxmacrodata&utm_content=readme)
|
|
23
|
+
|
|
24
|
+
## Install and connect
|
|
25
|
+
|
|
26
|
+
Requires Python 3.10 or later. Install this package and its adjacent public
|
|
27
|
+
client dependencies from the distribution supplied to you:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
python -m pip install /path/to/fxmacrodata-public-client /path/to/fxmacrodata-platform-core /path/to/pubsub-fxmacrodata
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Authenticate the Pub/Sub client with Application Default Credentials or the
|
|
34
|
+
service account your administrator provides. Then publish to a customer-owned
|
|
35
|
+
topic:
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from google.cloud import pubsub_v1
|
|
39
|
+
from fxmacrodata_platform_core import read
|
|
40
|
+
from fxmacrodata_pubsub import publish
|
|
41
|
+
|
|
42
|
+
client = pubsub_v1.PublisherClient(
|
|
43
|
+
publisher_options=pubsub_v1.types.PublisherOptions(enable_message_ordering=True),
|
|
44
|
+
)
|
|
45
|
+
batch = read("indicator_history", {"currency": "usd", "indicator": "policy_rate"})
|
|
46
|
+
result = publish(batch, client, "projects/my-project/topics/fxmacrodata")
|
|
47
|
+
print(result.messages, "messages in", result.batches, "blocks")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The topic must already exist. The destination is the full topic path,
|
|
51
|
+
`projects/<project>/topics/<topic>`; anything else, including URLs and
|
|
52
|
+
subscription paths, is rejected before any publish.
|
|
53
|
+
|
|
54
|
+
Every operation exposed by `fxmacrodata_platform_core.operations()` is
|
|
55
|
+
available, including REST and MCP operations. `publish_operation` reads one
|
|
56
|
+
operation and publishes it in a single call:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from fxmacrodata_pubsub import publish_operation
|
|
60
|
+
|
|
61
|
+
publish_operation("release_calendar", {"currency": "usd"}, client,
|
|
62
|
+
"projects/my-project/topics/fxmacrodata", api_key_env="FXMD_API_KEY")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Message format
|
|
66
|
+
|
|
67
|
+
Each envelope row becomes one message. The body is canonical UTF-8 JSON with
|
|
68
|
+
the keys `operation`, `source_url`, `record_id`, `currency`, `indicator`,
|
|
69
|
+
`date`, `announcement_datetime`, `value`, `value_text`, `record_json`,
|
|
70
|
+
`response_json` and the publisher's `content_hash` (SHA-256 of the ten source
|
|
71
|
+
columns). Only `value` is numeric. Dates and announcement times remain source
|
|
72
|
+
strings. No publication time is inferred, and no missing value is filled.
|
|
73
|
+
`record_json` retains the original record; `response_json` retains the
|
|
74
|
+
complete response, including pagination and metadata. An empty response
|
|
75
|
+
produces one message with `record_json` equal to `"null"`, so absence of data
|
|
76
|
+
and the server's metadata remain inspectable. It is not an observation.
|
|
77
|
+
|
|
78
|
+
Message attributes carry `operation`, `record_id`, `content_hash` and
|
|
79
|
+
`source_url` for subscription filters. The ordering key is
|
|
80
|
+
`operation:record_id:content_hash`; the same source record with the same
|
|
81
|
+
content always produces the same key, and changed data or metadata produces a
|
|
82
|
+
different one. Pass `ordered=False` to publish without an ordering key when
|
|
83
|
+
the client was created without `enable_message_ordering`.
|
|
84
|
+
|
|
85
|
+
## Delivery guarantee
|
|
86
|
+
|
|
87
|
+
Delivery is at-least-once. Messages are published in blocks of `batch_size`
|
|
88
|
+
rows (1 to 10,000, 1,000 by default) and every block's futures are awaited
|
|
89
|
+
before the next block starts. A failed block raises `PublishError` without the
|
|
90
|
+
service's own diagnostic text and is not retried; earlier blocks may already
|
|
91
|
+
be delivered. Subscribers should deduplicate on the ordering key, or on the
|
|
92
|
+
`operation`, `record_id` and `content_hash` attributes. After an ordering-key
|
|
93
|
+
failure the official client pauses that key; call `resume_publish` on the
|
|
94
|
+
client before publishing the key again.
|
|
95
|
+
|
|
96
|
+
`max_message_bytes` (16 MiB by default) rejects an individual envelope larger
|
|
97
|
+
than the budget before anything is published; narrow the source date range to
|
|
98
|
+
reduce the response size. The source response is materialized before
|
|
99
|
+
publishing; choose a source date range suitable for memory.
|
|
100
|
+
|
|
101
|
+
`publish_operation` accepts `api_key_env`, `complete_history`, `max_pages`
|
|
102
|
+
and `client_factory` for the public source reader. Supply source credentials
|
|
103
|
+
through your runtime secret store, exposing only the environment variable
|
|
104
|
+
name; omit `api_key_env` for anonymous USD access. Destination authentication
|
|
105
|
+
belongs to the official client you pass in. The adapter neither saves nor
|
|
106
|
+
prints credentials and never places one in a message or attribute.
|
|
107
|
+
|
|
108
|
+
The package licence covers this adapter code. FXMacroData data access and
|
|
109
|
+
onward redistribution remain subject to your subscription and applicable
|
|
110
|
+
licence. Publishing data to a topic does not grant permission to publish or
|
|
111
|
+
resell it.
|
|
112
|
+
|
|
113
|
+
## Development
|
|
114
|
+
|
|
115
|
+
Tests run offline against an in-memory fake client:
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
python -m pytest tests -q
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Official SDK reference:
|
|
122
|
+
[Pub/Sub publisher client](https://cloud.google.com/python/docs/reference/pubsub/latest/google.cloud.pubsub_v1.publisher.client.Client?utm_source=pypi&utm_medium=referral&utm_campaign=pubsub-fxmacrodata&utm_content=upstream-reference).
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
fxmacrodata_pubsub/__init__.py,sha256=BnloTKJILgm3co3yv4S85nyCtKhtMk_AY8FvlXYW2Wg,6667
|
|
2
|
+
fxmacrodata_pubsub/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
pubsub_fxmacrodata-0.1.0.dist-info/licenses/LICENSE,sha256=rklK7Fl8aL1eWGubsrozZ7rCNTsN-MATw4M6LQIMv10,1092
|
|
4
|
+
pubsub_fxmacrodata-0.1.0.dist-info/METADATA,sha256=sUl1BtL73qWCL9vfCbq4Ylg0kT8MGxnRHtLtOtuQtQo,5790
|
|
5
|
+
pubsub_fxmacrodata-0.1.0.dist-info/WHEEL,sha256=16fc_9Rp854SLcdhaDEjr0u65p0e3FhZxihNgcnCE98,91
|
|
6
|
+
pubsub_fxmacrodata-0.1.0.dist-info/top_level.txt,sha256=ambHvvmqVWonuWM8p3J0nLRNaPqrX1MQYOryUwggZj0,19
|
|
7
|
+
pubsub_fxmacrodata-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robert Tidball
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fxmacrodata_pubsub
|