t3api-utils 1.5.0__tar.gz → 2.0.1__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 (102) hide show
  1. t3api_utils-2.0.1/PKG-INFO +123 -0
  2. t3api_utils-2.0.1/README.md +95 -0
  3. t3api_utils-2.0.1/pyproject.toml +100 -0
  4. t3api_utils-2.0.1/pyproject.toml.orig +86 -0
  5. t3api_utils-2.0.1/t3api_utils/__init__.py +99 -0
  6. t3api_utils-2.0.1/t3api_utils/_api.py +85 -0
  7. t3api_utils-2.0.1/t3api_utils/_console.py +186 -0
  8. t3api_utils-2.0.1/t3api_utils/_http.py +320 -0
  9. t3api_utils-2.0.1/t3api_utils/_paths.py +19 -0
  10. t3api_utils-2.0.1/t3api_utils/_ratelimit.py +52 -0
  11. t3api_utils-2.0.1/t3api_utils/auth/__init__.py +0 -0
  12. t3api_utils-2.0.1/t3api_utils/auth/credentials.py +128 -0
  13. t3api_utils-2.0.1/t3api_utils/auth/login.py +234 -0
  14. t3api_utils-2.0.1/t3api_utils/auth/resolve.py +178 -0
  15. t3api_utils-2.0.1/t3api_utils/auth/secret_keys.py +132 -0
  16. t3api_utils-2.0.1/t3api_utils/auth/states.py +121 -0
  17. t3api_utils-2.0.1/t3api_utils/auth/store.py +234 -0
  18. t3api_utils-2.0.1/t3api_utils/cli/__init__.py +5 -0
  19. t3api_utils-2.0.1/t3api_utils/cli/main.py +655 -0
  20. t3api_utils-2.0.1/t3api_utils/client.py +940 -0
  21. t3api_utils-2.0.1/t3api_utils/errors.py +227 -0
  22. t3api_utils-2.0.1/t3api_utils/local.py +261 -0
  23. t3api_utils-2.0.1/t3api_utils/pickers.py +145 -0
  24. t3api_utils-2.0.1/t3api_utils/prompts.py +71 -0
  25. t3api_utils-2.0.1/t3api_utils/py.typed +0 -0
  26. t3api_utils-2.0.1/t3api_utils/reports.py +179 -0
  27. t3api_utils-2.0.1/t3api_utils/spec/__init__.py +6 -0
  28. t3api_utils-2.0.1/t3api_utils/spec/loader.py +208 -0
  29. t3api_utils-2.0.1/t3api_utils/spec/openapi.json.gz +0 -0
  30. t3api_utils-2.0.1/t3api_utils/spec/registry.py +399 -0
  31. t3api_utils-2.0.1/t3api_utils/tag_lists.py +342 -0
  32. t3api_utils-2.0.1/t3api_utils/tui/__init__.py +0 -0
  33. t3api_utils-2.0.1/t3api_utils/tui/inspector.py +150 -0
  34. t3api_utils-2.0.1/t3api_utils/writes.py +407 -0
  35. t3api_utils-1.5.0/LICENSE +0 -674
  36. t3api_utils-1.5.0/PKG-INFO +0 -141
  37. t3api_utils-1.5.0/README.md +0 -106
  38. t3api_utils-1.5.0/pyproject.toml +0 -89
  39. t3api_utils-1.5.0/setup.cfg +0 -4
  40. t3api_utils-1.5.0/t3api_utils/__init__.py +0 -18
  41. t3api_utils-1.5.0/t3api_utils/api/__init__.py +0 -8
  42. t3api_utils-1.5.0/t3api_utils/api/client.py +0 -209
  43. t3api_utils-1.5.0/t3api_utils/api/interfaces.py +0 -77
  44. t3api_utils-1.5.0/t3api_utils/api/operations.py +0 -357
  45. t3api_utils-1.5.0/t3api_utils/api/parallel.py +0 -481
  46. t3api_utils-1.5.0/t3api_utils/auth/__init__.py +0 -5
  47. t3api_utils-1.5.0/t3api_utils/auth/interfaces.py +0 -28
  48. t3api_utils-1.5.0/t3api_utils/auth/utils.py +0 -675
  49. t3api_utils-1.5.0/t3api_utils/cli/__init__.py +0 -1
  50. t3api_utils-1.5.0/t3api_utils/cli/consts.py +0 -155
  51. t3api_utils-1.5.0/t3api_utils/cli/utils.py +0 -716
  52. t3api_utils-1.5.0/t3api_utils/collection/__init__.py +0 -1
  53. t3api_utils-1.5.0/t3api_utils/collection/utils.py +0 -100
  54. t3api_utils-1.5.0/t3api_utils/db/__init__.py +0 -1
  55. t3api_utils-1.5.0/t3api_utils/db/consts.py +0 -13
  56. t3api_utils-1.5.0/t3api_utils/db/utils.py +0 -248
  57. t3api_utils-1.5.0/t3api_utils/exceptions.py +0 -5
  58. t3api_utils-1.5.0/t3api_utils/file/__init__.py +0 -1
  59. t3api_utils-1.5.0/t3api_utils/file/consts.py +0 -17
  60. t3api_utils-1.5.0/t3api_utils/file/utils.py +0 -206
  61. t3api_utils-1.5.0/t3api_utils/http/__init__.py +0 -1
  62. t3api_utils-1.5.0/t3api_utils/http/utils.py +0 -1067
  63. t3api_utils-1.5.0/t3api_utils/inspector/__init__.py +0 -5
  64. t3api_utils-1.5.0/t3api_utils/inspector/app.py +0 -341
  65. t3api_utils-1.5.0/t3api_utils/interfaces.py +0 -6
  66. t3api_utils-1.5.0/t3api_utils/intro.py +0 -650
  67. t3api_utils-1.5.0/t3api_utils/logging.py +0 -69
  68. t3api_utils-1.5.0/t3api_utils/main/__init__.py +0 -1
  69. t3api_utils-1.5.0/t3api_utils/main/handlers.py +0 -310
  70. t3api_utils-1.5.0/t3api_utils/main/utils.py +0 -1695
  71. t3api_utils-1.5.0/t3api_utils/openapi/__init__.py +0 -6
  72. t3api_utils-1.5.0/t3api_utils/openapi/collection_picker.py +0 -151
  73. t3api_utils-1.5.0/t3api_utils/openapi/spec_fetcher.py +0 -241
  74. t3api_utils-1.5.0/t3api_utils/style/__init__.py +0 -67
  75. t3api_utils-1.5.0/t3api_utils/style/console.py +0 -19
  76. t3api_utils-1.5.0/t3api_utils/style/messages.py +0 -140
  77. t3api_utils-1.5.0/t3api_utils/style/styles.py +0 -67
  78. t3api_utils-1.5.0/t3api_utils.egg-info/PKG-INFO +0 -141
  79. t3api_utils-1.5.0/t3api_utils.egg-info/SOURCES.txt +0 -66
  80. t3api_utils-1.5.0/t3api_utils.egg-info/dependency_links.txt +0 -1
  81. t3api_utils-1.5.0/t3api_utils.egg-info/requires.txt +0 -18
  82. t3api_utils-1.5.0/t3api_utils.egg-info/top_level.txt +0 -1
  83. t3api_utils-1.5.0/tests/test_api_auth.py +0 -300
  84. t3api_utils-1.5.0/tests/test_api_client.py +0 -206
  85. t3api_utils-1.5.0/tests/test_api_interfaces.py +0 -178
  86. t3api_utils-1.5.0/tests/test_api_operations.py +0 -461
  87. t3api_utils-1.5.0/tests/test_api_parallel.py +0 -455
  88. t3api_utils-1.5.0/tests/test_auth.py +0 -224
  89. t3api_utils-1.5.0/tests/test_cli.py +0 -430
  90. t3api_utils-1.5.0/tests/test_collection.py +0 -72
  91. t3api_utils-1.5.0/tests/test_db.py +0 -444
  92. t3api_utils-1.5.0/tests/test_exceptions.py +0 -258
  93. t3api_utils-1.5.0/tests/test_files.py +0 -81
  94. t3api_utils-1.5.0/tests/test_http.py +0 -1097
  95. t3api_utils-1.5.0/tests/test_inspector.py +0 -557
  96. t3api_utils-1.5.0/tests/test_interfaces.py +0 -325
  97. t3api_utils-1.5.0/tests/test_logging.py +0 -281
  98. t3api_utils-1.5.0/tests/test_main.py +0 -1089
  99. t3api_utils-1.5.0/tests/test_openapi.py +0 -295
  100. t3api_utils-1.5.0/tests/test_openapi_integration.py +0 -123
  101. t3api_utils-1.5.0/tests/test_smoke.py +0 -33
  102. t3api_utils-1.5.0/tests/test_style.py +0 -209
