ComtradeClient¶
The single public entry point to the SDK. Construct one ComtradeClient
per session; it opens five lazy service facades (metadata, trade,
analytics, etl, storage), each a per-client singleton that
shares the client's transport and configuration.
API reference¶
The full reference is generated from the SDK's docstrings via mkdocstrings. The page below renders the docstring verbatim.
ComtradeClient
¶
Primary entry point for the UN Comtrade Python SDK.
The client composes the infrastructure layers built in
earlier Phase 1 tasks and the public service facades
added in FC-001. It owns one HttpTransport instance
for the lifetime of the client.
Public service accessors (all lazy, all singletons per-client):
client.metadata— reference catalogue service (MetadataService).client.trade— trade-data service (TradeService).client.analytics— analytics engine (AnalyticsEngine).client.etl— ETL pipeline factory (ETLFacade).client.storage— storage registry (StorageRegistry).
Usage::
from un_comtrade import ComtradeClient
from un_comtrade.config import Configuration
cfg = Configuration(api_key="...")
with ComtradeClient(cfg) as client:
countries = client.metadata.get_countries()
exports = client.trade.get_exports(699, "2022")
summary = client.analytics.country_summary(
dataset, reporter_code=699,
)
pipeline = client.etl.pipeline("ingest", stages)
pipeline.run(source)
dataset = client.storage.open("data.parquet")
Or, for the convenience of letting the SDK load the configuration from environment variables::
client = ComtradeClient() # reads UN_COMTRADE_KEY etc.
Source code in un_comtrade/client.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | |
transport
property
¶
The HTTP transport used by the client.
Exposed for advanced consumers (e.g. diagnostics). Future business methods will not require callers to interact with the transport directly.
metadata
property
¶
metadata: MetadataService
The metadata service owned by this client.
Constructed lazily on first access. The client
retains ownership — the service is closed when
client.close() runs (no-op for the current
skeleton; cache lifecycle will land with the
cache subsystem).
trade
property
¶
The trade-data service owned by this client.
Constructed lazily on first access. The service
shares this client's transport and
configuration (per the FC-001 contract); it
does not take ownership of the transport.
analytics
property
¶
The analytics engine owned by this client.
Constructed lazily on first access. The engine receives a small mapping of the client's configuration so it can surface SDK-wide settings (e.g. log level) on its results.
etl
property
¶
The ETL pipeline facade owned by this client.
Constructed lazily on first access. The facade
injects a small mapping of this client's
configuration into every pipeline it builds
via ETLFacade.pipeline(...).
storage
property
¶
The storage registry owned by this client.
Constructed lazily on first access. The
registry is a thin wrapper over the five SDK
backends; client.storage.open(uri) is the
public read path.
close
¶
Release the transport's underlying resources.
Closes the transport only when the client owns it (the default). When a caller-supplied transport was injected via the constructor, the caller retains ownership and is responsible for closing it.
Safe to call multiple times.
Source code in un_comtrade/client.py
The Configuration dataclass lives in un_comtrade.config and is
documented on the Models page.
Examples¶
from un_comtrade import ComtradeClient
with ComtradeClient() as client:
countries = client.metadata.get_countries()
exports = client.trade.get_exports(reporter_code=699, period="2022")
top = client.analytics.top_partners(exports, by="exports", limit=5)
client.storage.open("india_exports_2022.parquet").write(exports)
Related Recipes¶
- RECIPE-001 — List reporter countries.
- RECIPE-011 — Fetch India's annual exports.
- RECIPE-021 — Compute a country trade balance.
Related Guides¶
- Python SDK → Index — idiomatic patterns.
- Quick Start — first-query walkthrough.