cnovatech-datapharma 0.3.0__tar.gz

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.
Files changed (78) hide show
  1. cnovatech_datapharma-0.3.0/.gitignore +26 -0
  2. cnovatech_datapharma-0.3.0/CHANGELOG.md +64 -0
  3. cnovatech_datapharma-0.3.0/LICENSE +21 -0
  4. cnovatech_datapharma-0.3.0/PKG-INFO +275 -0
  5. cnovatech_datapharma-0.3.0/README.md +243 -0
  6. cnovatech_datapharma-0.3.0/pyproject.toml +98 -0
  7. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/__init__.py +20 -0
  8. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/_client.py +145 -0
  9. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/_config.py +24 -0
  10. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/_version.py +9 -0
  11. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/errors.py +117 -0
  12. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/models/__init__.py +262 -0
  13. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/models/account.py +186 -0
  14. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/models/brands.py +51 -0
  15. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/models/common.py +56 -0
  16. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/models/history.py +74 -0
  17. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/models/products.py +245 -0
  18. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/models/stores.py +322 -0
  19. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/models/trends.py +592 -0
  20. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/py.typed +0 -0
  21. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/resources/__init__.py +24 -0
  22. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/resources/_operations.py +831 -0
  23. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/resources/_pricing.py +32 -0
  24. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/resources/_validation.py +39 -0
  25. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/resources/account.py +105 -0
  26. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/resources/brands.py +57 -0
  27. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/resources/products.py +864 -0
  28. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/resources/stores.py +674 -0
  29. cnovatech_datapharma-0.3.0/src/cnovatech/datapharma/types.py +81 -0
  30. cnovatech_datapharma-0.3.0/tests/conftest.py +135 -0
  31. cnovatech_datapharma-0.3.0/tests/fixtures/README.md +50 -0
  32. cnovatech_datapharma-0.3.0/tests/fixtures/checkCatalogCoverage.json +45 -0
  33. cnovatech_datapharma-0.3.0/tests/fixtures/error_ean_not_found.json +7 -0
  34. cnovatech_datapharma-0.3.0/tests/fixtures/error_filter_required.json +7 -0
  35. cnovatech_datapharma-0.3.0/tests/fixtures/error_validation_error.json +12 -0
  36. cnovatech_datapharma-0.3.0/tests/fixtures/getCatalog.json +150 -0
  37. cnovatech_datapharma-0.3.0/tests/fixtures/getCompetitorProducts.json +107 -0
  38. cnovatech_datapharma-0.3.0/tests/fixtures/getPriceChanges.json +99 -0
  39. cnovatech_datapharma-0.3.0/tests/fixtures/getProductByEan.json +142 -0
  40. cnovatech_datapharma-0.3.0/tests/fixtures/getProductPriceChanges.json +178 -0
  41. cnovatech_datapharma-0.3.0/tests/fixtures/getProductPriceHistory.json +92 -0
  42. cnovatech_datapharma-0.3.0/tests/fixtures/getProductPromotions.json +231 -0
  43. cnovatech_datapharma-0.3.0/tests/fixtures/getProductStockouts.json +169 -0
  44. cnovatech_datapharma-0.3.0/tests/fixtures/getProductsByBrand.json +96 -0
  45. cnovatech_datapharma-0.3.0/tests/fixtures/getProfile.json +52 -0
  46. cnovatech_datapharma-0.3.0/tests/fixtures/getPromotions.json +88 -0
  47. cnovatech_datapharma-0.3.0/tests/fixtures/getStockouts.json +77 -0
  48. cnovatech_datapharma-0.3.0/tests/fixtures/getStoreAssortment.json +133 -0
  49. cnovatech_datapharma-0.3.0/tests/fixtures/getStoreCompetitiveness.json +59 -0
  50. cnovatech_datapharma-0.3.0/tests/fixtures/getStoreCompetitors.json +52 -0
  51. cnovatech_datapharma-0.3.0/tests/fixtures/getStoreMarketPosition.json +138 -0
  52. cnovatech_datapharma-0.3.0/tests/fixtures/getStoreOpportunities.json +130 -0
  53. cnovatech_datapharma-0.3.0/tests/fixtures/getStorePromotions.json +230 -0
  54. cnovatech_datapharma-0.3.0/tests/fixtures/getStoreStockouts.json +187 -0
  55. cnovatech_datapharma-0.3.0/tests/fixtures/getStoresByBrand.json +108 -0
  56. cnovatech_datapharma-0.3.0/tests/fixtures/getUsage.json +21 -0
  57. cnovatech_datapharma-0.3.0/tests/fixtures/getUserLogs.json +72 -0
  58. cnovatech_datapharma-0.3.0/tests/fixtures/healthCheck.json +7 -0
  59. cnovatech_datapharma-0.3.0/tests/fixtures/listApiKeys.json +22 -0
  60. cnovatech_datapharma-0.3.0/tests/fixtures/listBrands.json +113 -0
  61. cnovatech_datapharma-0.3.0/tests/fixtures/listDepartments.json +144 -0
  62. cnovatech_datapharma-0.3.0/tests/fixtures/listStores.json +66 -0
  63. cnovatech_datapharma-0.3.0/tests/fixtures/openapi.json +1 -0
  64. cnovatech_datapharma-0.3.0/tests/test_account.py +110 -0
  65. cnovatech_datapharma-0.3.0/tests/test_brands.py +59 -0
  66. cnovatech_datapharma-0.3.0/tests/test_client.py +145 -0
  67. cnovatech_datapharma-0.3.0/tests/test_contract.py +52 -0
  68. cnovatech_datapharma-0.3.0/tests/test_find.py +123 -0
  69. cnovatech_datapharma-0.3.0/tests/test_live.py +44 -0
  70. cnovatech_datapharma-0.3.0/tests/test_models.py +128 -0
  71. cnovatech_datapharma-0.3.0/tests/test_operations.py +51 -0
  72. cnovatech_datapharma-0.3.0/tests/test_parity.py +105 -0
  73. cnovatech_datapharma-0.3.0/tests/test_pricing.py +34 -0
  74. cnovatech_datapharma-0.3.0/tests/test_products.py +315 -0
  75. cnovatech_datapharma-0.3.0/tests/test_secrets.py +45 -0
  76. cnovatech_datapharma-0.3.0/tests/test_stores.py +334 -0
  77. cnovatech_datapharma-0.3.0/tests/test_trends.py +371 -0
  78. cnovatech_datapharma-0.3.0/tests/test_validation.py +35 -0
