negirau 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.
- negirau-0.1.0/LICENSE +202 -0
- negirau-0.1.0/PKG-INFO +97 -0
- negirau-0.1.0/README.md +68 -0
- negirau-0.1.0/pyproject.toml +101 -0
- negirau-0.1.0/pyproject.toml.orig +105 -0
- negirau-0.1.0/src/negirau/__init__.py +37 -0
- negirau-0.1.0/src/negirau/_client.py +191 -0
- negirau-0.1.0/src/negirau/_exceptions.py +83 -0
- negirau-0.1.0/src/negirau/py.typed +0 -0
- negirau-0.1.0/src/negirau/resources/__init__.py +6 -0
- negirau-0.1.0/src/negirau/resources/subjects.py +166 -0
- negirau-0.1.0/src/negirau/types/__init__.py +15 -0
- negirau-0.1.0/src/negirau/types/display_value.py +5 -0
- negirau-0.1.0/src/negirau/types/reactions_summary.py +15 -0
- negirau-0.1.0/src/negirau/types/subject.py +19 -0
- negirau-0.1.0/src/negirau/types/subject_page.py +13 -0
negirau-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Shinsuke Mori
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
negirau-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: negirau
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for the Negirau public API
|
|
5
|
+
Keywords: negirau,api,client,appreciation,gratitude
|
|
6
|
+
Author: Shinsuke Mori
|
|
7
|
+
Author-email: Shinsuke Mori <sincekmori@gmail.com>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Dist: httpx>=0.27
|
|
22
|
+
Requires-Dist: pydantic>=2.7
|
|
23
|
+
Requires-Dist: typing-extensions>=4.6
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Project-URL: Homepage, https://negirau.com
|
|
26
|
+
Project-URL: Repository, https://github.com/sincekmori/negirau
|
|
27
|
+
Project-URL: Issues, https://github.com/sincekmori/negirau/issues
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# negirau
|
|
31
|
+
|
|
32
|
+
Python client for the [Negirau](https://negirau.com) public API (read-only, anonymous).
|
|
33
|
+
|
|
34
|
+
Hand-written around resource namespaces, a `types` package, a typed exception hierarchy, and retries with exponential backoff — and kept honest by contract tests: they load the service's build-time-generated `openapi.json` and prove that every operation is covered, that requests only use spec-declared parameters, and that spec-conformant payloads parse into the models.
|
|
35
|
+
Drift between the API and this client fails CI — the same guarantee code generation gave, without generated code.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
pip install negirau
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from negirau import Negirau
|
|
47
|
+
|
|
48
|
+
client = Negirau()
|
|
49
|
+
|
|
50
|
+
found = client.subjects.list(q="消防", limit=5) # free-text name search
|
|
51
|
+
for subject in found.subjects:
|
|
52
|
+
print(subject.id, subject.name)
|
|
53
|
+
|
|
54
|
+
nearby = client.subjects.list_near(35.64, 139.65, radius=3000)
|
|
55
|
+
|
|
56
|
+
reactions = client.subjects.reactions.retrieve(
|
|
57
|
+
"0e6f9b3a-6b1e-4b8a-9a6a-1c2d3e4f5a6b",
|
|
58
|
+
period="2026-W33", # or a month ("2026-08"), a year ("2026"), or "all"
|
|
59
|
+
)
|
|
60
|
+
print(reactions.total) # a display value: exact up to 100, "100+" beyond
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Async:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from negirau import AsyncNegirau
|
|
67
|
+
|
|
68
|
+
async with AsyncNegirau() as client:
|
|
69
|
+
page = await client.subjects.list(q="花火")
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Errors and retries:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from negirau import Negirau, NotFoundError, RateLimitError
|
|
76
|
+
|
|
77
|
+
client = Negirau(max_retries=2) # 408/409/429/5xx retry with exponential backoff
|
|
78
|
+
|
|
79
|
+
try:
|
|
80
|
+
client.subjects.retrieve("no-such-subject")
|
|
81
|
+
except NotFoundError as error:
|
|
82
|
+
print(error.status_code, error.code) # 404 not_found
|
|
83
|
+
except RateLimitError:
|
|
84
|
+
...
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Types live in `negirau.types` (`Subject`, `SubjectPage`, `ReactionsSummary`, `DisplayValue`, …).
|
|
88
|
+
|
|
89
|
+
## Notes
|
|
90
|
+
|
|
91
|
+
- Counts are display values by design: exact up to 100, `"100+"` beyond, and never rankings — no API returns unbounded exact numbers.
|
|
92
|
+
- There are no bulk dumps: resolve names with `client.subjects.list(q=...)` (server-side trigram search, Japanese and English substrings).
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
Apache-2.0.
|
|
97
|
+
This package lives in the [negirau monorepo](https://github.com/sincekmori/negirau), whose server code is separately licensed under AGPL-3.0-only.
|
negirau-0.1.0/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# negirau
|
|
2
|
+
|
|
3
|
+
Python client for the [Negirau](https://negirau.com) public API (read-only, anonymous).
|
|
4
|
+
|
|
5
|
+
Hand-written around resource namespaces, a `types` package, a typed exception hierarchy, and retries with exponential backoff — and kept honest by contract tests: they load the service's build-time-generated `openapi.json` and prove that every operation is covered, that requests only use spec-declared parameters, and that spec-conformant payloads parse into the models.
|
|
6
|
+
Drift between the API and this client fails CI — the same guarantee code generation gave, without generated code.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
pip install negirau
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from negirau import Negirau
|
|
18
|
+
|
|
19
|
+
client = Negirau()
|
|
20
|
+
|
|
21
|
+
found = client.subjects.list(q="消防", limit=5) # free-text name search
|
|
22
|
+
for subject in found.subjects:
|
|
23
|
+
print(subject.id, subject.name)
|
|
24
|
+
|
|
25
|
+
nearby = client.subjects.list_near(35.64, 139.65, radius=3000)
|
|
26
|
+
|
|
27
|
+
reactions = client.subjects.reactions.retrieve(
|
|
28
|
+
"0e6f9b3a-6b1e-4b8a-9a6a-1c2d3e4f5a6b",
|
|
29
|
+
period="2026-W33", # or a month ("2026-08"), a year ("2026"), or "all"
|
|
30
|
+
)
|
|
31
|
+
print(reactions.total) # a display value: exact up to 100, "100+" beyond
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Async:
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from negirau import AsyncNegirau
|
|
38
|
+
|
|
39
|
+
async with AsyncNegirau() as client:
|
|
40
|
+
page = await client.subjects.list(q="花火")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Errors and retries:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from negirau import Negirau, NotFoundError, RateLimitError
|
|
47
|
+
|
|
48
|
+
client = Negirau(max_retries=2) # 408/409/429/5xx retry with exponential backoff
|
|
49
|
+
|
|
50
|
+
try:
|
|
51
|
+
client.subjects.retrieve("no-such-subject")
|
|
52
|
+
except NotFoundError as error:
|
|
53
|
+
print(error.status_code, error.code) # 404 not_found
|
|
54
|
+
except RateLimitError:
|
|
55
|
+
...
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Types live in `negirau.types` (`Subject`, `SubjectPage`, `ReactionsSummary`, `DisplayValue`, …).
|
|
59
|
+
|
|
60
|
+
## Notes
|
|
61
|
+
|
|
62
|
+
- Counts are display values by design: exact up to 100, `"100+"` beyond, and never rankings — no API returns unbounded exact numbers.
|
|
63
|
+
- There are no bulk dumps: resolve names with `client.subjects.list(q=...)` (server-side trigram search, Japanese and English substrings).
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
Apache-2.0.
|
|
68
|
+
This package lives in the [negirau monorepo](https://github.com/sincekmori/negirau), whose server code is separately licensed under AGPL-3.0-only.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "negirau"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Python client for the Negirau public API"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
keywords = [
|
|
10
|
+
"negirau",
|
|
11
|
+
"api",
|
|
12
|
+
"client",
|
|
13
|
+
"appreciation",
|
|
14
|
+
"gratitude",
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Programming Language :: Python :: 3.14",
|
|
26
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"httpx>=0.27",
|
|
31
|
+
"pydantic>=2.7",
|
|
32
|
+
"typing-extensions>=4.6",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[[project.authors]]
|
|
36
|
+
name = "Shinsuke Mori"
|
|
37
|
+
email = "sincekmori@gmail.com"
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
Homepage = "https://negirau.com"
|
|
41
|
+
Repository = "https://github.com/sincekmori/negirau"
|
|
42
|
+
Issues = "https://github.com/sincekmori/negirau/issues"
|
|
43
|
+
|
|
44
|
+
[dependency-groups]
|
|
45
|
+
dev = [
|
|
46
|
+
"jsonschema>=4.23",
|
|
47
|
+
"pytest>=8",
|
|
48
|
+
"pytest-asyncio>=1.4.0",
|
|
49
|
+
"pytest-cov>=7.1.0",
|
|
50
|
+
"ruff",
|
|
51
|
+
"ty",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[build-system]
|
|
55
|
+
requires = ["uv_build>=0.11.0,<0.13.0"]
|
|
56
|
+
build-backend = "uv_build"
|
|
57
|
+
|
|
58
|
+
[tool.ruff]
|
|
59
|
+
line-length = 88
|
|
60
|
+
target-version = "py310"
|
|
61
|
+
|
|
62
|
+
[tool.ruff.lint]
|
|
63
|
+
select = ["ALL"]
|
|
64
|
+
ignore = [
|
|
65
|
+
"COM812",
|
|
66
|
+
"D203",
|
|
67
|
+
"D213",
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
[tool.ruff.lint.pydocstyle]
|
|
71
|
+
convention = "google"
|
|
72
|
+
|
|
73
|
+
[tool.ruff.lint.mccabe]
|
|
74
|
+
max-complexity = 5
|
|
75
|
+
|
|
76
|
+
[tool.ruff.lint.flake8-copyright]
|
|
77
|
+
notice-rgx = "SPDX-License-Identifier:"
|
|
78
|
+
|
|
79
|
+
[tool.ruff.lint.pylint]
|
|
80
|
+
max-args = 8
|
|
81
|
+
|
|
82
|
+
[tool.ruff.lint.per-file-ignores]
|
|
83
|
+
"tests/**" = [
|
|
84
|
+
"S101",
|
|
85
|
+
"PLR2004",
|
|
86
|
+
"D",
|
|
87
|
+
"ANN",
|
|
88
|
+
"SLF001",
|
|
89
|
+
"EM101",
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
[tool.ruff.format]
|
|
93
|
+
line-ending = "lf"
|
|
94
|
+
docstring-code-format = true
|
|
95
|
+
|
|
96
|
+
[tool.ty.environment]
|
|
97
|
+
python-version = "3.10"
|
|
98
|
+
|
|
99
|
+
[tool.pytest.ini_options]
|
|
100
|
+
addopts = "--cov=negirau --cov-branch --cov-report=term-missing --cov-fail-under=100"
|
|
101
|
+
asyncio_mode = "auto"
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "negirau"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Python client for the Negirau public API"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
# All non-EOL CPython minors (house rule); 3.10 is the current floor.
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
authors = [{ name = "Shinsuke Mori", email = "sincekmori@gmail.com" }]
|
|
11
|
+
keywords = ["negirau", "api", "client", "appreciation", "gratitude"]
|
|
12
|
+
# No License classifier: the SPDX `license` expression (PEP 639) supersedes it.
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.10",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"httpx>=0.27",
|
|
28
|
+
"pydantic>=2.7",
|
|
29
|
+
# typing.Self exists only from 3.11; the floor is 3.10 (also a pydantic transitive).
|
|
30
|
+
"typing-extensions>=4.6",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://negirau.com"
|
|
35
|
+
Repository = "https://github.com/sincekmori/negirau"
|
|
36
|
+
Issues = "https://github.com/sincekmori/negirau/issues"
|
|
37
|
+
|
|
38
|
+
[dependency-groups]
|
|
39
|
+
dev = [
|
|
40
|
+
# jsonschema powers the contract tests: sample payloads are proven
|
|
41
|
+
# spec-conformant before the client is required to parse them.
|
|
42
|
+
"jsonschema>=4.23",
|
|
43
|
+
"pytest>=8",
|
|
44
|
+
"pytest-asyncio>=1.4.0",
|
|
45
|
+
"pytest-cov>=7.1.0",
|
|
46
|
+
"ruff",
|
|
47
|
+
"ty",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[build-system]
|
|
51
|
+
requires = ["uv_build>=0.11.0,<0.13.0"]
|
|
52
|
+
build-backend = "uv_build"
|
|
53
|
+
|
|
54
|
+
[tool.ruff]
|
|
55
|
+
line-length = 88
|
|
56
|
+
# The floor of the supported range, matching requires-python.
|
|
57
|
+
target-version = "py310"
|
|
58
|
+
|
|
59
|
+
[tool.ruff.lint]
|
|
60
|
+
# House philosophy: enable everything; each ignore carries its reason.
|
|
61
|
+
select = ["ALL"]
|
|
62
|
+
ignore = [
|
|
63
|
+
"COM812", # conflicts with the ruff formatter (trailing-comma ownership)
|
|
64
|
+
"D203", # conflicting docstring pair: D211 (no blank line before class docstring) wins
|
|
65
|
+
"D213", # conflicting docstring pair: D212 (summary on the first line) wins
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint.pydocstyle]
|
|
69
|
+
convention = "google"
|
|
70
|
+
|
|
71
|
+
[tool.ruff.lint.mccabe]
|
|
72
|
+
# Deliberately stricter than the default 10 (house threshold).
|
|
73
|
+
max-complexity = 5
|
|
74
|
+
|
|
75
|
+
[tool.ruff.lint.flake8-copyright]
|
|
76
|
+
# SPDX headers are the house copyright notice.
|
|
77
|
+
notice-rgx = "SPDX-License-Identifier:"
|
|
78
|
+
|
|
79
|
+
[tool.ruff.lint.pylint]
|
|
80
|
+
# list_subjects mirrors the spec's six query parameters as keyword-only args.
|
|
81
|
+
max-args = 8
|
|
82
|
+
|
|
83
|
+
[tool.ruff.lint.per-file-ignores]
|
|
84
|
+
"tests/**" = [
|
|
85
|
+
"S101", # pytest asserts ARE the test mechanism
|
|
86
|
+
"PLR2004", # tests assert on literal values by design
|
|
87
|
+
"D", # test names document themselves; docstrings would repeat them
|
|
88
|
+
"ANN", # fixture/parametrize types add noise, not safety, in tests
|
|
89
|
+
"SLF001", # asserting on the private _http client is the point of the default-construction tests
|
|
90
|
+
"EM101", # mock transport handlers raise canned errors with literal messages
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
[tool.ruff.format]
|
|
94
|
+
line-ending = "lf"
|
|
95
|
+
docstring-code-format = true
|
|
96
|
+
|
|
97
|
+
[tool.ty.environment]
|
|
98
|
+
# Type-check against the floor so oldest-version breakage surfaces first.
|
|
99
|
+
python-version = "3.10"
|
|
100
|
+
|
|
101
|
+
[tool.pytest.ini_options]
|
|
102
|
+
# Coverage always on; private repo, so it stays in the terminal/CI log (no upload).
|
|
103
|
+
# --cov-branch matches the TS side's branches:100 threshold.
|
|
104
|
+
addopts = "--cov=negirau --cov-branch --cov-report=term-missing --cov-fail-under=100"
|
|
105
|
+
asyncio_mode = "auto"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Python client for the Negirau public API (read-only, anonymous).
|
|
3
|
+
|
|
4
|
+
Hand-written around resource namespaces, a ``types``
|
|
5
|
+
package, a typed exception hierarchy, retries with backoff — and kept honest
|
|
6
|
+
by spec contract tests (tests/test_contract.py).
|
|
7
|
+
|
|
8
|
+
from negirau import Negirau
|
|
9
|
+
|
|
10
|
+
client = Negirau()
|
|
11
|
+
page = client.subjects.list(q="消防")
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from negirau._client import AsyncNegirau, Negirau
|
|
15
|
+
from negirau._exceptions import (
|
|
16
|
+
APIConnectionError,
|
|
17
|
+
APIStatusError,
|
|
18
|
+
APITimeoutError,
|
|
19
|
+
BadRequestError,
|
|
20
|
+
InternalServerError,
|
|
21
|
+
NegirauError,
|
|
22
|
+
NotFoundError,
|
|
23
|
+
RateLimitError,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
__all__ = (
|
|
27
|
+
"APIConnectionError",
|
|
28
|
+
"APIStatusError",
|
|
29
|
+
"APITimeoutError",
|
|
30
|
+
"AsyncNegirau",
|
|
31
|
+
"BadRequestError",
|
|
32
|
+
"InternalServerError",
|
|
33
|
+
"Negirau",
|
|
34
|
+
"NegirauError",
|
|
35
|
+
"NotFoundError",
|
|
36
|
+
"RateLimitError",
|
|
37
|
+
)
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""The ``Negirau`` / ``AsyncNegirau`` clients (httpx-based).
|
|
3
|
+
|
|
4
|
+
Private module: everything public is re-exported through ``negirau``.
|
|
5
|
+
The clients own transport concerns (base URL, timeout, retries with
|
|
6
|
+
exponential backoff); the API surface lives on the resources
|
|
7
|
+
(``client.subjects``).
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import asyncio
|
|
13
|
+
import time
|
|
14
|
+
from typing import TYPE_CHECKING
|
|
15
|
+
|
|
16
|
+
import httpx
|
|
17
|
+
from typing_extensions import Self
|
|
18
|
+
|
|
19
|
+
from negirau._exceptions import (
|
|
20
|
+
INTERNAL_ERROR_FLOOR,
|
|
21
|
+
APIConnectionError,
|
|
22
|
+
APITimeoutError,
|
|
23
|
+
NegirauError,
|
|
24
|
+
make_status_error,
|
|
25
|
+
)
|
|
26
|
+
from negirau.resources import (
|
|
27
|
+
AsyncSubjects,
|
|
28
|
+
Subjects,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
if TYPE_CHECKING:
|
|
32
|
+
from types import TracebackType
|
|
33
|
+
|
|
34
|
+
DEFAULT_BASE_URL = "https://api.negirau.com/v1"
|
|
35
|
+
DEFAULT_TIMEOUT = 10.0
|
|
36
|
+
DEFAULT_MAX_RETRIES = 2
|
|
37
|
+
|
|
38
|
+
# Retryable statuses: timeouts, conflicts, throttles, 5xx.
|
|
39
|
+
_RETRYABLE_STATUS = frozenset({408, 409, 429})
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _should_retry(status_code: int) -> bool:
|
|
43
|
+
return status_code in _RETRYABLE_STATUS or status_code >= INTERNAL_ERROR_FLOOR
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _backoff_seconds(attempt: int) -> float:
|
|
47
|
+
return min(0.5 * 2**attempt, 8.0)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _clean_params(params: dict[str, str | int | None]) -> dict[str, str]:
|
|
51
|
+
return {key: str(value) for key, value in params.items() if value is not None}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class _Retry:
|
|
55
|
+
"""Sentinel: the attempt failed retryably; back off and go again."""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
_RETRY = _Retry()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _fail_or_retry(error: NegirauError, *, last: bool, cause: Exception) -> _Retry:
|
|
62
|
+
if last:
|
|
63
|
+
raise error from cause
|
|
64
|
+
return _RETRY
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _resolve(response: httpx.Response, *, last: bool) -> object:
|
|
68
|
+
if response.status_code == httpx.codes.OK:
|
|
69
|
+
return response.json()
|
|
70
|
+
if last or not _should_retry(response.status_code):
|
|
71
|
+
raise make_status_error(response)
|
|
72
|
+
return _RETRY
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class Negirau:
|
|
76
|
+
"""Synchronous client for the Negirau public API.
|
|
77
|
+
|
|
78
|
+
Entry point::
|
|
79
|
+
|
|
80
|
+
from negirau import Negirau
|
|
81
|
+
|
|
82
|
+
client = Negirau()
|
|
83
|
+
page = client.subjects.list(q="消防")
|
|
84
|
+
|
|
85
|
+
The API is read-only, anonymous, and returns counts only as coarse
|
|
86
|
+
buckets ("100+") — that is the service's anti-ranking design.
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
def __init__(
|
|
90
|
+
self,
|
|
91
|
+
*,
|
|
92
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
93
|
+
timeout: float = DEFAULT_TIMEOUT,
|
|
94
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
95
|
+
http_client: httpx.Client | None = None,
|
|
96
|
+
) -> None:
|
|
97
|
+
"""Create a client; ``http_client`` overrides the built-in transport."""
|
|
98
|
+
self._http = http_client or httpx.Client(base_url=base_url, timeout=timeout)
|
|
99
|
+
self.max_retries = max_retries
|
|
100
|
+
self.subjects = Subjects(self)
|
|
101
|
+
|
|
102
|
+
def close(self) -> None:
|
|
103
|
+
"""Close the underlying HTTP client."""
|
|
104
|
+
self._http.close()
|
|
105
|
+
|
|
106
|
+
def __enter__(self) -> Self:
|
|
107
|
+
"""Support ``with Negirau() as client:``."""
|
|
108
|
+
return self
|
|
109
|
+
|
|
110
|
+
def __exit__(
|
|
111
|
+
self,
|
|
112
|
+
exc_type: type[BaseException] | None,
|
|
113
|
+
exc: BaseException | None,
|
|
114
|
+
traceback: TracebackType | None,
|
|
115
|
+
) -> None:
|
|
116
|
+
"""Close on context-manager exit."""
|
|
117
|
+
self.close()
|
|
118
|
+
|
|
119
|
+
def get(self, path: str, params: dict[str, str | int | None]) -> object:
|
|
120
|
+
"""GET a spec-relative path with retries; returns the parsed JSON body."""
|
|
121
|
+
query = _clean_params(params)
|
|
122
|
+
for attempt in range(self.max_retries + 1):
|
|
123
|
+
result = self._attempt(path, query, last=attempt == self.max_retries)
|
|
124
|
+
if not isinstance(result, _Retry):
|
|
125
|
+
return result
|
|
126
|
+
time.sleep(_backoff_seconds(attempt))
|
|
127
|
+
raise AssertionError # pragma: no cover — the loop always returns or raises
|
|
128
|
+
|
|
129
|
+
def _attempt(self, path: str, query: dict[str, str], *, last: bool) -> object:
|
|
130
|
+
try:
|
|
131
|
+
response = self._http.get(path, params=query)
|
|
132
|
+
except httpx.TimeoutException as error:
|
|
133
|
+
return _fail_or_retry(APITimeoutError(), last=last, cause=error)
|
|
134
|
+
except httpx.HTTPError as error:
|
|
135
|
+
return _fail_or_retry(APIConnectionError(), last=last, cause=error)
|
|
136
|
+
return _resolve(response, last=last)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class AsyncNegirau:
|
|
140
|
+
"""Asynchronous twin of :class:`Negirau` (same surface, ``await``-able)."""
|
|
141
|
+
|
|
142
|
+
def __init__(
|
|
143
|
+
self,
|
|
144
|
+
*,
|
|
145
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
146
|
+
timeout: float = DEFAULT_TIMEOUT,
|
|
147
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
148
|
+
http_client: httpx.AsyncClient | None = None,
|
|
149
|
+
) -> None:
|
|
150
|
+
"""Create a client; ``http_client`` overrides the built-in transport."""
|
|
151
|
+
self._http = http_client or httpx.AsyncClient(
|
|
152
|
+
base_url=base_url, timeout=timeout
|
|
153
|
+
)
|
|
154
|
+
self.max_retries = max_retries
|
|
155
|
+
self.subjects = AsyncSubjects(self)
|
|
156
|
+
|
|
157
|
+
async def aclose(self) -> None:
|
|
158
|
+
"""Close the underlying HTTP client."""
|
|
159
|
+
await self._http.aclose()
|
|
160
|
+
|
|
161
|
+
async def __aenter__(self) -> Self:
|
|
162
|
+
"""Support ``async with AsyncNegirau() as client:``."""
|
|
163
|
+
return self
|
|
164
|
+
|
|
165
|
+
async def __aexit__(
|
|
166
|
+
self,
|
|
167
|
+
exc_type: type[BaseException] | None,
|
|
168
|
+
exc: BaseException | None,
|
|
169
|
+
traceback: TracebackType | None,
|
|
170
|
+
) -> None:
|
|
171
|
+
"""Close on context-manager exit."""
|
|
172
|
+
await self.aclose()
|
|
173
|
+
|
|
174
|
+
async def get(self, path: str, params: dict[str, str | int | None]) -> object:
|
|
175
|
+
"""GET a spec-relative path with retries; returns the parsed JSON body."""
|
|
176
|
+
query = _clean_params(params)
|
|
177
|
+
for attempt in range(self.max_retries + 1):
|
|
178
|
+
result = await self._attempt(path, query, last=attempt == self.max_retries)
|
|
179
|
+
if not isinstance(result, _Retry):
|
|
180
|
+
return result
|
|
181
|
+
await asyncio.sleep(_backoff_seconds(attempt))
|
|
182
|
+
raise AssertionError # pragma: no cover — the loop always returns or raises
|
|
183
|
+
|
|
184
|
+
async def _attempt(self, path: str, query: dict[str, str], *, last: bool) -> object:
|
|
185
|
+
try:
|
|
186
|
+
response = await self._http.get(path, params=query)
|
|
187
|
+
except httpx.TimeoutException as error:
|
|
188
|
+
return _fail_or_retry(APITimeoutError(), last=last, cause=error)
|
|
189
|
+
except httpx.HTTPError as error:
|
|
190
|
+
return _fail_or_retry(APIConnectionError(), last=last, cause=error)
|
|
191
|
+
return _resolve(response, last=last)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Exception hierarchy.
|
|
3
|
+
|
|
4
|
+
``NegirauError`` is the catch-all base; ``APIStatusError`` carries an HTTP
|
|
5
|
+
status and the API's machine-readable ``code``; common statuses get their own
|
|
6
|
+
subclass so callers can catch precisely.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
import httpx
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class NegirauError(Exception):
|
|
18
|
+
"""Base class for every error this SDK raises."""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class APIConnectionError(NegirauError):
|
|
22
|
+
"""The request never produced a response (DNS, TLS, refused, ...)."""
|
|
23
|
+
|
|
24
|
+
def __init__(self, message: str = "Connection error.") -> None:
|
|
25
|
+
"""Store the display message."""
|
|
26
|
+
super().__init__(message)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class APITimeoutError(APIConnectionError):
|
|
30
|
+
"""The request timed out."""
|
|
31
|
+
|
|
32
|
+
def __init__(self) -> None:
|
|
33
|
+
"""Fix the message; the timeout budget lives on the client."""
|
|
34
|
+
super().__init__("Request timed out.")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class APIStatusError(NegirauError):
|
|
38
|
+
"""A non-success HTTP response, with the API's ``code`` (e.g. "not_found")."""
|
|
39
|
+
|
|
40
|
+
def __init__(self, status_code: int, code: str) -> None:
|
|
41
|
+
"""Store the status and code, and build the display message."""
|
|
42
|
+
super().__init__(f"{status_code}: {code}")
|
|
43
|
+
self.status_code = status_code
|
|
44
|
+
self.code = code
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class BadRequestError(APIStatusError):
|
|
48
|
+
"""HTTP 400."""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class NotFoundError(APIStatusError):
|
|
52
|
+
"""HTTP 404."""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class RateLimitError(APIStatusError):
|
|
56
|
+
"""HTTP 429."""
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class InternalServerError(APIStatusError):
|
|
60
|
+
"""HTTP 5xx."""
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
_STATUS_CLASSES: dict[int, type[APIStatusError]] = {
|
|
64
|
+
400: BadRequestError,
|
|
65
|
+
404: NotFoundError,
|
|
66
|
+
429: RateLimitError,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#: Status classification boundary shared with the client's retry logic.
|
|
70
|
+
INTERNAL_ERROR_FLOOR = 500
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def make_status_error(response: httpx.Response) -> APIStatusError:
|
|
74
|
+
"""Build the most specific error class for a non-success response."""
|
|
75
|
+
try:
|
|
76
|
+
code = str(response.json().get("error", "unknown"))
|
|
77
|
+
except ValueError:
|
|
78
|
+
code = "unknown"
|
|
79
|
+
status = response.status_code
|
|
80
|
+
cls = _STATUS_CLASSES.get(status)
|
|
81
|
+
if cls is None:
|
|
82
|
+
cls = InternalServerError if status >= INTERNAL_ERROR_FLOOR else APIStatusError
|
|
83
|
+
return cls(status, code)
|
|
File without changes
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""The ``subjects`` resource (``client.subjects.list()`` etc.)."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
from urllib.parse import quote
|
|
8
|
+
|
|
9
|
+
from pydantic import BaseModel
|
|
10
|
+
|
|
11
|
+
from negirau.types import (
|
|
12
|
+
NearbySubject,
|
|
13
|
+
ReactionsSummary,
|
|
14
|
+
Subject,
|
|
15
|
+
SubjectPage,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
import builtins
|
|
20
|
+
|
|
21
|
+
from negirau._client import AsyncNegirau, Negirau
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _NearbyPage(BaseModel):
|
|
25
|
+
"""Internal: the near-search response shape (no cursor, distances included)."""
|
|
26
|
+
|
|
27
|
+
subjects: list[NearbySubject]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _subject_path(subject_id: str, suffix: str = "") -> str:
|
|
31
|
+
"""Path for one subject, with the id percent-encoded."""
|
|
32
|
+
return f"/subjects/{quote(subject_id, safe='')}{suffix}"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Reactions:
|
|
36
|
+
"""Sub-resource: ``client.subjects.reactions.retrieve(subject_id)``."""
|
|
37
|
+
|
|
38
|
+
def __init__(self, client: Negirau) -> None:
|
|
39
|
+
"""Bind to the owning client."""
|
|
40
|
+
self._client = client
|
|
41
|
+
|
|
42
|
+
def retrieve(
|
|
43
|
+
self, subject_id: str, *, period: str | None = None
|
|
44
|
+
) -> ReactionsSummary:
|
|
45
|
+
"""Reaction display values for a subject.
|
|
46
|
+
|
|
47
|
+
``period`` is an ISO week (``2026-W33``), month (``2026-08``),
|
|
48
|
+
year (``2026``), or ``all`` (default: ``all``).
|
|
49
|
+
"""
|
|
50
|
+
payload = self._client.get(
|
|
51
|
+
_subject_path(subject_id, "/reactions"), {"period": period}
|
|
52
|
+
)
|
|
53
|
+
return ReactionsSummary.model_validate(payload)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class Subjects:
|
|
57
|
+
"""The recipients of appreciation."""
|
|
58
|
+
|
|
59
|
+
def __init__(self, client: Negirau) -> None:
|
|
60
|
+
"""Bind to the owning client and expose sub-resources."""
|
|
61
|
+
self._client = client
|
|
62
|
+
self.reactions = Reactions(client)
|
|
63
|
+
|
|
64
|
+
def list(
|
|
65
|
+
self,
|
|
66
|
+
*,
|
|
67
|
+
q: str | None = None,
|
|
68
|
+
limit: int | None = None,
|
|
69
|
+
cursor: str | None = None,
|
|
70
|
+
) -> SubjectPage:
|
|
71
|
+
"""List/search subjects (cursor-paginated).
|
|
72
|
+
|
|
73
|
+
``q`` is free-text name search: 1-2 characters match as a name
|
|
74
|
+
prefix (single page), 3+ anywhere in the name.
|
|
75
|
+
"""
|
|
76
|
+
payload = self._client.get(
|
|
77
|
+
"/subjects",
|
|
78
|
+
{"q": q, "limit": limit, "cursor": cursor},
|
|
79
|
+
)
|
|
80
|
+
return SubjectPage.model_validate(payload)
|
|
81
|
+
|
|
82
|
+
def list_near(
|
|
83
|
+
self,
|
|
84
|
+
lat: float,
|
|
85
|
+
lng: float,
|
|
86
|
+
*,
|
|
87
|
+
radius: int | None = None,
|
|
88
|
+
limit: int | None = None,
|
|
89
|
+
) -> builtins.list[NearbySubject]:
|
|
90
|
+
"""Find spatial subjects near a point, closest first (radius in meters)."""
|
|
91
|
+
payload = self._client.get(
|
|
92
|
+
"/subjects", {"near": f"{lat},{lng}", "radius": radius, "limit": limit}
|
|
93
|
+
)
|
|
94
|
+
return _NearbyPage.model_validate(payload).subjects
|
|
95
|
+
|
|
96
|
+
def retrieve(self, subject_id: str) -> Subject:
|
|
97
|
+
"""Fetch one subject by its public identifier."""
|
|
98
|
+
payload = self._client.get(_subject_path(subject_id), {})
|
|
99
|
+
return Subject.model_validate(payload)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class AsyncReactions:
|
|
103
|
+
"""Async twin of :class:`Reactions`."""
|
|
104
|
+
|
|
105
|
+
def __init__(self, client: AsyncNegirau) -> None:
|
|
106
|
+
"""Bind to the owning client."""
|
|
107
|
+
self._client = client
|
|
108
|
+
|
|
109
|
+
async def retrieve(
|
|
110
|
+
self, subject_id: str, *, period: str | None = None
|
|
111
|
+
) -> ReactionsSummary:
|
|
112
|
+
"""Reaction display values for a subject.
|
|
113
|
+
|
|
114
|
+
``period`` is an ISO week (``2026-W33``), month (``2026-08``),
|
|
115
|
+
year (``2026``), or ``all`` (default: ``all``).
|
|
116
|
+
"""
|
|
117
|
+
payload = await self._client.get(
|
|
118
|
+
_subject_path(subject_id, "/reactions"), {"period": period}
|
|
119
|
+
)
|
|
120
|
+
return ReactionsSummary.model_validate(payload)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class AsyncSubjects:
|
|
124
|
+
"""Async twin of :class:`Subjects`."""
|
|
125
|
+
|
|
126
|
+
def __init__(self, client: AsyncNegirau) -> None:
|
|
127
|
+
"""Bind to the owning client and expose sub-resources."""
|
|
128
|
+
self._client = client
|
|
129
|
+
self.reactions = AsyncReactions(client)
|
|
130
|
+
|
|
131
|
+
async def list(
|
|
132
|
+
self,
|
|
133
|
+
*,
|
|
134
|
+
q: str | None = None,
|
|
135
|
+
limit: int | None = None,
|
|
136
|
+
cursor: str | None = None,
|
|
137
|
+
) -> SubjectPage:
|
|
138
|
+
"""List/search subjects (cursor-paginated).
|
|
139
|
+
|
|
140
|
+
``q`` is free-text name search: 1-2 characters match as a name
|
|
141
|
+
prefix (single page), 3+ anywhere in the name.
|
|
142
|
+
"""
|
|
143
|
+
payload = await self._client.get(
|
|
144
|
+
"/subjects",
|
|
145
|
+
{"q": q, "limit": limit, "cursor": cursor},
|
|
146
|
+
)
|
|
147
|
+
return SubjectPage.model_validate(payload)
|
|
148
|
+
|
|
149
|
+
async def list_near(
|
|
150
|
+
self,
|
|
151
|
+
lat: float,
|
|
152
|
+
lng: float,
|
|
153
|
+
*,
|
|
154
|
+
radius: int | None = None,
|
|
155
|
+
limit: int | None = None,
|
|
156
|
+
) -> builtins.list[NearbySubject]:
|
|
157
|
+
"""Find spatial subjects near a point, closest first (radius in meters)."""
|
|
158
|
+
payload = await self._client.get(
|
|
159
|
+
"/subjects", {"near": f"{lat},{lng}", "radius": radius, "limit": limit}
|
|
160
|
+
)
|
|
161
|
+
return _NearbyPage.model_validate(payload).subjects
|
|
162
|
+
|
|
163
|
+
async def retrieve(self, subject_id: str) -> Subject:
|
|
164
|
+
"""Fetch one subject by its public identifier."""
|
|
165
|
+
payload = await self._client.get(_subject_path(subject_id), {})
|
|
166
|
+
return Subject.model_validate(payload)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Public response types (``from negirau.types import Subject``)."""
|
|
3
|
+
|
|
4
|
+
from negirau.types.display_value import DisplayValue
|
|
5
|
+
from negirau.types.reactions_summary import ReactionsSummary
|
|
6
|
+
from negirau.types.subject import NearbySubject, Subject
|
|
7
|
+
from negirau.types.subject_page import SubjectPage
|
|
8
|
+
|
|
9
|
+
__all__ = (
|
|
10
|
+
"DisplayValue",
|
|
11
|
+
"NearbySubject",
|
|
12
|
+
"ReactionsSummary",
|
|
13
|
+
"Subject",
|
|
14
|
+
"SubjectPage",
|
|
15
|
+
)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Reaction summary models."""
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel
|
|
5
|
+
|
|
6
|
+
from negirau.types.display_value import DisplayValue
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ReactionsSummary(BaseModel):
|
|
10
|
+
"""Reaction display values for one subject over a period."""
|
|
11
|
+
|
|
12
|
+
id: str
|
|
13
|
+
period: str
|
|
14
|
+
total: DisplayValue
|
|
15
|
+
by_type: dict[str, DisplayValue]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Subject models."""
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Subject(BaseModel):
|
|
8
|
+
"""A recipient of appreciation — a name, optionally with a location and a period."""
|
|
9
|
+
|
|
10
|
+
id: str
|
|
11
|
+
name: str
|
|
12
|
+
lat: float | None
|
|
13
|
+
lng: float | None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class NearbySubject(Subject):
|
|
17
|
+
"""A subject from a near search, with its distance from the query point."""
|
|
18
|
+
|
|
19
|
+
distance_m: int
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Cursor-paginated subject listing."""
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel
|
|
5
|
+
|
|
6
|
+
from negirau.types.subject import Subject
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SubjectPage(BaseModel):
|
|
10
|
+
"""One page of a cursor-paginated subject listing."""
|
|
11
|
+
|
|
12
|
+
subjects: list[Subject]
|
|
13
|
+
next_cursor: str | None
|