pdc-api 0.4.1__py3-none-any.whl
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.
- pdc_api/__init__.py +37 -0
- pdc_api/_base.py +6 -0
- pdc_api/_generated.py +873 -0
- pdc_api/client.py +332 -0
- pdc_api/errors.py +61 -0
- pdc_api/models.py +136 -0
- pdc_api/permission_grants.py +307 -0
- pdc_api/py.typed +0 -0
- pdc_api/seeding/__init__.py +0 -0
- pdc_api/seeding/keycloak_admin.py +179 -0
- pdc_api/seeding/pdc_entities.py +711 -0
- pdc_api/testing.py +314 -0
- pdc_api/token.py +69 -0
- pdc_api/transport.py +100 -0
- pdc_api-0.4.1.dist-info/METADATA +276 -0
- pdc_api-0.4.1.dist-info/RECORD +18 -0
- pdc_api-0.4.1.dist-info/WHEEL +4 -0
- pdc_api-0.4.1.dist-info/licenses/LICENSE +661 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pdc-api
|
|
3
|
+
Version: 0.4.1
|
|
4
|
+
Summary: Python client for the Philanthropy Data Commons (PDC) API
|
|
5
|
+
Keywords: philanthropy,philanthropy data commons,pdc,grantmaking,api client
|
|
6
|
+
Author: Philanthropy Data Commons
|
|
7
|
+
License-Expression: AGPL-3.0-only
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Dist: pydantic>=2.7
|
|
18
|
+
Requires-Dist: requests>=2.34
|
|
19
|
+
Requires-Dist: urllib3>=2
|
|
20
|
+
Requires-Dist: faker>=26 ; extra == 'seeding'
|
|
21
|
+
Requires-Dist: python-keycloak>=6 ; extra == 'seeding'
|
|
22
|
+
Requires-Dist: responses>=0.23 ; extra == 'testing'
|
|
23
|
+
Maintainer: DrivenData
|
|
24
|
+
Maintainer-email: DrivenData <info@drivendata.org>
|
|
25
|
+
Requires-Python: >=3.12
|
|
26
|
+
Project-URL: Repository, https://github.com/PhilanthropyDataCommons/pdc-api-python
|
|
27
|
+
Project-URL: Issues, https://github.com/PhilanthropyDataCommons/pdc-api-python/issues
|
|
28
|
+
Project-URL: Changelog, https://github.com/PhilanthropyDataCommons/pdc-api-python/blob/main/CHANGELOG.md
|
|
29
|
+
Project-URL: PDC API, https://api.philanthropydatacommons.org/
|
|
30
|
+
Provides-Extra: seeding
|
|
31
|
+
Provides-Extra: testing
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# pdc-api-python
|
|
35
|
+
|
|
36
|
+
A Python client for the [Philanthropy Data Commons](https://philanthropydatacommons.org) (PDC) API.
|
|
37
|
+
|
|
38
|
+
PDC is an access-controlled service where changemakers and funders share funding proposals and organization data. A changemaker can submit a proposal once and share it with any funder it chooses, and the data owner decides who sees what. This package wraps the [PDC API](https://api.philanthropydatacommons.org/) in typed Pydantic models, handles pagination, and raises one exception class per HTTP error. It doesn't depend on any web framework.
|
|
39
|
+
|
|
40
|
+
Install it as `pdc-api` and import it as `pdc_api`.
|
|
41
|
+
|
|
42
|
+
> **Status:** alpha. The API may change between minor versions until 1.0.
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
Install it from [PyPI](https://pypi.org/project/pdc-api/):
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install pdc-api
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
or with uv:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
uv add pdc-api
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Two optional extras add test and seeding helpers:
|
|
59
|
+
|
|
60
|
+
- **`testing`:** helpers for mocking PDC responses in your own tests, built on [`responses`](https://github.com/getsentry/responses).
|
|
61
|
+
- **`seeding`:** helpers for creating PDC and Keycloak test data, built on [`python-keycloak`](https://github.com/marcospereirampj/python-keycloak) and [Faker](https://github.com/joke2k/faker).
|
|
62
|
+
|
|
63
|
+
Add them to the requirement, as in `pdc-api[testing,seeding]`.
|
|
64
|
+
|
|
65
|
+
Python 3.12 or later is required.
|
|
66
|
+
|
|
67
|
+
## Quickstart
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from pdc_api import APISession
|
|
71
|
+
from pdc_api.client import get_changemakers, get_proposal
|
|
72
|
+
from pdc_api.token import KeycloakClientCredentialsProvider
|
|
73
|
+
|
|
74
|
+
session = APISession(
|
|
75
|
+
base_url="https://api.philanthropydatacommons.org/",
|
|
76
|
+
token_provider=KeycloakClientCredentialsProvider(
|
|
77
|
+
server_url="https://auth.philanthropydatacommons.org",
|
|
78
|
+
realm="pdc",
|
|
79
|
+
client_id="your-client-id",
|
|
80
|
+
client_secret="your-client-secret",
|
|
81
|
+
timeout=15,
|
|
82
|
+
),
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
for changemaker in get_changemakers(session):
|
|
86
|
+
print(changemaker.name, changemaker.tax_id)
|
|
87
|
+
|
|
88
|
+
proposal = get_proposal(session, proposal_id=42)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Every response is parsed into a model from `pdc_api.models`, with snake_case attributes over PDC's camelCase fields.
|
|
92
|
+
|
|
93
|
+
## Authentication
|
|
94
|
+
|
|
95
|
+
`APISession` asks a token provider for a bearer token before each request. A token provider is any object with these two methods:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
class TokenProvider(Protocol):
|
|
99
|
+
def get_token(self) -> str: ...
|
|
100
|
+
def refresh_if_necessary(self) -> None: ...
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The package ships one: `KeycloakClientCredentialsProvider` runs the OAuth2 client-credentials grant against PDC's Keycloak and caches the token until 30 seconds before it expires. Use it for a service account.
|
|
104
|
+
|
|
105
|
+
To call PDC as a signed-in user, write your own provider that returns the user's access token and refreshes it as your app requires. The protocol is structural, so your class doesn't need to import or subclass anything from this package.
|
|
106
|
+
|
|
107
|
+
PDC applies the caller's permissions to every request. The same call can return different records for different tokens, and a record the caller may not view comes back as a 404, not a 403.
|
|
108
|
+
|
|
109
|
+
## Reading data
|
|
110
|
+
|
|
111
|
+
There are two ways to read a list.
|
|
112
|
+
|
|
113
|
+
**Iterate everything.** `get_changemakers`, `get_funders`, `get_proposals`, `get_initiatives`, and the other plural `get_*` functions are generators that walk every page for you. The exception is `get_base_fields`, which returns a list:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from pdc_api.client import get_changemaker_proposals
|
|
117
|
+
|
|
118
|
+
for link in get_changemaker_proposals(session, changemaker_id=7):
|
|
119
|
+
print(link.proposal_id)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Fetch one page.** `get_bundle_page` returns a single page as a bundle, which is what a paginated UI needs. It takes the bundle type, a page number, an optional page size, and an optional search string:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from pdc_api.client import get_bundle_page
|
|
126
|
+
from pdc_api.models import FunderBundle
|
|
127
|
+
|
|
128
|
+
page = get_bundle_page(session, FunderBundle, page=2, count_per_page=25, content="climate")
|
|
129
|
+
print(page.total, [funder.name for funder in page.entries])
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Bundles for nested endpoints take path parameters, such as `get_bundle_page(session, FunderCollaborativeMemberBundle, path_params={"funder_collaborative_short_code": "acme"})`. Query parameters that `get_bundle_page` doesn't name go in `extra_params`.
|
|
133
|
+
|
|
134
|
+
## Writing data
|
|
135
|
+
|
|
136
|
+
The client covers the writes a proposal workflow needs: creating a proposal, adding a version with field values, linking a changemaker to it, and uploading files.
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from pdc_api.client import create_proposal, create_proposal_version
|
|
140
|
+
from pdc_api.models import NewProposalFieldValue, NewProposalVersion
|
|
141
|
+
|
|
142
|
+
proposal = create_proposal(session, data={"opportunityId": 5, "externalId": "app-2026-001"})
|
|
143
|
+
version = create_proposal_version(
|
|
144
|
+
session,
|
|
145
|
+
proposal_version=NewProposalVersion(
|
|
146
|
+
proposal_id=proposal.id,
|
|
147
|
+
source_id=3,
|
|
148
|
+
application_form_id=12,
|
|
149
|
+
field_values=[NewProposalFieldValue(application_form_field_id=88, position=1, value="Summer meals")],
|
|
150
|
+
),
|
|
151
|
+
)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
A changemaker can also write its own profile values. `create_changemaker_source` creates a source the changemaker owns, `create_changemaker_field_value_batch` opens a batch under it, and `create_changemaker_field_value` adds one value to the batch. PDC ranks changemaker-sourced values above funder and data-provider values.
|
|
155
|
+
|
|
156
|
+
`create_initiative` creates an initiative led by a changemaker, and `create_initiative_field_value` adds a value to it from a given source.
|
|
157
|
+
|
|
158
|
+
Uploading a file takes two steps. `create_file` creates the file record and returns a presigned S3 POST, and `upload_file_to_s3_presigned_url` sends the bytes to it.
|
|
159
|
+
|
|
160
|
+
## Permission grants
|
|
161
|
+
|
|
162
|
+
PDC controls access with permission grants. Each grant gives a grantee (a user, a Keycloak organization, or every authenticated user) a set of verbs (such as `view` or `edit`) over a set of scopes, in the context of one entity such as a changemaker or a proposal. `pdc_api.permission_grants` models each context as its own class, so a grant parses into, say, `ChangemakerPermissionGrant` with a typed `changemaker_id`:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from pdc_api.models import PermissionGrantEntityType, PermissionGrantGranteeType, PermissionGrantVerb
|
|
166
|
+
from pdc_api.permission_grants import ChangemakerPermissionGrant, create_permission_grant, list_permission_grants
|
|
167
|
+
|
|
168
|
+
grant = create_permission_grant(
|
|
169
|
+
session,
|
|
170
|
+
grant=ChangemakerPermissionGrant(
|
|
171
|
+
changemaker_id=7,
|
|
172
|
+
grantee_type=PermissionGrantGranteeType.USER_GROUP,
|
|
173
|
+
grantee_keycloak_organization_id="1f0c6e2a-7d4b-4c55-9a51-0d8c2f0b6a11",
|
|
174
|
+
verbs=[PermissionGrantVerb.VIEW],
|
|
175
|
+
scope=[PermissionGrantEntityType.CHANGEMAKER, PermissionGrantEntityType.PROPOSAL],
|
|
176
|
+
),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
page = list_permission_grants(session, changemaker_id=7, verb=PermissionGrantVerb.VIEW)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
The models check that the grantee fields match `grantee_type` before anything is sent. Create, get, update, and delete functions exist for grants and for default permission grants. A default permission grant has no context entity; PDC copies it into a real grant on each new entity of its type as that entity is created.
|
|
183
|
+
|
|
184
|
+
## Errors
|
|
185
|
+
|
|
186
|
+
`APISession` raises a subclass of `pdc_api.PDCException` for every failed request. Each exception has `.code` (the HTTP status) and `.detail` (the parsed JSON body, or the raw text when the body isn't JSON). Before raising, it retries a GET or HEAD up to three times on a 502, 503, or 504 or a dropped connection.
|
|
187
|
+
|
|
188
|
+
| Status | Exception |
|
|
189
|
+
| ------------- | ----------------------------- |
|
|
190
|
+
| 400 | `PDCBadRequestError` |
|
|
191
|
+
| 401 | `PDCUnauthorizedError` |
|
|
192
|
+
| 403 | `PDCForbiddenError` |
|
|
193
|
+
| 404 | `PDCNotFoundError` |
|
|
194
|
+
| 409 | `PDCConflictError` |
|
|
195
|
+
| 422 | `PDCUnprocessableEntityError` |
|
|
196
|
+
| 5xx, timeout, or connection failure | `PDCServiceError` |
|
|
197
|
+
| Any other non-2xx | `PDCException` |
|
|
198
|
+
|
|
199
|
+
A few lookups raise a subclass of `PDCNotFoundError` named for the resource:
|
|
200
|
+
|
|
201
|
+
- `get_proposal` and `get_changemaker_proposal` raise `ProposalNotFoundError`.
|
|
202
|
+
- `get_changemaker` raises `ChangemakerNotFoundError`.
|
|
203
|
+
- `get_funder` raises `FunderNotFoundError`.
|
|
204
|
+
- `get_changemaker_field_value_batch` raises `ChangemakerFieldValueBatchNotFoundError`.
|
|
205
|
+
- `get_application_form_by_name` raises `ApplicationFormNotFoundError` when no form has that name.
|
|
206
|
+
|
|
207
|
+
`KeycloakClientCredentialsProvider` raises `TokenRequestError` when Keycloak rejects the grant.
|
|
208
|
+
|
|
209
|
+
## Models
|
|
210
|
+
|
|
211
|
+
Most models are generated from PDC's OpenAPI spec, which is pinned in `src/pdc_api/openapi.json`. Import them from `pdc_api.models`; `pdc_api._generated` is an implementation detail.
|
|
212
|
+
|
|
213
|
+
The models ignore fields they don't declare. When PDC adds a field to a response, parsing keeps working, and the new field appears once the models are regenerated.
|
|
214
|
+
|
|
215
|
+
The spec doesn't always match what PDC returns. Some fields it marks required are missing or null in real responses, and a few shapes don't generate usable models. `codegen/preprocess.py` patches each mismatch before generation, one function per problem. Each rule names the mismatch it patches, and should be deleted once PDC's spec matches its responses. We reported the first batch in [PhilanthropyDataCommons/service#2451](https://github.com/PhilanthropyDataCommons/service/issues/2451).
|
|
216
|
+
|
|
217
|
+
## What's not covered yet
|
|
218
|
+
|
|
219
|
+
The client covers most of the API but not all of it:
|
|
220
|
+
|
|
221
|
+
- **No client functions:** `/applicationFormFields`, `/sources` (apart from `create_changemaker_source`), `/tasks` (bulk uploads and base field copies), `/terminologySets`, and `/users`. Their models are generated, and `session.get`/`session.post` reach any endpoint directly.
|
|
222
|
+
- **Other writes:** creating funders, changemakers, opportunities, and funder or data-provider sources is possible only through the seeding helpers in `pdc_api.seeding`, which aren't meant for production data.
|
|
223
|
+
- **Async:** the client is synchronous and built on `requests`.
|
|
224
|
+
|
|
225
|
+
Issues and pull requests that fill these gaps are welcome.
|
|
226
|
+
|
|
227
|
+
## Testing your code
|
|
228
|
+
|
|
229
|
+
`pdc_api.testing` (extra: `testing`) registers mocked PDC responses with `responses`. It has payload builders that return valid response bodies, and `add_*_response` functions that register them for a base URL:
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
import responses
|
|
233
|
+
|
|
234
|
+
from pdc_api import APISession, testing
|
|
235
|
+
from pdc_api.client import get_bundle_page
|
|
236
|
+
from pdc_api.models import ChangemakerBundle
|
|
237
|
+
|
|
238
|
+
BASE_URL = "https://pdc.test/"
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
class StaticToken:
|
|
242
|
+
def get_token(self) -> str:
|
|
243
|
+
return "test-token"
|
|
244
|
+
|
|
245
|
+
def refresh_if_necessary(self) -> None:
|
|
246
|
+
pass
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
@responses.activate
|
|
250
|
+
def test_lists_changemakers():
|
|
251
|
+
testing.add_page_response(
|
|
252
|
+
ChangemakerBundle,
|
|
253
|
+
base_url=BASE_URL,
|
|
254
|
+
payload=testing.bundle([testing.changemaker_payload(id=1, name="Acme")]),
|
|
255
|
+
)
|
|
256
|
+
session = APISession(base_url=BASE_URL, token_provider=StaticToken())
|
|
257
|
+
|
|
258
|
+
page = get_bundle_page(session, ChangemakerBundle)
|
|
259
|
+
|
|
260
|
+
assert page.entries[0].name == "Acme"
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Seeding a PDC stack
|
|
264
|
+
|
|
265
|
+
`pdc_api.seeding` (extra: `seeding`) creates realistic data on a PDC stack through the public PDC API and the Keycloak admin API. It is for local development and integration tests, not production:
|
|
266
|
+
|
|
267
|
+
- **`pdc_api.seeding.pdc_entities`:** `seed_*` functions for funders, collaboratives, changemakers, fiscal sponsorships, data providers, opportunities, application forms, proposals, initiatives, and field values. `seed_proposal_bundle` creates a full proposal and everything it depends on in one call.
|
|
268
|
+
- **`pdc_api.seeding.keycloak_admin`:** `KeycloakAdmin`, whose `ensure_*` methods create Keycloak organizations, users, memberships, and realm roles only if they don't already exist, so re-running a seed script is safe.
|
|
269
|
+
|
|
270
|
+
## Contributing
|
|
271
|
+
|
|
272
|
+
See [CONTRIBUTING.md](https://github.com/PhilanthropyDataCommons/pdc-api-python/blob/main/CONTRIBUTING.md) for development setup, tests, and regenerating the models.
|
|
273
|
+
|
|
274
|
+
## License
|
|
275
|
+
|
|
276
|
+
[AGPL-3.0](https://github.com/PhilanthropyDataCommons/pdc-api-python/blob/main/LICENSE). Maintained by [DrivenData](https://drivendata.co/).
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
pdc_api/__init__.py,sha256=S_VBhGJevfb7X2adnmLudRIj5E8h7j7xcs0gQ9Npt04,940
|
|
2
|
+
pdc_api/_base.py,sha256=VXGxyB8BooUbu8QVWheyxdyUnGlzq6FmSmf3jz7QtVU,231
|
|
3
|
+
pdc_api/_generated.py,sha256=qsyxQRshzqYGySgLDAQs6rvB0JRhXbwFQJfvJuNVhdo,34141
|
|
4
|
+
pdc_api/client.py,sha256=jsQXHDj8i4XhxNzEvetQ_C1jylyqcIj2NqwePwvZW3M,12512
|
|
5
|
+
pdc_api/errors.py,sha256=uxbvzoqwFxeVnMlQSvjRdQAM1-diSiMeqzkQVQ9jndA,1021
|
|
6
|
+
pdc_api/models.py,sha256=iTbPKLtF4NUqsAzhfcFjiG2UXmQR6JVXRsZzkCTC8BY,3578
|
|
7
|
+
pdc_api/permission_grants.py,sha256=wBnK_hW-nwnzfqjAFgjwBGVYrUEXKSiaygv3D2zF2Ec,12687
|
|
8
|
+
pdc_api/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
pdc_api/seeding/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
10
|
+
pdc_api/seeding/keycloak_admin.py,sha256=fZu5kpzkxtEogdtl9S6f1w1YweYIQ39C4it_Be6nLxA,7194
|
|
11
|
+
pdc_api/seeding/pdc_entities.py,sha256=K0ONhRhg0LlBE629e9vFfwPhNWELehY5EJ89zgMHfH8,22891
|
|
12
|
+
pdc_api/testing.py,sha256=uBRyruRa-RehDAMvAh0Y_ygfXHCxuLYymT33UWT8WRQ,9208
|
|
13
|
+
pdc_api/token.py,sha256=3n4uZOALLD9pqf-KI_FGsm6goawk0caAXZof4Y_RYR0,2419
|
|
14
|
+
pdc_api/transport.py,sha256=7f4Ixc_TQcdHrmgHlQtsdGq1guTCALrwEbb0eguhSx8,3925
|
|
15
|
+
pdc_api-0.4.1.dist-info/licenses/LICENSE,sha256=DZak_2itbUtvHzD3E7GNUYSRK6jdOJ-GqncQ2weavLA,34523
|
|
16
|
+
pdc_api-0.4.1.dist-info/WHEEL,sha256=G_-rrEibZr7OEM7zdH1dSO6s-VdjfnS5PA4CLAdI9Vw,80
|
|
17
|
+
pdc_api-0.4.1.dist-info/METADATA,sha256=UbEy7kPchqdwuObf6kIPSUrH90RO_6ZcGXwPMm4AWzI,13391
|
|
18
|
+
pdc_api-0.4.1.dist-info/RECORD,,
|