xippios-faostat 0.1.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 (50) hide show
  1. xippios_faostat-0.1.0/.gitignore +23 -0
  2. xippios_faostat-0.1.0/CHANGELOG.md +96 -0
  3. xippios_faostat-0.1.0/LICENSE +201 -0
  4. xippios_faostat-0.1.0/PKG-INFO +204 -0
  5. xippios_faostat-0.1.0/README.md +175 -0
  6. xippios_faostat-0.1.0/docs/handover.md +286 -0
  7. xippios_faostat-0.1.0/docs/upstream/FETCHED.md +143 -0
  8. xippios_faostat-0.1.0/docs/upstream/bulk/datasets_E.json +6 -0
  9. xippios_faostat-0.1.0/docs/upstream/openapi/faostat_openapi.yaml +2065 -0
  10. xippios_faostat-0.1.0/docs/upstream/pages/db-terms-of-use.md +61 -0
  11. xippios_faostat-0.1.0/docs/upstream/samples/codes_element_QCL.json +1 -0
  12. xippios_faostat-0.1.0/docs/upstream/samples/data_QCL_231_2510_15_2015-2021.json +1 -0
  13. xippios_faostat-0.1.0/docs/upstream/samples/dimensions_QCL.json +1 -0
  14. xippios_faostat-0.1.0/docs/upstream/samples/groupsanddomains.json +1 -0
  15. xippios_faostat-0.1.0/pyproject.toml +107 -0
  16. xippios_faostat-0.1.0/src/xippios/faostat/__init__.py +28 -0
  17. xippios_faostat-0.1.0/src/xippios/faostat/_client.py +426 -0
  18. xippios_faostat-0.1.0/src/xippios/faostat/_version.py +10 -0
  19. xippios_faostat-0.1.0/src/xippios/faostat/api/__init__.py +18 -0
  20. xippios_faostat-0.1.0/src/xippios/faostat/api/auth_login.py +51 -0
  21. xippios_faostat-0.1.0/src/xippios/faostat/api/datasets_e.py +59 -0
  22. xippios_faostat-0.1.0/src/xippios/faostat/api/get_code_list.py +84 -0
  23. xippios_faostat-0.1.0/src/xippios/faostat/api/get_data.py +132 -0
  24. xippios_faostat-0.1.0/src/xippios/faostat/api/get_groupsanddomains.py +53 -0
  25. xippios_faostat-0.1.0/src/xippios/faostat/enums.py +3 -0
  26. xippios_faostat-0.1.0/src/xippios/faostat/errors.py +11 -0
  27. xippios_faostat-0.1.0/src/xippios/faostat/http.py +18 -0
  28. xippios_faostat-0.1.0/src/xippios/faostat/mcp.py +33 -0
  29. xippios_faostat-0.1.0/src/xippios/faostat/models.py +557 -0
  30. xippios_faostat-0.1.0/src/xippios/faostat/params.py +102 -0
  31. xippios_faostat-0.1.0/src/xippios/faostat/parse/__init__.py +11 -0
  32. xippios_faostat-0.1.0/src/xippios/faostat/parse/bulk_csv.py +150 -0
  33. xippios_faostat-0.1.0/src/xippios/faostat/py.typed +0 -0
  34. xippios_faostat-0.1.0/src/xippios/faostat/reference/README.md +66 -0
  35. xippios_faostat-0.1.0/src/xippios/faostat/reference/area_FBS.json +1242 -0
  36. xippios_faostat-0.1.0/src/xippios/faostat/reference/area_PP.json +1172 -0
  37. xippios_faostat-0.1.0/src/xippios/faostat/reference/area_QCL.json +1397 -0
  38. xippios_faostat-0.1.0/src/xippios/faostat/reference/area_RL.json +1612 -0
  39. xippios_faostat-0.1.0/src/xippios/faostat/reference/area_TCL.json +1592 -0
  40. xippios_faostat-0.1.0/src/xippios/faostat/reference/element_FBS.json +112 -0
  41. xippios_faostat-0.1.0/src/xippios/faostat/reference/element_PP.json +27 -0
  42. xippios_faostat-0.1.0/src/xippios/faostat/reference/element_QCL.json +32 -0
  43. xippios_faostat-0.1.0/src/xippios/faostat/reference/element_RL.json +61 -0
  44. xippios_faostat-0.1.0/src/xippios/faostat/reference/element_TCL.json +31 -0
  45. xippios_faostat-0.1.0/src/xippios/faostat/reference/item_FBS.json +742 -0
  46. xippios_faostat-0.1.0/src/xippios/faostat/reference/item_PP.json +1302 -0
  47. xippios_faostat-0.1.0/src/xippios/faostat/reference/item_QCL.json +2035 -0
  48. xippios_faostat-0.1.0/src/xippios/faostat/reference/item_RL.json +343 -0
  49. xippios_faostat-0.1.0/src/xippios/faostat/reference/item_TCL.json +4051 -0
  50. xippios_faostat-0.1.0/src/xippios/faostat/tasks.py +741 -0