@@ -0,0 +1,26 @@
1
+ # Environments and secrets
2
+ .venv/
3
+ .venv-*/
4
+ .env
5
+ .env.*
6
+ !.env.example
7
+
8
+ # Build output
9
+ dist/
10
+ build/
11
+ *.egg-info/
12
+
13
+ # Tool caches
14
+ .pytest_cache/
15
+ .mypy_cache/
16
+ .ruff_cache/
17
+ .coverage
18
+ coverage.xml
19
+ htmlcov/
20
+ __pycache__/
21
+ *.pyc
22
+
23
+ # Editors and OS
24
+ .idea/
25
+ .vscode/
26
+ .DS_Store
@@ -0,0 +1,64 @@
1
+ # Changelog
2
+
3
+ All notable changes to `cnovatech-datapharma` are documented here. The format
4
+ follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the
5
+ project uses [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.3.0] - 2026-11-29
10
+
11
+ First release of the package. It carries the version number shared by every
12
+ Cnovatech SDK and offers the same surface as `cnovatech-datamarket` 0.3.0,
13
+ over the Data Pharma API.
14
+
15
+ ### Added
16
+
17
+ - `DataPharma` and `AsyncDataPharma` clients with configuration from arguments
18
+ or `DATAPHARMA_API_KEY` / `DATAPHARMA_BASE_URL`, connection pooling, context
19
+ manager support and a `repr` that masks the key. `max_retries` and
20
+ `max_units_per_request` are enforced by the shared core. A client can be
21
+ shared between threads; responses, pages and exceptions can be pickled.
22
+ - `health()` on both clients: unauthenticated, free of quota cost, and
23
+ tolerant of changes in the unversioned health body.
24
+ - Resources covering every read endpoint of the API: `products` (`catalog`,
25
+ `departments`, `brands`, `by_brand`, `get`, `price_history`, `coverage`),
26
+ `stores` (`list`, `find`, `assortment`, `competitors`, `competitiveness`,
27
+ `compare`, `market_position`, `opportunities`), `brands.stores` and
28
+ `account` (`profile`, `usage`, `logs`, `api_keys`). Each method states its
29
+ quota cost and estimates it before sending.
30
+ - Trend rankings on `products`: `price_changes()`, `promotions()` and
31
+ `stockouts()`, over a window of 30 or 90 days, filtered by brand, category,
32
+ department, state or free text and ordered by the share of stores involved.
33
+ - Per-product detail on `products`: `price_change_detail()`,
34
+ `promotion_detail()` and `stockout_detail()`. One product, store by store,
35
+ as events with a date: each price change with both prices, each promotion
36
+ period, each gap in stock and since when, plus the totals and the breakdown
37
+ by state and by day.
38
+ - Per-store lists on `stores`: `promotions()` and `stockouts()`, with the
39
+ situation of the same product in the rest of the market next to each row.
40
+ - `products.catalog()` takes `sort_by`, `sort_order` and `min_store_count`,
41
+ and `CatalogProduct` carries `state_store_count`, the stores of the filtered
42
+ state selling the product.
43
+ - The account methods, with the models of `cnovatech-datamarket`:
44
+ `profile().usage` has `used`, `limit` and `remaining`, the request log is a
45
+ page with `data`, `meta` and `summary`, `usage()` reports `used` and
46
+ `call_count` by day and by month, and `api_keys()` has `data`.
47
+ - Paginated responses with `next_page()`, `iter_pages()` and `iter_all()`
48
+ capped by `max_pages` or `max_units`, plus `estimate_remaining_cost()`.
49
+ - `stores.find(name)`: reads up to five pages of results until an exact name
50
+ matches; `StoreNotFoundError` and `AmbiguousStoreError` with the
51
+ candidates otherwise.
52
+ - `to_dataframe()` on every list response, with the `pandas` extra.
53
+ - `client.usage` (units spent through the client, last balance, refreshed
54
+ by quota errors too) and `client.rate_limit` (per-minute limit from the
55
+ API's headers).
56
+ - `cnovatech.datapharma.errors` with `DataPharmaError` as the root of every
57
+ exception the SDK raises, plus `EANNotFoundError`, `StoreNotFoundError`,
58
+ `FilterRequiredError`, `AmbiguousStoreError` and `BudgetExceededError`.
59
+ - `cnovatech.datapharma.models`, one module per resource, and
60
+ `cnovatech.datapharma.types` with the input enumerations. `Channel` has one
61
+ value, `"e-commerce"`: every store of the pharmacy base is an online store.
62
+ - `__version__`, read from the installed package metadata.
63
+ - Guides under `docs/`: quota and costs, pagination, errors and recipes.
64
+ - Support for Python 3.10 to 3.15.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cnovatech
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,275 @@
1
+ Metadata-Version: 2.5
2
+ Name: cnovatech-datapharma
3
+ Version: 0.3.0
4
+ Summary: Official Python SDK for the Cnovatech Data Pharma API: price, availability and assortment intelligence for the online channel of Brazilian pharmacies.
5
+ Project-URL: Homepage, https://data.pharma.cnovatech.com.br
6
+ Project-URL: Documentation, https://data.pharma.cnovatech.com.br/redoc
7
+ Project-URL: Repository, https://github.com/cnovatech-br/sdks
8
+ Project-URL: Changelog, https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/CHANGELOG.md
9
+ Author-email: Cnovatech <suporte@cnovatech.com.br>
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: api,assortment,cnovatech,data pharma,datapharma,drugstore,ean,pharmacy,pricing,sdk
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Natural Language :: English
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Programming Language :: Python :: 3.15
24
+ Classifier: Topic :: Office/Business
25
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
26
+ Classifier: Typing :: Typed
27
+ Requires-Python: >=3.10
28
+ Requires-Dist: cnovatech-sdk-core<0.4,>=0.3.0
29
+ Provides-Extra: pandas
30
+ Requires-Dist: cnovatech-sdk-core[pandas]; extra == 'pandas'
31
+ Description-Content-Type: text/markdown
32
+
33
+ # cnovatech-datapharma
34
+
35
+ Official Python SDK for the [Cnovatech Data Pharma](https://data.pharma.cnovatech.com.br)
36
+ API: daily prices, availability and assortment of more than 100,000 products
37
+ (medicines, health, beauty, baby and supplements) across the online stores of
38
+ more than 30 Brazilian pharmacies and drugstores in 10 states.
39
+
40
+ - Synchronous and asynchronous clients with the same surface
41
+ - Every response carries its quota cost; pagination can be capped by cost
42
+ - Retries only where a repeat cannot be billed twice
43
+ - Typed end to end: pydantic models, `py.typed`, `mypy --strict`
44
+
45
+ > **Status: 0.x.** Every read endpoint of the API is implemented and tested
46
+ > offline against recorded responses. Until 1.0
47
+ > a minor release may change the public surface; every such change is listed
48
+ > in the
49
+ > [changelog](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/CHANGELOG.md).
50
+
51
+ ## Installation
52
+
53
+ ```bash
54
+ pip install cnovatech-datapharma
55
+ ```
56
+
57
+ Add `[pandas]` for `to_dataframe()` on list responses. Python 3.10 or newer.
58
+
59
+ ## Quick start
60
+
61
+ ```python
62
+ from cnovatech.datapharma import DataPharma
63
+
64
+ client = DataPharma() # reads DATAPHARMA_API_KEY
65
+
66
+ product = client.products.get("7891058001155", state="SP")
67
+
68
+ product.product_name # "ANALGESICO E ANTITERMICO NOVALGINA 1G DIPIRONA ADULTO 10 COMPRIMIDOS"
69
+ product.market.median_price # 21.29
70
+ product.offers[0].store_name # "Drogaria Modelo"
71
+ product.offers[0].price # 18.9
72
+ product.offers[0].available # True
73
+
74
+ product.usage.query_cost # 1
75
+ product.usage.remaining # 994
76
+ ```
77
+
78
+ Get a key at <https://data.pharma.cnovatech.com.br>. `client.health()` checks
79
+ the API without a key and without cost.
80
+
81
+ The first lookup of a product the API has not cached yet can take up to 90
82
+ seconds; after that it answers in under a second. The default timeout allows
83
+ for it.
84
+
85
+ ## Quota
86
+
87
+ Every call is billed in quota units, and every response says what it cost:
88
+
89
+ ```python
90
+ page = client.products.catalog(search="protetor solar", limit=50)
91
+ page.usage.query_cost # 1
92
+ page.usage.remaining # 993
93
+ client.usage.spent # 2, through this client
94
+ ```
95
+
96
+ Paginated responses can be walked page by page or item by item, with a ceiling
97
+ on how much to spend:
98
+
99
+ ```python
100
+ position = client.stores.market_position(store_id, limit=1000) # 8 units per page
101
+ position.meta.pages # 4
102
+ position.estimate_remaining_cost() # 24, for the three pages not fetched yet
103
+
104
+ items = list(position.iter_all(max_units=40))
105
+ items[0].product_name # "PROTETOR SOLAR FACIAL FPS 60 40G"
106
+ items[0].price_position # "above"
107
+ ```
108
+
109
+ `iter_all` never requests again the page you already paid for. It stops quietly
110
+ at `max_pages` and raises `BudgetExceededError` before a page that would cross
111
+ `max_units`. To refuse any single call above a budget, build the client with
112
+ `DataPharma(max_units_per_request=20)`.
113
+
114
+ ## Stores and competitors
115
+
116
+ ```python
117
+ store = client.stores.find("Drogaria Modelo", state="SP") # 1 unit
118
+ store.store_id # "0f8c7d2e-6b1a-4f3e-9c5d-2a7b8e1f4c63"
119
+
120
+ competitors = client.stores.competitors(store.store_id, limit=5) # 1 unit each
121
+ competitors.data[0].store_name # "Farmácia Vizinha"
122
+ competitors.data[0].overlap_pct # 62.5, share of the assortment in common
123
+
124
+ comparison = client.stores.compare(store.store_id, competitors.data[0].store_id)
125
+ comparison.data[0].product_name # "PROTETOR SOLAR FACIAL FPS 60 40G"
126
+ comparison.data[0].price_position # "above"
127
+ comparison.data[0].price_diff_pct # 12.3
128
+ ```
129
+
130
+ ## Trends: what moved, where and since when
131
+
132
+ Three rankings say which products changed price, went on promotion or went out
133
+ of stock in the last 30 or 90 days. Each has a detail that opens one product
134
+ store by store, and the store resource lists what one store is promoting and
135
+ what is missing from its shelf.
136
+
137
+ ```python
138
+ ranking = client.products.stockouts(brand="MARCA EXEMPLO", min_store_count=10) # 1 unit
139
+ top = ranking.data[0]
140
+ top.product_name # "VITAMINA C EXEMPLO 1G 10 COMPRIMIDOS EFERVESCENTES"
141
+ top.stockout_store_count, top.store_count # 4, 28
142
+ top.stockout_store_pct # 14.3
143
+
144
+ detail = client.products.stockout_detail(top.ean, available=False) # 1 unit
145
+ detail.summary.never_available_store_count # 2, listings left out of the count
146
+ store = detail.data[0]
147
+ store.store_name # "Drogaria Modelo"
148
+ store.unavailable_since_date # datetime.date(2026, 11, 5)
149
+ store.unavailable_day_count # 19
150
+
151
+ changes = client.products.price_change_detail(top.ean, min_price_change_count=1)
152
+ changes.data[0].changes[0].change_date # datetime.date(2026, 11, 12)
153
+ changes.data[0].changes[0].previous_price # 24.9
154
+ changes.data[0].changes[0].price # 27.9
155
+
156
+ shelf = client.stores.stockouts(store.store_id, available=False) # 2 units per page
157
+ shelf.data[0].market.stockout_store_pct # 7.1: missing here, not across the market
158
+ ```
159
+
160
+ A stockout is a store that sold the product in the window and has it
161
+ unavailable now; a listing that was never available in the window is a product
162
+ the store stopped selling and is counted apart. Rankings are ordered by the
163
+ share of stores involved, because a total only follows how widely a product is
164
+ distributed. The details return events with a date (each price change, each
165
+ promotion period, each gap in stock), not the log of collections.
166
+
167
+ ## Errors
168
+
169
+ ```python
170
+ from cnovatech.datapharma import DataPharma, errors
171
+
172
+ client = DataPharma()
173
+
174
+ try:
175
+ product = client.products.get("0000000000000")
176
+ except errors.EANNotFoundError as exc:
177
+ exc.hint # what the API suggests doing next
178
+ except errors.QuotaExceededError as exc:
179
+ exc.code # "monthly_limit_exceeded"
180
+ exc.upgrade_url # where to change plan
181
+ except errors.APITimeoutError:
182
+ ... # may have been billed; check client.account.usage() before repeating
183
+ except errors.DataPharmaError as exc:
184
+ str(exc) # "HTTP 503 service_unavailable: ... [correlation_id=...]"; never the key
185
+ ```
186
+
187
+ Connection errors, 502/503 and the per-minute 429 are retried with backoff.
188
+ Read timeouts, 504 and quota errors never are: the request may already have
189
+ been processed and billed.
190
+
191
+ ## Async
192
+
193
+ ```python
194
+ import asyncio
195
+
196
+ from cnovatech.datapharma import AsyncDataPharma
197
+
198
+
199
+ async def main() -> None:
200
+ async with AsyncDataPharma() as client:
201
+ product = await client.products.get("7891058001155")
202
+ product.market.median_price # 21.29
203
+
204
+
205
+ asyncio.run(main())
206
+ ```
207
+
208
+ ## pandas
209
+
210
+ ```python
211
+ assortment = client.stores.assortment(store.store_id, limit=1000)
212
+ frame = assortment.to_dataframe() # one row per item; usage and meta as columns
213
+ ```
214
+
215
+ ## Configuration
216
+
217
+ | Argument | Environment variable | Default |
218
+ |---|---|---|
219
+ | `api_key` | `DATAPHARMA_API_KEY` | required, except for `health()` |
220
+ | `base_url` | `DATAPHARMA_BASE_URL` | `https://data.pharma.cnovatech.com.br` |
221
+ | `timeout` | | 150 s read timeout; pass `httpx.Timeout` to set every phase. Applies per request, also through your own `http_client` |
222
+ | `max_retries` | | 2 |
223
+ | `max_units_per_request` | | no limit |
224
+ | `http_client` | | your own `httpx.Client`, for proxies, certificates or a shared pool; never closed, redirects never followed |
225
+
226
+ ## Guides
227
+
228
+ - [Quota and costs](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/docs/quota-and-costs.md): the tariff, what each response reports, estimates and caps.
229
+ - [Pagination](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/docs/pagination.md): pages, walks, `max_pages` and `max_units`.
230
+ - [Errors](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/docs/errors.md): the hierarchy, what is retried, how to handle each case.
231
+ - [Recipes](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/docs/recipes.md): coverage of a list, competitors, exports to pandas, when to use `get` and when to export.
232
+
233
+ ## Logging
234
+
235
+ The SDK logs through the standard `logging` module under the `cnovatech`
236
+ namespace and installs no handler of its own. At DEBUG it records each request's
237
+ method, path, status and duration; retries are logged at INFO, and a WARNING is
238
+ emitted when the remaining quota drops below 10%. Keys, headers and bodies are
239
+ never logged. The `httpx` logger, if the application enables it at INFO, prints
240
+ the full URL with its query string; the key is sent as a header and is not in it.
241
+
242
+ ```python
243
+ import logging
244
+
245
+ logging.basicConfig(level=logging.INFO)
246
+ logging.getLogger("cnovatech").setLevel(logging.DEBUG)
247
+ ```
248
+
249
+ ## Resources
250
+
251
+ | Resource | Methods | Cost |
252
+ |---|---|---|
253
+ | `client.products` | `catalog`, `departments`, `brands`, `by_brand`, `get`, `price_history` | 1 unit |
254
+ | | `price_changes`, `promotions`, `stockouts` (rankings) | 1 unit per page |
255
+ | | `price_change_detail`, `promotion_detail`, `stockout_detail` (one product, store by store) | 1 unit per page of stores |
256
+ | | `coverage` | ceil(codes / 250) units; up to 500 codes per call |
257
+ | `client.stores` | `list`, `find` | 1 unit |
258
+ | | `competitors`, `competitiveness` | 1 unit per competitor |
259
+ | | `assortment`, `compare`, `market_position`, `opportunities`, `promotions`, `stockouts` | ceil(rows / 250) × 2 units per page |
260
+ | `client.brands` | `stores` | 1 unit |
261
+ | `client.account` | `profile`, `usage`, `logs`, `api_keys` | free |
262
+ | `client` | `health`, `usage`, `rate_limit`, `close` | free |
263
+
264
+ Each method's docstring states its cost. The API reference is at
265
+ <https://data.pharma.cnovatech.com.br/redoc>.
266
+
267
+ ## Support
268
+
269
+ - Issues and feature requests: <https://github.com/cnovatech-br/sdks/issues>
270
+ - Email: <suporte@cnovatech.com.br>
271
+
272
+ ## License
273
+
274
+ [MIT](https://github.com/cnovatech-br/sdks/blob/main/LICENSE). The API terms
275
+ of use are at <https://cnovatech.com.br/termos-de-uso>.
@@ -0,0 +1,243 @@
1
+ # cnovatech-datapharma
2
+
3
+ Official Python SDK for the [Cnovatech Data Pharma](https://data.pharma.cnovatech.com.br)
4
+ API: daily prices, availability and assortment of more than 100,000 products
5
+ (medicines, health, beauty, baby and supplements) across the online stores of
6
+ more than 30 Brazilian pharmacies and drugstores in 10 states.
7
+
8
+ - Synchronous and asynchronous clients with the same surface
9
+ - Every response carries its quota cost; pagination can be capped by cost
10
+ - Retries only where a repeat cannot be billed twice
11
+ - Typed end to end: pydantic models, `py.typed`, `mypy --strict`
12
+
13
+ > **Status: 0.x.** Every read endpoint of the API is implemented and tested
14
+ > offline against recorded responses. Until 1.0
15
+ > a minor release may change the public surface; every such change is listed
16
+ > in the
17
+ > [changelog](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/CHANGELOG.md).
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ pip install cnovatech-datapharma
23
+ ```
24
+
25
+ Add `[pandas]` for `to_dataframe()` on list responses. Python 3.10 or newer.
26
+
27
+ ## Quick start
28
+
29
+ ```python
30
+ from cnovatech.datapharma import DataPharma
31
+
32
+ client = DataPharma() # reads DATAPHARMA_API_KEY
33
+
34
+ product = client.products.get("7891058001155", state="SP")
35
+
36
+ product.product_name # "ANALGESICO E ANTITERMICO NOVALGINA 1G DIPIRONA ADULTO 10 COMPRIMIDOS"
37
+ product.market.median_price # 21.29
38
+ product.offers[0].store_name # "Drogaria Modelo"
39
+ product.offers[0].price # 18.9
40
+ product.offers[0].available # True
41
+
42
+ product.usage.query_cost # 1
43
+ product.usage.remaining # 994
44
+ ```
45
+
46
+ Get a key at <https://data.pharma.cnovatech.com.br>. `client.health()` checks
47
+ the API without a key and without cost.
48
+
49
+ The first lookup of a product the API has not cached yet can take up to 90
50
+ seconds; after that it answers in under a second. The default timeout allows
51
+ for it.
52
+
53
+ ## Quota
54
+
55
+ Every call is billed in quota units, and every response says what it cost:
56
+
57
+ ```python
58
+ page = client.products.catalog(search="protetor solar", limit=50)
59
+ page.usage.query_cost # 1
60
+ page.usage.remaining # 993
61
+ client.usage.spent # 2, through this client
62
+ ```
63
+
64
+ Paginated responses can be walked page by page or item by item, with a ceiling
65
+ on how much to spend:
66
+
67
+ ```python
68
+ position = client.stores.market_position(store_id, limit=1000) # 8 units per page
69
+ position.meta.pages # 4
70
+ position.estimate_remaining_cost() # 24, for the three pages not fetched yet
71
+
72
+ items = list(position.iter_all(max_units=40))
73
+ items[0].product_name # "PROTETOR SOLAR FACIAL FPS 60 40G"
74
+ items[0].price_position # "above"
75
+ ```
76
+
77
+ `iter_all` never requests again the page you already paid for. It stops quietly
78
+ at `max_pages` and raises `BudgetExceededError` before a page that would cross
79
+ `max_units`. To refuse any single call above a budget, build the client with
80
+ `DataPharma(max_units_per_request=20)`.
81
+
82
+ ## Stores and competitors
83
+
84
+ ```python
85
+ store = client.stores.find("Drogaria Modelo", state="SP") # 1 unit
86
+ store.store_id # "0f8c7d2e-6b1a-4f3e-9c5d-2a7b8e1f4c63"
87
+
88
+ competitors = client.stores.competitors(store.store_id, limit=5) # 1 unit each
89
+ competitors.data[0].store_name # "Farmácia Vizinha"
90
+ competitors.data[0].overlap_pct # 62.5, share of the assortment in common
91
+
92
+ comparison = client.stores.compare(store.store_id, competitors.data[0].store_id)
93
+ comparison.data[0].product_name # "PROTETOR SOLAR FACIAL FPS 60 40G"
94
+ comparison.data[0].price_position # "above"
95
+ comparison.data[0].price_diff_pct # 12.3
96
+ ```
97
+
98
+ ## Trends: what moved, where and since when
99
+
100
+ Three rankings say which products changed price, went on promotion or went out
101
+ of stock in the last 30 or 90 days. Each has a detail that opens one product
102
+ store by store, and the store resource lists what one store is promoting and
103
+ what is missing from its shelf.
104
+
105
+ ```python
106
+ ranking = client.products.stockouts(brand="MARCA EXEMPLO", min_store_count=10) # 1 unit
107
+ top = ranking.data[0]
108
+ top.product_name # "VITAMINA C EXEMPLO 1G 10 COMPRIMIDOS EFERVESCENTES"
109
+ top.stockout_store_count, top.store_count # 4, 28
110
+ top.stockout_store_pct # 14.3
111
+
112
+ detail = client.products.stockout_detail(top.ean, available=False) # 1 unit
113
+ detail.summary.never_available_store_count # 2, listings left out of the count
114
+ store = detail.data[0]
115
+ store.store_name # "Drogaria Modelo"
116
+ store.unavailable_since_date # datetime.date(2026, 11, 5)
117
+ store.unavailable_day_count # 19
118
+
119
+ changes = client.products.price_change_detail(top.ean, min_price_change_count=1)
120
+ changes.data[0].changes[0].change_date # datetime.date(2026, 11, 12)
121
+ changes.data[0].changes[0].previous_price # 24.9
122
+ changes.data[0].changes[0].price # 27.9
123
+
124
+ shelf = client.stores.stockouts(store.store_id, available=False) # 2 units per page
125
+ shelf.data[0].market.stockout_store_pct # 7.1: missing here, not across the market
126
+ ```
127
+
128
+ A stockout is a store that sold the product in the window and has it
129
+ unavailable now; a listing that was never available in the window is a product
130
+ the store stopped selling and is counted apart. Rankings are ordered by the
131
+ share of stores involved, because a total only follows how widely a product is
132
+ distributed. The details return events with a date (each price change, each
133
+ promotion period, each gap in stock), not the log of collections.
134
+
135
+ ## Errors
136
+
137
+ ```python
138
+ from cnovatech.datapharma import DataPharma, errors
139
+
140
+ client = DataPharma()
141
+
142
+ try:
143
+ product = client.products.get("0000000000000")
144
+ except errors.EANNotFoundError as exc:
145
+ exc.hint # what the API suggests doing next
146
+ except errors.QuotaExceededError as exc:
147
+ exc.code # "monthly_limit_exceeded"
148
+ exc.upgrade_url # where to change plan
149
+ except errors.APITimeoutError:
150
+ ... # may have been billed; check client.account.usage() before repeating
151
+ except errors.DataPharmaError as exc:
152
+ str(exc) # "HTTP 503 service_unavailable: ... [correlation_id=...]"; never the key
153
+ ```
154
+
155
+ Connection errors, 502/503 and the per-minute 429 are retried with backoff.
156
+ Read timeouts, 504 and quota errors never are: the request may already have
157
+ been processed and billed.
158
+
159
+ ## Async
160
+
161
+ ```python
162
+ import asyncio
163
+
164
+ from cnovatech.datapharma import AsyncDataPharma
165
+
166
+
167
+ async def main() -> None:
168
+ async with AsyncDataPharma() as client:
169
+ product = await client.products.get("7891058001155")
170
+ product.market.median_price # 21.29
171
+
172
+
173
+ asyncio.run(main())
174
+ ```
175
+
176
+ ## pandas
177
+
178
+ ```python
179
+ assortment = client.stores.assortment(store.store_id, limit=1000)
180
+ frame = assortment.to_dataframe() # one row per item; usage and meta as columns
181
+ ```
182
+
183
+ ## Configuration
184
+
185
+ | Argument | Environment variable | Default |
186
+ |---|---|---|
187
+ | `api_key` | `DATAPHARMA_API_KEY` | required, except for `health()` |
188
+ | `base_url` | `DATAPHARMA_BASE_URL` | `https://data.pharma.cnovatech.com.br` |
189
+ | `timeout` | | 150 s read timeout; pass `httpx.Timeout` to set every phase. Applies per request, also through your own `http_client` |
190
+ | `max_retries` | | 2 |
191
+ | `max_units_per_request` | | no limit |
192
+ | `http_client` | | your own `httpx.Client`, for proxies, certificates or a shared pool; never closed, redirects never followed |
193
+
194
+ ## Guides
195
+
196
+ - [Quota and costs](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/docs/quota-and-costs.md): the tariff, what each response reports, estimates and caps.
197
+ - [Pagination](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/docs/pagination.md): pages, walks, `max_pages` and `max_units`.
198
+ - [Errors](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/docs/errors.md): the hierarchy, what is retried, how to handle each case.
199
+ - [Recipes](https://github.com/cnovatech-br/sdks/blob/main/datapharma-python/docs/recipes.md): coverage of a list, competitors, exports to pandas, when to use `get` and when to export.
200
+
201
+ ## Logging
202
+
203
+ The SDK logs through the standard `logging` module under the `cnovatech`
204
+ namespace and installs no handler of its own. At DEBUG it records each request's
205
+ method, path, status and duration; retries are logged at INFO, and a WARNING is
206
+ emitted when the remaining quota drops below 10%. Keys, headers and bodies are
207
+ never logged. The `httpx` logger, if the application enables it at INFO, prints
208
+ the full URL with its query string; the key is sent as a header and is not in it.
209
+
210
+ ```python
211
+ import logging
212
+
213
+ logging.basicConfig(level=logging.INFO)
214
+ logging.getLogger("cnovatech").setLevel(logging.DEBUG)
215
+ ```
216
+
217
+ ## Resources
218
+
219
+ | Resource | Methods | Cost |
220
+ |---|---|---|
221
+ | `client.products` | `catalog`, `departments`, `brands`, `by_brand`, `get`, `price_history` | 1 unit |
222
+ | | `price_changes`, `promotions`, `stockouts` (rankings) | 1 unit per page |
223
+ | | `price_change_detail`, `promotion_detail`, `stockout_detail` (one product, store by store) | 1 unit per page of stores |
224
+ | | `coverage` | ceil(codes / 250) units; up to 500 codes per call |
225
+ | `client.stores` | `list`, `find` | 1 unit |
226
+ | | `competitors`, `competitiveness` | 1 unit per competitor |
227
+ | | `assortment`, `compare`, `market_position`, `opportunities`, `promotions`, `stockouts` | ceil(rows / 250) × 2 units per page |
228
+ | `client.brands` | `stores` | 1 unit |
229
+ | `client.account` | `profile`, `usage`, `logs`, `api_keys` | free |
230
+ | `client` | `health`, `usage`, `rate_limit`, `close` | free |
231
+
232
+ Each method's docstring states its cost. The API reference is at
233
+ <https://data.pharma.cnovatech.com.br/redoc>.
234
+
235
+ ## Support
236
+
237
+ - Issues and feature requests: <https://github.com/cnovatech-br/sdks/issues>
238
+ - Email: <suporte@cnovatech.com.br>
239
+
240
+ ## License
241
+
242
+ [MIT](https://github.com/cnovatech-br/sdks/blob/main/LICENSE). The API terms
243
+ of use are at <https://cnovatech.com.br/termos-de-uso>.