@@ -0,0 +1,123 @@
1
+ Metadata-Version: 2.4
2
+ Name: t3api-utils
3
+ Version: 2.0.1
4
+ Summary: Secret-key auth, collection loading, reports and tag lists for the T3 API
5
+ Keywords: t3,metrc,track and trace,cannabis,api
6
+ Author: Matt Frisbie
7
+ Author-email: Matt Frisbie <matt@classvsoftware.com>
8
+ License-Expression: GPL-3.0-only
9
+ Classifier: Development Status :: 5 - Production/Stable
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Typing :: Typed
15
+ Requires-Dist: httpx2>=2.16
16
+ Requires-Dist: rich>=13.0
17
+ Requires-Dist: typer>=0.16
18
+ Requires-Dist: keyring>=25.0
19
+ Requires-Dist: platformdirs>=4.0
20
+ Requires-Dist: pyotp>=2.8
21
+ Requires-Dist: tomli-w>=1.0
22
+ Requires-Dist: textual>=6.0 ; extra == 'tui'
23
+ Requires-Python: >=3.12
24
+ Project-URL: Homepage, https://github.com/classvsoftware/t3api-python-utils
25
+ Project-URL: Documentation, https://classvsoftware.github.io/t3api-python-utils/
26
+ Provides-Extra: tui
27
+ Description-Content-Type: text/markdown
28
+
29
+ # t3api-utils
30
+
31
+ A Python library and `t3api` command for the [T3 API](https://api.trackandtrace.tools/v2/docs)
32
+ (Track & Trace Tools), which serves Metrc cannabis-compliance data. It handles
33
+ login and secret keys, loads whole collections, sends writes safely (dry run
34
+ unless you say otherwise), downloads server-side reports and builds tag lists.
35
+
36
+ **Documentation: <https://classvsoftware.github.io/t3api-python-utils/>**
37
+
38
+ > 2.0 is a rewrite and doesn't share an API with 1.x. See
39
+ > [Migrating from 1.x](https://classvsoftware.github.io/t3api-python-utils/migration/).
40
+ > 1.x is frozen at 1.5.0.
41
+
42
+ ## Install
43
+
44
+ Python 3.12 or later.
45
+
46
+ ```bash
47
+ uv add t3api-utils
48
+ pip install "t3api-utils[tui]" # [tui] adds the terminal row inspector
49
+ ```
50
+
51
+ ## Quickstart
52
+
53
+ Log in once. This creates a T3 secret key from your Metrc login and saves it to
54
+ your OS keyring; your Metrc password isn't stored.
55
+
56
+ ```bash
57
+ t3api login
58
+ ```
59
+
60
+ Then, in a script:
61
+
62
+ ```python
63
+ import t3api_utils as t3
64
+
65
+ client = t3.connect()
66
+ items = client.collect("METRC_ACTIVE_ITEMS", license="LIC-00001")
67
+ t3.to_csv(items, "items.csv")
68
+ ```
69
+
70
+ For scheduled jobs and CI, set `T3_SECRET_KEY` instead of logging in.
71
+
72
+ A few more things it does:
73
+
74
+ ```python
75
+ # Writes are a dry run unless submit=True
76
+ client.post("/v2/items/discontinue", license="LIC-00001", json={"id": 12345}, submit=True)
77
+
78
+ # Validate every row, show the results, and ask before submitting
79
+ client.bulk("/v2/items/discontinue", rows=[{"id": i["id"]} for i in items], license="LIC-00001", submit="ask")
80
+
81
+ # Server-side report straight to a file
82
+ client.report("ITEMS_REPORT", licenses=["LIC-00001"], to="items.xlsx")
83
+
84
+ # A column of tag labels to full package records
85
+ tl = client.tag_lists.from_csv("labels.csv", column="label", license="LIC-00001",
86
+ endpoint="METRC_ACTIVE_PACKAGES")
87
+ packages = tl.resolve()
88
+ ```
89
+
90
+ ## The t3api command
91
+
92
+ ```bash
93
+ t3api login # save a secret key to a profile
94
+ t3api pull METRC_ACTIVE_PACKAGES -l LIC-00001 -o packages.csv # a whole collection to CSV or JSON
95
+ t3api report ITEMS_REPORT -l LIC-00001 -o items.xlsx # a server-side report
96
+ t3api endpoints harvest --kind collection # search the T3 spec
97
+ t3api keys list # manage secret keys
98
+ ```
99
+
100
+ `t3api --help` lists every command.
101
+
102
+ ## Development
103
+
104
+ ```bash
105
+ uv sync
106
+ uv run pytest
107
+ uv run mypy
108
+ uv run mkdocs serve
109
+ ```
110
+
111
+ See [CLAUDE.md](CLAUDE.md) for the project layout and conventions, and
112
+ [CHANGELOG.md](CHANGELOG.md) for release notes.
113
+
114
+ ## License
115
+
116
+ [GPL-3.0-only](LICENSE).
117
+
118
+ ## Links
119
+
120
+ - [Documentation](https://classvsoftware.github.io/t3api-python-utils/)
121
+ - [T3 API docs](https://api.trackandtrace.tools/v2/docs)
122
+ - [T3 website](https://www.trackandtrace.tools) and [wiki](https://www.trackandtrace.tools/wiki)
123
+ - [Example scripts](https://github.com/classvsoftware/t3-api-examples)
@@ -0,0 +1,95 @@
1
+ # t3api-utils
2
+
3
+ A Python library and `t3api` command for the [T3 API](https://api.trackandtrace.tools/v2/docs)
4
+ (Track & Trace Tools), which serves Metrc cannabis-compliance data. It handles
5
+ login and secret keys, loads whole collections, sends writes safely (dry run
6
+ unless you say otherwise), downloads server-side reports and builds tag lists.
7
+
8
+ **Documentation: <https://classvsoftware.github.io/t3api-python-utils/>**
9
+
10
+ > 2.0 is a rewrite and doesn't share an API with 1.x. See
11
+ > [Migrating from 1.x](https://classvsoftware.github.io/t3api-python-utils/migration/).
12
+ > 1.x is frozen at 1.5.0.
13
+
14
+ ## Install
15
+
16
+ Python 3.12 or later.
17
+
18
+ ```bash
19
+ uv add t3api-utils
20
+ pip install "t3api-utils[tui]" # [tui] adds the terminal row inspector
21
+ ```
22
+
23
+ ## Quickstart
24
+
25
+ Log in once. This creates a T3 secret key from your Metrc login and saves it to
26
+ your OS keyring; your Metrc password isn't stored.
27
+
28
+ ```bash
29
+ t3api login
30
+ ```
31
+
32
+ Then, in a script:
33
+
34
+ ```python
35
+ import t3api_utils as t3
36
+
37
+ client = t3.connect()
38
+ items = client.collect("METRC_ACTIVE_ITEMS", license="LIC-00001")
39
+ t3.to_csv(items, "items.csv")
40
+ ```
41
+
42
+ For scheduled jobs and CI, set `T3_SECRET_KEY` instead of logging in.
43
+
44
+ A few more things it does:
45
+
46
+ ```python
47
+ # Writes are a dry run unless submit=True
48
+ client.post("/v2/items/discontinue", license="LIC-00001", json={"id": 12345}, submit=True)
49
+
50
+ # Validate every row, show the results, and ask before submitting
51
+ client.bulk("/v2/items/discontinue", rows=[{"id": i["id"]} for i in items], license="LIC-00001", submit="ask")
52
+
53
+ # Server-side report straight to a file
54
+ client.report("ITEMS_REPORT", licenses=["LIC-00001"], to="items.xlsx")
55
+
56
+ # A column of tag labels to full package records
57
+ tl = client.tag_lists.from_csv("labels.csv", column="label", license="LIC-00001",
58
+ endpoint="METRC_ACTIVE_PACKAGES")
59
+ packages = tl.resolve()
60
+ ```
61
+
62
+ ## The t3api command
63
+
64
+ ```bash
65
+ t3api login # save a secret key to a profile
66
+ t3api pull METRC_ACTIVE_PACKAGES -l LIC-00001 -o packages.csv # a whole collection to CSV or JSON
67
+ t3api report ITEMS_REPORT -l LIC-00001 -o items.xlsx # a server-side report
68
+ t3api endpoints harvest --kind collection # search the T3 spec
69
+ t3api keys list # manage secret keys
70
+ ```
71
+
72
+ `t3api --help` lists every command.
73
+
74
+ ## Development
75
+
76
+ ```bash
77
+ uv sync
78
+ uv run pytest
79
+ uv run mypy
80
+ uv run mkdocs serve
81
+ ```
82
+
83
+ See [CLAUDE.md](CLAUDE.md) for the project layout and conventions, and
84
+ [CHANGELOG.md](CHANGELOG.md) for release notes.
85
+
86
+ ## License
87
+
88
+ [GPL-3.0-only](LICENSE).
89
+
90
+ ## Links
91
+
92
+ - [Documentation](https://classvsoftware.github.io/t3api-python-utils/)
93
+ - [T3 API docs](https://api.trackandtrace.tools/v2/docs)
94
+ - [T3 website](https://www.trackandtrace.tools) and [wiki](https://www.trackandtrace.tools/wiki)
95
+ - [Example scripts](https://github.com/classvsoftware/t3-api-examples)
@@ -0,0 +1,100 @@
1
+ [project]
2
+ name = "t3api-utils"
3
+ version = "2.0.1"
4
+ description = "Secret-key auth, collection loading, reports and tag lists for the T3 API"
5
+ readme = "README.md"
6
+ license = "GPL-3.0-only"
7
+ requires-python = ">=3.12"
8
+ keywords = [
9
+ "t3",
10
+ "metrc",
11
+ "track and trace",
12
+ "cannabis",
13
+ "api",
14
+ ]
15
+ classifiers = [
16
+ "Development Status :: 5 - Production/Stable",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Operating System :: OS Independent",
21
+ "Typing :: Typed",
22
+ ]
23
+ dependencies = [
24
+ "httpx2>=2.16",
25
+ "rich>=13.0",
26
+ "typer>=0.16",
27
+ "keyring>=25.0",
28
+ "platformdirs>=4.0",
29
+ "pyotp>=2.8",
30
+ "tomli-w>=1.0",
31
+ ]
32
+
33
+ [[project.authors]]
34
+ name = "Matt Frisbie"
35
+ email = "matt@classvsoftware.com"
36
+
37
+ [project.optional-dependencies]
38
+ tui = ["textual>=6.0"]
39
+
40
+ [project.scripts]
41
+ t3api = "t3api_utils.cli:app"
42
+
43
+ [project.urls]
44
+ Homepage = "https://github.com/classvsoftware/t3api-python-utils"
45
+ Documentation = "https://classvsoftware.github.io/t3api-python-utils/"
46
+
47
+ [build-system]
48
+ requires = ["uv_build>=0.9.0,<0.10.0"]
49
+ build-backend = "uv_build"
50
+
51
+ [tool.uv.build-backend]
52
+ module-name = "t3api_utils"
53
+ module-root = ""
54
+
55
+ [tool.mypy]
56
+ files = [
57
+ "t3api_utils",
58
+ "tests",
59
+ "scripts",
60
+ ]
61
+ strict = true
62
+ warn_unreachable = true
63
+
64
+ [[tool.mypy.overrides]]
65
+ module = [
66
+ "pyotp",
67
+ "pyotp.*",
68
+ ]
69
+ ignore_missing_imports = true
70
+
71
+ [tool.pytest.ini_options]
72
+ testpaths = ["tests"]
73
+ addopts = "-ra --strict-markers"
74
+ markers = ["sandbox: live tests against the T3 sandbox; need T3_SMOKE_SECRET_KEY"]
75
+ filterwarnings = ["error"]
76
+
77
+ [tool.coverage.run]
78
+ source = ["t3api_utils"]
79
+ branch = true
80
+
81
+ [tool.coverage.report]
82
+ show_missing = true
83
+ skip_covered = true
84
+ fail_under = 100
85
+ exclude_also = [
86
+ "if TYPE_CHECKING:",
87
+ "@overload",
88
+ "raise NotImplementedError",
89
+ ]
90
+
91
+ [dependency-groups]
92
+ dev = [
93
+ "textual>=6.0",
94
+ "pytest>=8.3",
95
+ "pytest-cov>=6.0",
96
+ "mypy>=1.14",
97
+ "mkdocs>=1.6",
98
+ "mkdocs-material>=9.0",
99
+ "mkdocstrings[python]>=0.27",
100
+ ]
@@ -0,0 +1,86 @@
1
+ [project]
2
+ name = "t3api-utils"
3
+ version = "2.0.1"
4
+ description = "Secret-key auth, collection loading, reports and tag lists for the T3 API"
5
+ authors = [{ name = "Matt Frisbie", email = "matt@classvsoftware.com" }]
6
+ readme = "README.md"
7
+ license = "GPL-3.0-only"
8
+ requires-python = ">=3.12"
9
+ keywords = ["t3", "metrc", "track and trace", "cannabis", "api"]
10
+ classifiers = [
11
+ "Development Status :: 5 - Production/Stable",
12
+ "Programming Language :: Python :: 3",
13
+ "Programming Language :: Python :: 3.12",
14
+ "Programming Language :: Python :: 3.13",
15
+ "Operating System :: OS Independent",
16
+ "Typing :: Typed",
17
+ ]
18
+ dependencies = [
19
+ "httpx2>=2.16",
20
+ "rich>=13.0",
21
+ "typer>=0.16",
22
+ "keyring>=25.0",
23
+ "platformdirs>=4.0",
24
+ "pyotp>=2.8",
25
+ "tomli-w>=1.0",
26
+ ]
27
+
28
+ [project.optional-dependencies]
29
+ tui = ["textual>=6.0"]
30
+
31
+ [project.scripts]
32
+ t3api = "t3api_utils.cli:app"
33
+
34
+ [project.urls]
35
+ Homepage = "https://github.com/classvsoftware/t3api-python-utils"
36
+ Documentation = "https://classvsoftware.github.io/t3api-python-utils/"
37
+
38
+ [build-system]
39
+ requires = ["uv_build>=0.9.0,<0.10.0"]
40
+ build-backend = "uv_build"
41
+
42
+ [tool.uv.build-backend]
43
+ module-name = "t3api_utils"
44
+ module-root = ""
45
+
46
+ [dependency-groups]
47
+ dev = [
48
+ "textual>=6.0",
49
+ "pytest>=8.3",
50
+ "pytest-cov>=6.0",
51
+ "mypy>=1.14",
52
+ "mkdocs>=1.6",
53
+ "mkdocs-material>=9.0",
54
+ "mkdocstrings[python]>=0.27",
55
+ ]
56
+
57
+ [tool.mypy]
58
+ files = ["t3api_utils", "tests", "scripts"]
59
+ strict = true
60
+ warn_unreachable = true
61
+
62
+ [[tool.mypy.overrides]]
63
+ module = ["pyotp", "pyotp.*"]
64
+ ignore_missing_imports = true
65
+
66
+ [tool.pytest.ini_options]
67
+ testpaths = ["tests"]
68
+ addopts = "-ra --strict-markers"
69
+ markers = [
70
+ "sandbox: live tests against the T3 sandbox; need T3_SMOKE_SECRET_KEY",
71
+ ]
72
+ filterwarnings = ["error"]
73
+
74
+ [tool.coverage.run]
75
+ source = ["t3api_utils"]
76
+ branch = true
77
+
78
+ [tool.coverage.report]
79
+ show_missing = true
80
+ skip_covered = true
81
+ fail_under = 100
82
+ exclude_also = [
83
+ "if TYPE_CHECKING:",
84
+ "@overload",
85
+ "raise NotImplementedError",
86
+ ]
@@ -0,0 +1,99 @@
1
+ """t3api-utils: secret-key auth, collections, reports and tag lists for the T3 API.
2
+
3
+ ```python
4
+ import t3api_utils as t3
5
+
6
+ client = t3.connect()
7
+ items = client.collect("METRC_ACTIVE_ITEMS", license="LIC-00001")
8
+ t3.to_csv(items, "items.csv")
9
+ ```
10
+
11
+ Importing the package has no side effects: no files, no logging setup, no
12
+ network calls.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from collections.abc import Mapping, Sequence
18
+ from importlib import metadata
19
+ from typing import Any
20
+
21
+ from t3api_utils.auth.credentials import BearerAuth, SecretKeyAuth
22
+ from t3api_utils.auth.resolve import connect
23
+ from t3api_utils.client import T3Client
24
+ from t3api_utils.errors import (
25
+ APIError,
26
+ AuthError,
27
+ AuthRequired,
28
+ NotFound,
29
+ NotInteractive,
30
+ RateLimited,
31
+ ServerError,
32
+ SpecError,
33
+ T3Error,
34
+ TagListResolveError,
35
+ UnknownEndpoint,
36
+ ValidationError,
37
+ )
38
+ from t3api_utils.local import filter_rows, read_rows, to_csv, to_json
39
+ from t3api_utils.pickers import choose, pick_file
40
+ from t3api_utils.prompts import ask, confirm
41
+ from t3api_utils.spec.registry import Endpoint
42
+ from t3api_utils.tag_lists import TagList
43
+ from t3api_utils.writes import BulkResult, RowResult, WriteResult
44
+
45
+ try:
46
+ __version__ = metadata.version("t3api-utils")
47
+ except metadata.PackageNotFoundError: # pragma: no cover - only in odd installs
48
+ __version__ = "0+unknown"
49
+
50
+ TUI_HINT = "t3.inspect() needs Textual. Install it with: pip install 't3api-utils[tui]'"
51
+
52
+
53
+ def inspect(rows: Sequence[Mapping[str, Any]], *, title: str = "rows") -> None:
54
+ """Browse rows in the Textual inspector (needs ``t3api-utils[tui]``)."""
55
+ try:
56
+ from t3api_utils.tui import inspector
57
+ except ImportError as exc:
58
+ raise ImportError(TUI_HINT) from exc
59
+ if not rows:
60
+ from t3api_utils import _console
61
+
62
+ _console.info("No rows to inspect.")
63
+ return
64
+ inspector.run(rows, title=title)
65
+
66
+
67
+ __all__ = [
68
+ "APIError",
69
+ "AuthError",
70
+ "AuthRequired",
71
+ "BearerAuth",
72
+ "BulkResult",
73
+ "Endpoint",
74
+ "NotFound",
75
+ "NotInteractive",
76
+ "RateLimited",
77
+ "RowResult",
78
+ "SecretKeyAuth",
79
+ "ServerError",
80
+ "SpecError",
81
+ "T3Client",
82
+ "T3Error",
83
+ "TagList",
84
+ "TagListResolveError",
85
+ "UnknownEndpoint",
86
+ "ValidationError",
87
+ "WriteResult",
88
+ "__version__",
89
+ "ask",
90
+ "choose",
91
+ "confirm",
92
+ "connect",
93
+ "filter_rows",
94
+ "inspect",
95
+ "pick_file",
96
+ "read_rows",
97
+ "to_csv",
98
+ "to_json",
99
+ ]
@@ -0,0 +1,85 @@
1
+ """Send a request and turn errors into APIError (internal)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import tempfile
7
+ from pathlib import Path
8
+ from typing import Any
9
+
10
+ import httpx2
11
+
12
+ from t3api_utils.errors import T3Error, error_from_response
13
+
14
+
15
+ def send(
16
+ http: httpx2.Client,
17
+ method: str,
18
+ url: str,
19
+ *,
20
+ credential_kind: str | None = None,
21
+ **kwargs: Any,
22
+ ) -> httpx2.Response:
23
+ """Send one request; raise the matching APIError for a non-2xx response."""
24
+ response = http.request(method, url, **kwargs)
25
+ if response.is_error:
26
+ raise error_from_response(response, credential_kind=credential_kind)
27
+ return response
28
+
29
+
30
+ def parse(response: httpx2.Response) -> Any:
31
+ """Parsed JSON for JSON responses, ``None`` for an empty body, else ``bytes``."""
32
+ content = response.content
33
+ if not content:
34
+ return None
35
+ if "json" in response.headers.get("content-type", ""):
36
+ return response.json()
37
+ return content
38
+
39
+
40
+ def require_field(data: Any, field: str, what: str) -> str:
41
+ """Return ``data[field]`` as a string, or raise T3Error if the response lacks it."""
42
+ if isinstance(data, dict) and data.get(field):
43
+ return str(data[field])
44
+ raise T3Error(f"{what} returned an unexpected response (no {field!r}).")
45
+
46
+
47
+ def call(
48
+ http: httpx2.Client,
49
+ method: str,
50
+ url: str,
51
+ *,
52
+ credential_kind: str | None = None,
53
+ **kwargs: Any,
54
+ ) -> Any:
55
+ return parse(send(http, method, url, credential_kind=credential_kind, **kwargs))
56
+
57
+
58
+ def download(
59
+ http: httpx2.Client,
60
+ url: str,
61
+ target: Path,
62
+ *,
63
+ credential_kind: str | None = None,
64
+ **kwargs: Any,
65
+ ) -> Path:
66
+ """Stream a GET response to ``target``.
67
+
68
+ The body goes to a temporary file next to ``target`` that is renamed only
69
+ once the download finishes, so a failure never leaves a half-written file.
70
+ """
71
+ target.parent.mkdir(parents=True, exist_ok=True)
72
+ with http.stream("GET", url, **kwargs) as response:
73
+ if response.is_error:
74
+ response.read()
75
+ raise error_from_response(response, credential_kind=credential_kind)
76
+ fd, tmp = tempfile.mkstemp(dir=target.parent, prefix=f".{target.name}.", suffix=".part")
77
+ try:
78
+ with os.fdopen(fd, "wb") as handle:
79
+ for chunk in response.iter_bytes():
80
+ handle.write(chunk)
81
+ os.replace(tmp, target)
82
+ except BaseException:
83
+ Path(tmp).unlink(missing_ok=True)
84
+ raise
85
+ return target