@@ -0,0 +1,23 @@
1
+ # environments and caches
2
+ .venv/
3
+ __pycache__/
4
+ *.py[cod]
5
+ .mypy_cache/
6
+ .ruff_cache/
7
+ .pytest_cache/
8
+ .hypothesis/
9
+
10
+ # build artefacts
11
+ dist/
12
+ build/
13
+ *.egg-info/
14
+
15
+ # local secrets are never files, but belt and braces
16
+ .env
17
+ .env.*
18
+ .envrc
19
+ .envrc.*
20
+
21
+ # editors
22
+ .idea/
23
+ .vscode/
@@ -0,0 +1,96 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
5
+ [Semantic Versioning](https://semver.org/) (pre-1.0: breaking changes bump the minor).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2026-10-29
10
+
11
+ First release, on xippios-core 0.3.0 from PyPI (`~=0.3.0`), sdk-template v0.3.0 and the
12
+ family's conventions 1.22. It wraps FAOSTAT, the statistical database of the Food and
13
+ Agriculture Organization of the United Nations (FAO): its data API, which needs a FAO
14
+ account, and its bulk downloads, which need none. The client logs in with the account
15
+ (`FAOSTAT_USERNAME`, `FAOSTAT_PASSWORD`) once per process, sends the access token as a
16
+ bearer, and logs in again when the token lapses or a request is refused with HTTP 401 or
17
+ 403. `get_data` reads a domain's rows, `list_domains` lists the domains with their
18
+ release dates, and `get_series` reads a yearly series in one request. Fifteen code
19
+ tables ship with this version, the area, element and item codes of five domains (QCL,
20
+ TCL, FBS, PP and RL) as fetched on 2026-10-29: `list_areas`, `list_elements` and
21
+ `list_items` read them without a request, and `get_data` refuses before sending a code
22
+ the domain's table lacks, unless the query names another coding system. FAOSTAT has two
23
+ element code systems: a query filters by the codes of the domain's element list (2510,
24
+ Production Quantity, in QCL) and the rows print others (5510, Production); the tables
25
+ hold the codes a query takes, and nothing is translated between the two.
26
+ `list_bulk_datasets` reads the bulk downloads' catalogue; the bulk task
27
+ `download_dataset`, in Python only, with no MCP tool and no route, streams a dataset's
28
+ ZIP to disk, and `parse.parse_bulk_csv` reads its table row by row. The other seven
29
+ tasks are MCP tools and HTTP routes. The client keeps a self-imposed pace of 30 requests
30
+ per minute, since FAOSTAT documents no rate limit, and caches its answers of HTTP 200
31
+ for five minutes. The documentation snapshot of 2026-10-29 under `docs/upstream/`, FAO's
32
+ OpenAPI document among it, is the reference for names and semantics.
33
+
34
+ ### Added
35
+
36
+ - Scaffold from [sdk-template](https://github.com/xippios/sdk-template): configured
37
+ client for FAOSTAT, MCP server and FastAPI router wired to the task layer, test
38
+ harness with core's family check, CI, the nightly live tests and the release train, in
39
+ which a release PR's merge tags and publishes.
40
+ - The documentation snapshot in `docs/upstream/`, captured on 2026-10-29 by
41
+ `scripts/refresh_upstream.py`: FAO's OpenAPI document, the FAO Statistical Database
42
+ Terms of Use, the bulk downloads' catalogue and four structural samples of the API's
43
+ answers; the README names the FAO account the API needs and the data's licence,
44
+ CC BY 4.0.
45
+ - The FAO account's login: the client logs in once per process with `FAOSTAT_USERNAME`
46
+ and `FAOSTAT_PASSWORD` (`auth_login`, `POST /auth/login`), sends the access token as a
47
+ bearer, and logs in again when the token lapses or a request is refused with HTTP 401
48
+ or 403, once per request; a missing variable is `ConfigError`, a login that refuses the
49
+ credentials `AuthError`, any other failed login core's mapping (`RateLimitError`,
50
+ `UpstreamError`). Only the login's answer is read by the login; every other answer goes
51
+ back to the client unread. Neither the credentials nor a token is logged; the access
52
+ token alone is kept, masked.
53
+ - `get_data(domain, *, area, item, element, year, area_cs, item_cs, page_size,
54
+ page_number)`, a domain's rows from FAOSTAT's data endpoint, each filter a code or a
55
+ list of codes, every row's value a number where upstream's text is one, every column a
56
+ domain prints beyond the named ones kept in `columns` under upstream's header (a code
57
+ column that `area_cs` or `item_cs` renames among them, `area_code` or `item_code` then
58
+ `None`); a query without a filter is refused before sending, an unknown domain is
59
+ `NotFoundError`; `meta.vintage` is the domain's last update as `groupsanddomains` prints it,
60
+ `meta.published_at` stays empty. Tool `faostat_get_data`.
61
+ - `list_domains(*, search)`, FAOSTAT's domains and subgroups with their groups and release
62
+ dates (`groupsanddomains`). Tool `faostat_list_domains`.
63
+ - The wrappers `api.auth_login`, `api.get_data` and `api.get_groupsanddomains`, their
64
+ recorded fixtures, and the nightly live run with the account's two org secrets.
65
+ - Fifteen packaged code tables under `reference/`, the area, element and item codes of
66
+ QCL, TCL, FBS, PP and RL as FAOSTAT's `getCodeList` lists them, fetched on 2026-10-29 by
67
+ `scripts/refresh_reference.py` through the new wrapper `api.get_code_list`.
68
+ - `list_areas(domain, *, search)`, `list_elements(domain, *, search)` and
69
+ `list_items(domain, *, search)`, a domain's codes from the packaged tables, without a
70
+ request, `search` over code and label; the element lists hold the codes a data query
71
+ filters by, not those the rows print. Tools `faostat_list_areas`,
72
+ `faostat_list_elements` and `faostat_list_items`, up to 1,000 rows each.
73
+ - `get_data` refuses an area, element or item code the packaged table of QCL, TCL, FBS,
74
+ PP or RL lacks with `UnknownCode` before sending, naming the table, its fetch date and
75
+ the closest codes (`element=5510` on QCL names 2510, Production Quantity), since
76
+ FAOSTAT answers an unknown code with no rows; another domain's codes are sent unchecked.
77
+ - `get_series(domain, area, item, element, *, since, until)`, one `get_data` request for
78
+ a yearly series: the years `since` to `until` as upstream's `year` list, or no year
79
+ filter when both are left out, every year the domain holds; one without the other, or
80
+ `since` after `until`, is refused before sending. Rows in upstream's order, `get_data`'s
81
+ code checks and `meta`, `since` and `until` added to `meta.request`. Tool
82
+ `faostat_get_series`, 200 rows with a note naming `since` and `until`.
83
+ - `list_bulk_datasets(*, search)`, the datasets FAOSTAT publishes as files, from the bulk
84
+ downloads' catalogue `datasets_E.json` on `bulks-faostat.fao.org`, fetched without the
85
+ account through the new wrapper `api.datasets_e`: each dataset's code, name, last update,
86
+ ZIP size, rows and URL; `search` over code and name. Tool `faostat_list_bulk_datasets`,
87
+ up to 100 rows and 160 KiB, the whole catalogue uncut, its note naming `search`.
88
+ - `download_dataset(domain, dest)`, a bulk task, Python only: the dataset's ZIP, as the
89
+ catalogue names it, streamed to `dest` without the account, never held in memory;
90
+ `meta.vintage` the catalogue's last update as printed and `meta.published_at` the same
91
+ time, which the catalogue prints without a zone.
92
+ - `parse_bulk_csv(path)` in `xippios.faostat.parse`, the ZIP's normalized table
93
+ (`<Name>_E_All_Data_(Normalized).csv`) read row by row straight from the archive, each
94
+ row a `BulkRecord`: the columns of the production files named, every other column a
95
+ dataset prints kept under its header in `columns`, `value` a number where its text is
96
+ one.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,204 @@
1
+ Metadata-Version: 2.5
2
+ Name: xippios-faostat
3
+ Version: 0.1.0
4
+ Summary: Typed client, MCP tools and HTTP routes for FAOSTAT
5
+ Project-URL: Homepage, https://github.com/xippios/sdk-faostat
6
+ Project-URL: Upstream, https://www.fao.org/faostat/
7
+ Project-URL: Changelog, https://github.com/xippios/sdk-faostat/blob/main/CHANGELOG.md
8
+ Project-URL: Issues, https://github.com/xippios/sdk-faostat/issues
9
+ Author: XIPPIOS GmbH
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: faostat,httpx,mcp,pydantic,sdk
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Typing :: Typed
20
+ Requires-Python: >=3.12
21
+ Requires-Dist: xippios-core~=0.3.0
22
+ Provides-Extra: http
23
+ Requires-Dist: xippios-core[http]~=0.3.0; extra == 'http'
24
+ Provides-Extra: mcp
25
+ Requires-Dist: xippios-core[mcp]~=0.3.0; extra == 'mcp'
26
+ Provides-Extra: testing
27
+ Requires-Dist: xippios-core[testing]~=0.3.0; extra == 'testing'
28
+ Description-Content-Type: text/markdown
29
+
30
+ # xippios-faostat
31
+
32
+ Typed Python client, MCP server and FastAPI routes for
33
+ [FAOSTAT](https://www.fao.org/faostat/), part of the
34
+ [xippios SDK family](https://github.com/xippios). Names and semantics follow the
35
+ upstream documentation; every response carries release metadata (source, endpoint,
36
+ retrieved_at, published_at, vintage).
37
+
38
+ ## Install
39
+
40
+ ```bash
41
+ uv add xippios-faostat # client only
42
+ uv add "xippios-faostat[mcp]" # + MCP server
43
+ uv add "xippios-faostat[http]" # + FastAPI router
44
+ ```
45
+
46
+ A FAO account is needed: `FAOSTAT_USERNAME` (the account's e-mail) and
47
+ `FAOSTAT_PASSWORD` in the environment. The client logs in with them on its first request
48
+ (`POST /auth/login`, FAOSTAT's `auth_login`), once per process, sends the access token
49
+ with every request to the API, and logs in again when the token lapses (after an hour) or a request
50
+ is refused with HTTP 401 or 403. Neither the credentials nor a token is logged, and the
51
+ tasks return neither. The bulk downloads need none.
52
+
53
+ ## 30-second example
54
+
55
+ ```python
56
+ from xippios.faostat import get_data, get_series, list_domains, list_elements
57
+
58
+ wheat = get_data("QCL", area="231", element="2510", item="15", year=range(2015, 2022))
59
+ row = wheat.data[0] # wheat production in the United States of America, 2015
60
+ row.area, row.item, row.year # ("United States of America", "Wheat", "2015")
61
+ row.element_code, row.element # ("5510", "Production"): the answer's code, not the 2510 sent
62
+ row.value, row.unit, row.flag_description # (Decimal("55839540"), "t", "Official value")
63
+ wheat.meta.vintage # "2025-12-31": QCL's last update, as list_domains prints it
64
+ wheat.meta.request # {"lang": "en", "domain_code": "QCL", "area": "231", ...}
65
+
66
+ series = get_series("QCL", "231", "15", "2510", since=2015, until=2021) # the same seven rows
67
+ series.meta.request["year"], series.meta.request["since"] # ("2015,2016,...,2021", 2015)
68
+
69
+ list_domains(search="prices").data # PP, CP, PD, PE (group Prices) and PA, an archive
70
+ list_elements("QCL", search="production").data[0] # code "2510", "Production Quantity"
71
+ get_data("QCL", area="231", element="5510") # UnknownCode: did you mean 2510 (Production Quantity)?
72
+ ```
73
+
74
+ `get_data(domain, *, area, item, element, year, area_cs, item_cs, page_size,
75
+ page_number)` sends one request to FAOSTAT's data endpoint (`get_data`, `GET
76
+ /{lang}/data/{domain_code}`), in English, with the output options the portal sends (codes,
77
+ units, flags and notes shown); each filter is a code or a list of codes. At least one of
78
+ `area`, `item`, `element` and `year` is needed: FAOSTAT refuses a query without a filter,
79
+ and so does `get_data`, before sending. Without `page_size` every matching row comes in
80
+ one answer; with `page_size` and `page_number`, one page. A query filters by the element
81
+ codes of the domain's element list (2510, Production Quantity), and the rows print other
82
+ codes (5510, Production). FAOSTAT answers a code it does not know with no rows, so for
83
+ the five domains whose code tables ship with the package, QCL, TCL, FBS, PP and RL,
84
+ `get_data` checks every area, element and item code before sending and refuses one the
85
+ domain's table lacks with `UnknownCode`, naming the table, its fetch date and the
86
+ closest codes; another domain's codes are sent unchecked, and a code that matches nothing
87
+ there gives an empty list. An unknown domain raises `NotFoundError`. `meta.vintage` is
88
+ the domain's `date_update` from `list_domains` (`groupsanddomains`); `meta.published_at`
89
+ stays `None`, since a date is no release time.
90
+
91
+ `get_series(domain, area, item, element, *, since, until)` is one `get_data` request
92
+ for a yearly series: `since` and `until` become the year filter, the years from one to
93
+ the other as upstream's comma list, or no year filter when both are left out, which
94
+ answers every year the domain holds; one without the other is refused before sending.
95
+ The rows come in upstream's order, area, item, year ascending in the recorded answers,
96
+ and a year the domain does not hold has no row. The codes are checked as `get_data` checks them, and `meta` is
97
+ `get_data`'s, with `since` and `until` added to `meta.request`.
98
+
99
+ `list_areas(domain, *, search)`, `list_elements(domain, *, search)` and
100
+ `list_items(domain, *, search)` list the codes those filters take, for the same five
101
+ domains, from the package's copies of FAOSTAT's code lists (`getCodeList`,
102
+ `codes/<dimension>/<domain>`, `src/xippios/faostat/reference/`), without a request;
103
+ `search` keeps the codes whose code or label contains the text. The element lists hold
104
+ the filter codes, not the codes the rows print. `meta.retrieved_at` is when the package
105
+ fetched the list.
106
+
107
+ ### Bulk downloads
108
+
109
+ ```python
110
+ from pathlib import Path
111
+
112
+ from xippios.faostat import download_dataset, list_bulk_datasets
113
+ from xippios.faostat.parse import parse_bulk_csv
114
+
115
+ list_bulk_datasets(search="land use").data[0] # dataset_code "RL", file_size "2902KB", ...
116
+ land = download_dataset("RL", Path("RL.zip")) # 3 MB; TCL's ZIP is 267 MB
117
+ land.data.path # PosixPath("RL.zip"): the ZIP as FAOSTAT serves it
118
+ land.meta.vintage # "2026-10-02T00:00:00": the catalogue's DateUpdate as printed
119
+ land.meta.published_at # 2026-10-02 00:00 without a zone, meta.published_at_naive set
120
+ rows = parse_bulk_csv(land.data.path) # one BulkRecord per row of its table, read lazily
121
+ first = next(rows)
122
+ first.area, first.area_code_m49, first.item, first.year # ("Afghanistan", "'004", ...)
123
+ first.value, first.unit # (Decimal("65286.000000"), "1000 ha")
124
+ ```
125
+
126
+ `list_bulk_datasets(*, search)` lists the datasets FAOSTAT publishes as files, 69 on
127
+ 2026-10-29, from the bulk downloads' catalogue (`production/datasets_E.json` on
128
+ `bulks-faostat.fao.org`), fetched without the account: each with its code, the domain
129
+ code `get_data` takes, its name, last update, the ZIP's size and URL and the rows of its
130
+ table; `search` keeps the datasets whose code or name contains the text.
131
+
132
+ `download_dataset(domain, dest)` looks the dataset up in the catalogue and streams its
133
+ ZIP to `dest`, without the account, never holding it in memory or caching it; it returns
134
+ core's `Downloaded` (path, size, content type, `ETag`, `Last-Modified`, retrieval time).
135
+ It is a bulk task: Python only, so the MCP server and the router skip it and no agent is
136
+ handed a file path. The files run from 12 KB (MDDW) to 410 MB (TM); TCL's is 267 MB.
137
+ `meta.vintage` is the catalogue's `DateUpdate` as printed and `meta.published_at` the
138
+ same time, which the catalogue prints without a zone. `parse_bulk_csv(path)` reads the
139
+ ZIP's normalized table, the member `<Name>_E_All_Data_(Normalized).csv`, row by row,
140
+ straight from the archive: each `BulkRecord` has the columns of the production files
141
+ (`area_code`, `area_code_m49` as printed with its apostrophe, `area`, `item_code`,
142
+ `item_code_cpc`, `item`, `element_code`, `element`, `year_code`, `year`, `unit`,
143
+ `value` as a `Decimal`, `flag`, `note`), `None` where a dataset does not print one, and
144
+ every other column of a dataset under its header in `columns`, as MDDW's surveys and
145
+ food groups.
146
+
147
+ ## MCP
148
+
149
+ ```json
150
+ {
151
+ "mcpServers": {
152
+ "faostat": {
153
+ "command": "uvx",
154
+ "args": ["--from", "xippios-faostat[mcp]", "xippios-faostat-mcp"]
155
+ }
156
+ }
157
+ }
158
+ ```
159
+
160
+ Tools are read-only and named `faostat_<verb>_<noun>`. Results are capped, at
161
+ 200 rows or 64 KB unless a tool has a cap of its own, with a note on how to see the rest.
162
+ `--http` serves streamable HTTP instead of stdio.
163
+
164
+ ## Status
165
+
166
+ <!-- Core's family check (tests/test_family.py) reads this table: keep the "## Status"
167
+ heading and the "Task function" column. Each row's Task function cell starts with the
168
+ task's name in backticks, alone or as its call (`get_data(code, year)`); several tasks in
169
+ one row are backticked names separated by commas; prose may follow. The tables list
170
+ exactly tasks.__all__. -->
171
+
172
+ | Upstream operation | Task function | MCP tool | Status |
173
+ | --- | --- | --- | --- |
174
+ | `get_data` (`GET /{lang}/data/{domain_code}`), the release date from `get_groupsanddomains` | `get_data(domain, area, item, element, year, area_cs, item_cs, page_size, page_number)`, a domain's rows | `faostat_get_data` | alpha |
175
+ | `get_data` with a `year` list, the release date from `get_groupsanddomains` | `get_series(domain, area, item, element, since, until)`, one domain's series over a year range | `faostat_get_series` | alpha |
176
+ | `get_groupsanddomains` (`GET /{lang}/groupsanddomains`) | `list_domains(search)`, the domains and subgroups with their groups and release dates | `faostat_list_domains` | alpha |
177
+ | `getCodeList` (`GET /{lang}/codes/{dimension_id}/{domain_code}`), `area`, packaged for QCL, TCL, FBS, PP and RL | `list_areas(domain, search)`, a domain's area codes | `faostat_list_areas` | alpha |
178
+ | `getCodeList`, `element`, packaged for the same five domains | `list_elements(domain, search)`, a domain's element codes, the filter codes | `faostat_list_elements` | alpha |
179
+ | `getCodeList`, `item`, packaged for the same five domains | `list_items(domain, search)`, a domain's item codes | `faostat_list_items` | alpha |
180
+ | `production/datasets_E.json` on `bulks-faostat.fao.org`, the bulk downloads' catalogue | `list_bulk_datasets(search)`, the datasets FAOSTAT publishes as files, with sizes and last updates | `faostat_list_bulk_datasets` | alpha |
181
+ | The catalogue's `FileLocation`, a dataset's ZIP on `bulks-faostat.fao.org` | `download_dataset(domain, dest)`, a dataset's ZIP on disk, its normalized CSV read by `parse_bulk_csv` | none: a bulk task, Python only, which the adapters skip | alpha |
182
+
183
+ Every request to the API logs in first where the process has no current token
184
+ (`auth_login`, `POST /auth/login`); the login is the client's, no task's. The bulk
185
+ downloads' catalogue and files are fetched without it.
186
+
187
+ ## Upstream terms and attribution
188
+
189
+ Data comes from FAOSTAT (https://www.fao.org/faostat/) under the
190
+ [FAO Statistical Database Terms of Use](https://www.fao.org/contact-us/terms/db-terms-of-use/en/):
191
+ unless their metadata or webpage say otherwise, the datasets FAO's corporate statistical
192
+ databases disseminate, FAOSTAT among them (the terms' Annex 1), are licensed under the
193
+ Creative Commons Attribution-4.0 International licence (CC BY 4.0), as complemented by
194
+ those terms. The terms ask for this citation:
195
+
196
+ > FAO. [YYYY (year of last update)]. [Name of database: Name of dataset OR Name of
197
+ > database]. [Accessed on [DD Month YYYY]]. [URL] Licence: CC-BY-4.0.
198
+
199
+ This package adds no rights to the data. Documentation snapshot and its attribution:
200
+ `docs/upstream/FETCHED.md`.
201
+
202
+ ## Licence
203
+
204
+ Apache-2.0. See [LICENSE](LICENSE).