Metadata-Version: 2.4
Name: bigquery-fxmacrodata
Version: 0.1.0
Summary: Load public FXMacroData responses into Google BigQuery
License-Expression: MIT
Project-URL: Documentation, https://fxmacrodata.com/documentation/reference?utm_source=pypi&utm_medium=referral&utm_campaign=bigquery-fxmacrodata&utm_content=package
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fxmacrodata-platform-core==0.1.0
Requires-Dist: google-cloud-bigquery<4,>=3.46.1
Provides-Extra: test
Requires-Dist: pytest<9,>=8; extra == "test"
Dynamic: license-file

# FXMacroData for BigQuery

Load FXMacroData public responses into a customer-owned BigQuery dataset with
native JSON load jobs. Preserve records and response metadata alongside typed
columns for research and analytical joins.

[FXMacroData documentation](https://fxmacrodata.com/documentation/reference?utm_source=github&utm_medium=referral&utm_campaign=bigquery-fxmacrodata&utm_content=readme)

## Install and connect

Requires Python 3.10 or later. Install this package and its adjacent public
client dependencies from the distribution supplied to you:

```sh
python -m pip install /path/to/fxmacrodata-public-client /path/to/fxmacrodata-platform-core /path/to/bigquery-fxmacrodata
```

Configure Google Application Default Credentials in your execution environment
and expose the FXMacroData source key as `FXMD_API_KEY` through your secret store.
The adapter does not read or package a Google credential file.

```python
import os
from google.cloud import bigquery
from fxmacrodata_platform_core import read
from fxmacrodata_bigquery import load

client = bigquery.Client(project=os.environ["GOOGLE_CLOUD_PROJECT"])
try:
    batch = read("release_calendar", {"currency": "usd"}, api_key_env="FXMD_API_KEY")
    result = load(
        batch, client,
        f"{client.project}.analytics.fxmacrodata",
        location=os.environ["BIGQUERY_LOCATION"],
    )
finally:
    client.close()
```

The dataset must exist and its location must match the supplied job location.
The caller needs permission to create jobs and load rows, plus table-creation
permission when `create_table=True`. Set `create_table=False` for an existing
table with the schema returned by `schema()`. Table identifiers must be fully
qualified `project.dataset.table` names; SQL fragments are rejected.

Every operation exposed by `fxmacrodata_platform_core.operations()` is available,
including REST and MCP operations. The loader adds a tabular envelope without
changing the original public response. `record_json` retains the original record;
`response_json` retains the complete response, including pagination and metadata.
An empty response produces one envelope with `record_json = 'null'`, so absence of
data and the server's metadata remain inspectable. It is not an observation.

Columns are `operation`, `source_url`, `record_id`, `currency`, `indicator`,
`date`, `announcement_datetime`, `value`, `value_text`, `record_json`,
`response_json`, and the loader's `content_hash`. Only `value` is numeric.
Dates and announcement times remain source strings. No publication time is
inferred, and no missing value is filled. `value_text` and JSON preserve source
numbers supplied as strings. JSON numeric tokens follow the public Python client
parser, so original lexical precision is not guaranteed.

Select operations with the shared catalogue rather than constructing URLs:

```python
from fxmacrodata_platform_core import operations, schemas

available = operations()
input_schemas = schemas()
```

`load_operation` accepts `api_key_env`, `complete_history`, `max_pages`, and
`client_factory` for the public source reader. `max_pages` bounds history
pagination; an incomplete read raises an error before a destination load starts.
`batch_size` bounds destination blocks to 1–10,000 rows. `max_batch_bytes` also
bounds each block using a conservative JSON byte estimate (16 MiB by default,
128 MiB maximum). An oversized individual envelope is rejected before destination
changes; narrow the source date range to reduce the response size. The source response is
materialized before loading; choose a source date range suitable for memory.
Read the installed operation's schema for required arguments and date filters.

Supply source credentials through your runtime secret store, exposing only the
environment variable name to the adapter. Destination authentication belongs to
the official SDK connection you pass to the loader. The adapter neither saves nor
prints credentials and never constructs destination credentials for you.

The package licence covers this adapter code. FXMacroData data access and onward
redistribution remain subject to your subscription and applicable licence. Loading
data into a warehouse does not grant permission to publish or resell it.

## Retries and querying

Each bounded load job has a deterministic ID derived from the destination and
the complete block. Retrying the same batch with the same block size recovers a
conflicting job by ID and waits for its result. A failed or timed-out job raises
`LoadError` with its ID; inspect that job before retrying. Earlier jobs may already
have completed. A permanently failed job needs a reviewed correction to the data
or destination; the adapter does not silently submit it under a new ID.

BigQuery retains job metadata for a limited period. IDs therefore do not provide
permanent deduplication. Use the supplied view helper for exact repeated envelopes:

```python
from fxmacrodata_bigquery import deduplicated_view_sql

sql = deduplicated_view_sql(
    f"{client.project}.analytics.fxmacrodata",
    f"{client.project}.analytics.fxmacrodata_distinct",
)
client.query(sql).result()
```

Content hashes retain changed records and response metadata. This view does not
choose a latest revision or assert that an observation was known at any historic
time. `LoadResult.rows` counts envelopes submitted, rather than newly created
logical rows. Load jobs append; the loader never truncates a destination table.

[event_study.sql](examples/event_study.sql) joins source announcement times to
your licensed market prices. Validate publisher/publication metadata separately
for point-in-time research. [sharing.md](examples/sharing.md) describes the
customer-controlled BigQuery Sharing route and its permission and licensing gates.

## Test

```sh
python -m pytest tests -q
```

Official SDK reference:
[BigQuery Python Client](https://docs.cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client?utm_source=pypi&utm_medium=referral&utm_campaign=bigquery-fxmacrodata&utm_content=upstream-reference).
