reportnet-api 0.1.0__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.
- reportnet_api/__init__.py +38 -0
- reportnet_api/_auth.py +30 -0
- reportnet_api/_core.py +222 -0
- reportnet_api/_exceptions.py +243 -0
- reportnet_api/async_client.py +113 -0
- reportnet_api/cli.py +212 -0
- reportnet_api/client.py +114 -0
- reportnet_api/demo.py +148 -0
- reportnet_api/discover.py +106 -0
- reportnet_api/easy.py +1359 -0
- reportnet_api/handles.py +326 -0
- reportnet_api/jobs.py +383 -0
- reportnet_api/models/__init__.py +0 -0
- reportnet_api/models/generated.py +555 -0
- reportnet_api/py.typed +0 -0
- reportnet_api/resources/__init__.py +0 -0
- reportnet_api/resources/dataflow_ext.py +239 -0
- reportnet_api/resources/dataset_schema.py +274 -0
- reportnet_api/resources/dataset_schema_ext.py +589 -0
- reportnet_api/resources/dataset_snapshot.py +173 -0
- reportnet_api/resources/datasets.py +1810 -0
- reportnet_api/resources/datasets_ext.py +1001 -0
- reportnet_api/resources/eu_datasets.py +44 -0
- reportnet_api/resources/reference_dataset_ext.py +75 -0
- reportnet_api/resources/release_receipts.py +122 -0
- reportnet_api/resources/representative_ext.py +366 -0
- reportnet_api/resources/user_ext.py +53 -0
- reportnet_api/resources/validation_ext.py +447 -0
- reportnet_api-0.1.0.dist-info/METADATA +307 -0
- reportnet_api-0.1.0.dist-info/RECORD +33 -0
- reportnet_api-0.1.0.dist-info/WHEEL +4 -0
- reportnet_api-0.1.0.dist-info/entry_points.txt +2 -0
- reportnet_api-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Generated from spec/api-docs.json by scripts/generate_resources.py.
|
|
2
|
+
# Do not edit by hand.
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from ..async_client import AsyncReportnetClient
|
|
10
|
+
from ..client import ReportnetClient
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class EuDatasetsResource:
|
|
14
|
+
def __init__(self, client: ReportnetClient) -> None:
|
|
15
|
+
self._client = client
|
|
16
|
+
|
|
17
|
+
def populate_data_from_data_collection(
|
|
18
|
+
self,
|
|
19
|
+
dataflow_id: int,
|
|
20
|
+
job_id: int | None = None,
|
|
21
|
+
) -> None:
|
|
22
|
+
"""Allowed roles: CUSTODIAN, STEWARD"""
|
|
23
|
+
params = {"jobId": job_id}
|
|
24
|
+
params = {k: v for k, v in params.items() if v is not None}
|
|
25
|
+
url = f"/euDataset/v1/populateData/dataflow/{dataflow_id}"
|
|
26
|
+
self._client.request("POST", url, params=params)
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class AsyncEuDatasetsResource:
|
|
31
|
+
def __init__(self, client: AsyncReportnetClient) -> None:
|
|
32
|
+
self._client = client
|
|
33
|
+
|
|
34
|
+
async def populate_data_from_data_collection(
|
|
35
|
+
self,
|
|
36
|
+
dataflow_id: int,
|
|
37
|
+
job_id: int | None = None,
|
|
38
|
+
) -> None:
|
|
39
|
+
"""Allowed roles: CUSTODIAN, STEWARD"""
|
|
40
|
+
params = {"jobId": job_id}
|
|
41
|
+
params = {k: v for k, v in params.items() if v is not None}
|
|
42
|
+
url = f"/euDataset/v1/populateData/dataflow/{dataflow_id}"
|
|
43
|
+
await self._client.request("POST", url, params=params)
|
|
44
|
+
return None
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Hand-written Reference-dataset resource (lock / unlock a reference dataset).
|
|
2
|
+
|
|
3
|
+
`ReferenceDatasetControllerImpl` (`dataset-service`) is `@ApiIgnore` and its
|
|
4
|
+
`PUT /referenceDataset/{datasetId}` is `@ApiOperation(hidden = true)`, so it is
|
|
5
|
+
absent from spec/api-docs.json ("Known spec-snapshot gap"). The
|
|
6
|
+
controller and `ReferenceDatasetServiceImpl#updateUpdatableReferenceDataset`
|
|
7
|
+
are identical on the `develop` and `MasterDataLakes` branches of
|
|
8
|
+
the Reportnet 3 backend repository. The controller's other routes are `/private/*` (stripped by
|
|
9
|
+
the gateway) or read-only lookups and are not covered. This file is never
|
|
10
|
+
touched by the generators. Mocked-test only: nothing has been run live.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from typing import TYPE_CHECKING
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from ..async_client import AsyncReportnetClient
|
|
19
|
+
from ..client import ReportnetClient
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _bool_param(updatable: bool) -> str:
|
|
23
|
+
# Strict: bool("false") is True, which would silently unlock instead of lock.
|
|
24
|
+
if not isinstance(updatable, bool):
|
|
25
|
+
raise TypeError(f"updatable must be a bool, got {type(updatable).__name__}")
|
|
26
|
+
return "true" if updatable else "false"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ReferenceDatasetResource:
|
|
30
|
+
def __init__(self, client: ReportnetClient) -> None:
|
|
31
|
+
self._client = client
|
|
32
|
+
|
|
33
|
+
def set_updatable(self, dataset_id: int, updatable: bool) -> None:
|
|
34
|
+
"""Lock or unlock a reference dataset (`PUT /referenceDataset/{datasetId}`).
|
|
35
|
+
|
|
36
|
+
The backend sets the reference dataset's `updatable` flag. Setting it to
|
|
37
|
+
False ("lock") also generates the reference dataset's files
|
|
38
|
+
(`FileTreatmentHelper#createReferenceDatasetFiles`), so it is more than a
|
|
39
|
+
flag flip; True ("unlock") only saves the flag. Answers an empty 200.
|
|
40
|
+
`404` is returned both when the dataset is not a reference dataset and
|
|
41
|
+
for any I/O failure while generating the files (the controller maps
|
|
42
|
+
`EEAException`/`IOException` to the same 404). Roles (`@PreAuthorize`):
|
|
43
|
+
dataset/dataschema/reference-dataset custodian, steward, steward support,
|
|
44
|
+
observer or reference lead reporter on the dataset, or a data custodian/
|
|
45
|
+
steward with access to the reference entity; not documented for ApiKey.
|
|
46
|
+
This WRITES. A PUT is never retried after the request was sent (see
|
|
47
|
+
`RetryPolicy`), so a lock is not re-run by a retry.
|
|
48
|
+
|
|
49
|
+
Raises `TypeError` if `updatable` is not a real `bool`.
|
|
50
|
+
"""
|
|
51
|
+
self._client.request(
|
|
52
|
+
"PUT",
|
|
53
|
+
f"/referenceDataset/{dataset_id}",
|
|
54
|
+
params={"updatable": _bool_param(updatable)},
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class AsyncReferenceDatasetResource:
|
|
59
|
+
def __init__(self, client: AsyncReportnetClient) -> None:
|
|
60
|
+
self._client = client
|
|
61
|
+
|
|
62
|
+
async def set_updatable(self, dataset_id: int, updatable: bool) -> None:
|
|
63
|
+
"""Async counterpart of `ReferenceDatasetResource.set_updatable`.
|
|
64
|
+
|
|
65
|
+
Lock or unlock a reference dataset (`PUT /referenceDataset/{datasetId}`).
|
|
66
|
+
This WRITES, and `updatable=False` ("lock") also makes the backend
|
|
67
|
+
generate the reference dataset's files. Answers an empty 200; `404` for a
|
|
68
|
+
non-reference dataset or a file-generation failure. Raises `TypeError` if
|
|
69
|
+
`updatable` is not a real `bool`.
|
|
70
|
+
"""
|
|
71
|
+
await self._client.request(
|
|
72
|
+
"PUT",
|
|
73
|
+
f"/referenceDataset/{dataset_id}",
|
|
74
|
+
params={"updatable": _bool_param(updatable)},
|
|
75
|
+
)
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Generated from spec/api-docs.json by scripts/generate_resources.py.
|
|
2
|
+
# Do not edit by hand.
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
from ..models import generated
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from ..async_client import AsyncReportnetClient
|
|
12
|
+
from ..client import ReportnetClient
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ReleaseReceiptsResource:
|
|
16
|
+
def __init__(self, client: ReportnetClient) -> None:
|
|
17
|
+
self._client = client
|
|
18
|
+
|
|
19
|
+
def create_release_receipt(
|
|
20
|
+
self,
|
|
21
|
+
release_receipt_vo: generated.ReleaseReceiptVO,
|
|
22
|
+
) -> generated.ReleaseReceiptVO:
|
|
23
|
+
"""createReleaseReceipt"""
|
|
24
|
+
params = None
|
|
25
|
+
url = "/release-receipts"
|
|
26
|
+
response = self._client.request(
|
|
27
|
+
"POST",
|
|
28
|
+
url,
|
|
29
|
+
params=params,
|
|
30
|
+
json=release_receipt_vo.model_dump(mode="json", exclude_none=True),
|
|
31
|
+
)
|
|
32
|
+
return generated.ReleaseReceiptVO.model_validate(response.json())
|
|
33
|
+
|
|
34
|
+
def delete_release_receipt_by_dataflow_id(
|
|
35
|
+
self,
|
|
36
|
+
dataflow_id: int,
|
|
37
|
+
) -> None:
|
|
38
|
+
"""deleteReleaseReceiptByDataflowId"""
|
|
39
|
+
params = None
|
|
40
|
+
url = f"/release-receipts/dataflow/{dataflow_id}"
|
|
41
|
+
self._client.request("DELETE", url, params=params)
|
|
42
|
+
return None
|
|
43
|
+
|
|
44
|
+
def get_release_receipt_by_dataflow_id(
|
|
45
|
+
self,
|
|
46
|
+
dataflow_id: int,
|
|
47
|
+
) -> generated.ReleaseReceiptVO:
|
|
48
|
+
"""getReleaseReceiptByDataflowId"""
|
|
49
|
+
params = None
|
|
50
|
+
url = f"/release-receipts/dataflow/{dataflow_id}"
|
|
51
|
+
response = self._client.request("GET", url, params=params)
|
|
52
|
+
return generated.ReleaseReceiptVO.model_validate(response.json())
|
|
53
|
+
|
|
54
|
+
def update_release_receipt(
|
|
55
|
+
self,
|
|
56
|
+
release_receipt_vo: generated.ReleaseReceiptVO,
|
|
57
|
+
) -> generated.ReleaseReceiptVO:
|
|
58
|
+
"""updateReleaseReceipt"""
|
|
59
|
+
params = None
|
|
60
|
+
url = "/release-receipts"
|
|
61
|
+
response = self._client.request(
|
|
62
|
+
"PUT",
|
|
63
|
+
url,
|
|
64
|
+
params=params,
|
|
65
|
+
json=release_receipt_vo.model_dump(mode="json", exclude_none=True),
|
|
66
|
+
)
|
|
67
|
+
return generated.ReleaseReceiptVO.model_validate(response.json())
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class AsyncReleaseReceiptsResource:
|
|
71
|
+
def __init__(self, client: AsyncReportnetClient) -> None:
|
|
72
|
+
self._client = client
|
|
73
|
+
|
|
74
|
+
async def create_release_receipt(
|
|
75
|
+
self,
|
|
76
|
+
release_receipt_vo: generated.ReleaseReceiptVO,
|
|
77
|
+
) -> generated.ReleaseReceiptVO:
|
|
78
|
+
"""createReleaseReceipt"""
|
|
79
|
+
params = None
|
|
80
|
+
url = "/release-receipts"
|
|
81
|
+
response = await self._client.request(
|
|
82
|
+
"POST",
|
|
83
|
+
url,
|
|
84
|
+
params=params,
|
|
85
|
+
json=release_receipt_vo.model_dump(mode="json", exclude_none=True),
|
|
86
|
+
)
|
|
87
|
+
return generated.ReleaseReceiptVO.model_validate(response.json())
|
|
88
|
+
|
|
89
|
+
async def delete_release_receipt_by_dataflow_id(
|
|
90
|
+
self,
|
|
91
|
+
dataflow_id: int,
|
|
92
|
+
) -> None:
|
|
93
|
+
"""deleteReleaseReceiptByDataflowId"""
|
|
94
|
+
params = None
|
|
95
|
+
url = f"/release-receipts/dataflow/{dataflow_id}"
|
|
96
|
+
await self._client.request("DELETE", url, params=params)
|
|
97
|
+
return None
|
|
98
|
+
|
|
99
|
+
async def get_release_receipt_by_dataflow_id(
|
|
100
|
+
self,
|
|
101
|
+
dataflow_id: int,
|
|
102
|
+
) -> generated.ReleaseReceiptVO:
|
|
103
|
+
"""getReleaseReceiptByDataflowId"""
|
|
104
|
+
params = None
|
|
105
|
+
url = f"/release-receipts/dataflow/{dataflow_id}"
|
|
106
|
+
response = await self._client.request("GET", url, params=params)
|
|
107
|
+
return generated.ReleaseReceiptVO.model_validate(response.json())
|
|
108
|
+
|
|
109
|
+
async def update_release_receipt(
|
|
110
|
+
self,
|
|
111
|
+
release_receipt_vo: generated.ReleaseReceiptVO,
|
|
112
|
+
) -> generated.ReleaseReceiptVO:
|
|
113
|
+
"""updateReleaseReceipt"""
|
|
114
|
+
params = None
|
|
115
|
+
url = "/release-receipts"
|
|
116
|
+
response = await self._client.request(
|
|
117
|
+
"PUT",
|
|
118
|
+
url,
|
|
119
|
+
params=params,
|
|
120
|
+
json=release_receipt_vo.model_dump(mode="json", exclude_none=True),
|
|
121
|
+
)
|
|
122
|
+
return generated.ReleaseReceiptVO.model_validate(response.json())
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
"""Hand-written Representative resource (providers and a dataflow's representatives).
|
|
2
|
+
|
|
3
|
+
The Representative tag is absent from spec/api-docs.json ("Known
|
|
4
|
+
spec-snapshot gap"), and the provider lookups are `@ApiOperation(hidden =
|
|
5
|
+
true)` in `RepresentativeControllerImpl` (`dataflow-service`), as are the
|
|
6
|
+
lead reporters export/import and the create/validate routes (written from the
|
|
7
|
+
`develop` source). This file is never
|
|
8
|
+
touched by the generators.
|
|
9
|
+
|
|
10
|
+
Roles: the read routes take a dataflow role or an ApiKey (see each method).
|
|
11
|
+
Everything that exports, imports or creates (`export_lead_reporters_file`,
|
|
12
|
+
`import_lead_reporters_file`, `create_representative`, `create_lead_reporter`,
|
|
13
|
+
`validate_lead_reporters`) is `secondLevelAuthorize(dataflowId,
|
|
14
|
+
DATAFLOW_CUSTODIAN | DATAFLOW_STEWARD | DATAFLOW_STEWARD_SUPPORT)`: no ApiKey
|
|
15
|
+
branch, so only such a user's login token (Bearer) passes; an ApiKey gets 403.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import os
|
|
21
|
+
from typing import TYPE_CHECKING, Any, cast
|
|
22
|
+
|
|
23
|
+
from .._core import ACCEPT_ANY
|
|
24
|
+
from .._exceptions import ReportnetAPIError
|
|
25
|
+
|
|
26
|
+
if TYPE_CHECKING:
|
|
27
|
+
import httpx
|
|
28
|
+
|
|
29
|
+
from ..async_client import AsyncReportnetClient
|
|
30
|
+
from ..client import ReportnetClient
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _created_id(response: httpx.Response, what: str) -> int:
|
|
34
|
+
"""The new id a create route answers (a JSON `Long`, so plain ASCII digits)."""
|
|
35
|
+
text = response.text.strip().strip('"')
|
|
36
|
+
if not (text.isascii() and text.isdecimal()):
|
|
37
|
+
raise ReportnetAPIError(
|
|
38
|
+
f"Creating a {what} answered {response.status_code} without an id: {text[:200]!r}",
|
|
39
|
+
status_code=response.status_code,
|
|
40
|
+
response_body=response.text,
|
|
41
|
+
request=response.request,
|
|
42
|
+
response=response,
|
|
43
|
+
)
|
|
44
|
+
return int(text)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _upload(file: str | os.PathLike[str]) -> dict[str, tuple[str, bytes, str]]:
|
|
48
|
+
"""The lead reporters CSV as Reportnet's import can read it, as an httpx `files=` mapping.
|
|
49
|
+
|
|
50
|
+
`importLeadReportersFile` drops the first line, splits the rest on `\\n`
|
|
51
|
+
and each line on the delimiter (`,`) without reading quotes, strips `"`
|
|
52
|
+
from both values and `\\r` only from the email. So: the name must end in
|
|
53
|
+
`.csv` (else 400), line ends become `\\n` (a CRLF file's `AT\\r` code
|
|
54
|
+
would match no provider), blank lines are dropped (each would come back
|
|
55
|
+
as an unknown-code error), the first line must be the `Representing,Email`
|
|
56
|
+
header, and every other line must have exactly two values (with more, the
|
|
57
|
+
backend silently ignores the email; this includes Reportnet's own import
|
|
58
|
+
result CSV, which has a third `Imported` column). Raises `ValueError`.
|
|
59
|
+
"""
|
|
60
|
+
name = os.path.basename(os.fspath(file))
|
|
61
|
+
if not name.lower().endswith(".csv"):
|
|
62
|
+
raise ValueError(f"The lead reporters file must be a .csv file: {name!r}")
|
|
63
|
+
with open(file, "rb") as fh:
|
|
64
|
+
text = fh.read().decode("utf-8-sig")
|
|
65
|
+
normalized = text.replace("\r\n", "\n").replace("\r", "\n")
|
|
66
|
+
lines = [line for line in normalized.split("\n") if line.strip()]
|
|
67
|
+
if not lines or lines[0].split(",")[0].strip().strip('"').casefold() != "representing":
|
|
68
|
+
raise ValueError(
|
|
69
|
+
f"{name}: the first line must be the header `Representing,Email` "
|
|
70
|
+
"(Reportnet discards the first line whatever it holds)"
|
|
71
|
+
)
|
|
72
|
+
for number, line in enumerate(lines[1:], start=2):
|
|
73
|
+
if len(line.split(",")) != 2:
|
|
74
|
+
raise ValueError(
|
|
75
|
+
f"{name}: data line {number} must have exactly two comma-separated values "
|
|
76
|
+
"(provider code and email; Reportnet does not read quotes, and a result CSV with "
|
|
77
|
+
"an `Imported` column can't be imported again)"
|
|
78
|
+
)
|
|
79
|
+
data = ("\n".join(lines) + "\n").encode("utf-8")
|
|
80
|
+
return {"file": (name, data, "text/csv")}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _representative_body(
|
|
84
|
+
data_provider_id: int, data_provider_group_id: int | None
|
|
85
|
+
) -> dict[str, Any]:
|
|
86
|
+
body: dict[str, Any] = {"dataProviderId": data_provider_id}
|
|
87
|
+
if data_provider_group_id is not None:
|
|
88
|
+
body["dataProviderGroupId"] = data_provider_group_id
|
|
89
|
+
return body
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _check_representative(
|
|
93
|
+
representative_id: int, dataflow_id: int, representatives: list[dict[str, Any]]
|
|
94
|
+
) -> None:
|
|
95
|
+
if not any(rep.get("id") == representative_id for rep in representatives or []):
|
|
96
|
+
raise ValueError(
|
|
97
|
+
f"Representative {representative_id} is not one of dataflow {dataflow_id}'s "
|
|
98
|
+
"representatives (Reportnet would add the lead reporter to it anyway)"
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
_EXPORT_PATH = "/representative/export/{}"
|
|
103
|
+
_IMPORT_PATH = "/representative/import/{}/group/{}"
|
|
104
|
+
_VALIDATE_PATH = "/representative/validateLeadReporters/dataflow/{}"
|
|
105
|
+
_CREATE_LEAD_PATH = "/representative/{}/leadReporter/dataflow/{}"
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class RepresentativeResource:
|
|
109
|
+
def __init__(self, client: ReportnetClient) -> None:
|
|
110
|
+
self._client = client
|
|
111
|
+
|
|
112
|
+
def representatives(self, dataflow_id: int) -> list[dict[str, Any]]:
|
|
113
|
+
"""GET /representative/v1/dataflow/{dataflowId}: a dataflow's representatives.
|
|
114
|
+
|
|
115
|
+
Each item has `id`, `dataProviderId` (the `providerId` other endpoints
|
|
116
|
+
take; resolve it with `data_provider()`), `dataProviderGroupId`,
|
|
117
|
+
`leadReporters` (a list that includes email addresses: personal data),
|
|
118
|
+
`receiptDownloaded`, `receiptOutdated`, `hasDatasets` and
|
|
119
|
+
`restrictFromPublic`. Backend rule: a dataflow role (custodian,
|
|
120
|
+
steward, editor, observer, lead reporter, reporter write, steward
|
|
121
|
+
support), an ApiKey for the dataflow, or ADMIN. A dataflow without
|
|
122
|
+
representatives (e.g. a design dataflow) gives an empty list.
|
|
123
|
+
"""
|
|
124
|
+
response = self._client.request("GET", f"/representative/v1/dataflow/{dataflow_id}")
|
|
125
|
+
return cast(list[dict[str, Any]], response.json())
|
|
126
|
+
|
|
127
|
+
def data_provider_groups(self) -> list[dict[str, Any]]:
|
|
128
|
+
"""GET /representative/dataProvider/countryGroups: the country provider groups.
|
|
129
|
+
|
|
130
|
+
Items are `{"dataProviderGroupId", "label"}` (note the key: the items of
|
|
131
|
+
`data_providers()` call the same id `groupId`); live there are two, `1`
|
|
132
|
+
"Member countries" and `2` "All countries: member countries
|
|
133
|
+
PLUS other countries and territories". Any authenticated user.
|
|
134
|
+
"""
|
|
135
|
+
response = self._client.request("GET", "/representative/dataProvider/countryGroups")
|
|
136
|
+
return cast(list[dict[str, Any]], response.json())
|
|
137
|
+
|
|
138
|
+
def data_providers(self, group_id: int) -> list[dict[str, Any]]:
|
|
139
|
+
"""GET /representative/dataProvider/{groupId}: the providers in a group.
|
|
140
|
+
|
|
141
|
+
Items are `{"id", "group", "label", "code", "groupId"}`: `id` is the
|
|
142
|
+
`providerId`, `code` the country code, `groupId` the group (same value
|
|
143
|
+
as `dataProviderGroupId` on `data_provider_groups()` items; `group` was
|
|
144
|
+
always `None` live). Live: group 1 has 33 member countries, e.g.
|
|
145
|
+
id 2 = Austria `AT`. Any authenticated user.
|
|
146
|
+
"""
|
|
147
|
+
response = self._client.request("GET", f"/representative/dataProvider/{group_id}")
|
|
148
|
+
return cast(list[dict[str, Any]], response.json())
|
|
149
|
+
|
|
150
|
+
def data_provider(self, data_provider_id: int) -> dict[str, Any]:
|
|
151
|
+
"""GET /representative/dataProvider/id/{id}: one provider (same fields as
|
|
152
|
+
`data_providers()` items). Any authenticated user.
|
|
153
|
+
"""
|
|
154
|
+
response = self._client.request(
|
|
155
|
+
"GET", f"/representative/dataProvider/id/{data_provider_id}"
|
|
156
|
+
)
|
|
157
|
+
return cast(dict[str, Any], response.json())
|
|
158
|
+
|
|
159
|
+
def export_lead_reporters_file(self, dataflow_id: int) -> bytes:
|
|
160
|
+
"""GET /representative/export/{dataflowId}: the web app's "Export lead reporters" CSV.
|
|
161
|
+
|
|
162
|
+
`RepresentativeControllerImpl#exportLeadReportersFile` (hidden),
|
|
163
|
+
`RepresentativeServiceImpl#exportFile`. Returns the file's bytes
|
|
164
|
+
(`Dataflow-<id>-Lead-Reporters.csv`, no BOM): a header line
|
|
165
|
+
`Representing,Email` (not quoted), then one line per lead reporter
|
|
166
|
+
with every value quoted (`"AT","someone@example.org"`), and `"AT",""`
|
|
167
|
+
for a representative without lead reporters. The delimiter is the
|
|
168
|
+
platform's `exportDataDelimiter` (`,` in `configuration/consulKV.json`);
|
|
169
|
+
lines end in `\\n`. `Representing` is the data provider's code: the file
|
|
170
|
+
carries no provider group, label or flags. Emails are personal data.
|
|
171
|
+
Requires DATAFLOW_CUSTODIAN, DATAFLOW_STEWARD or DATAFLOW_STEWARD_SUPPORT
|
|
172
|
+
(login token only, see the module docstring).
|
|
173
|
+
"""
|
|
174
|
+
response = self._client.request(
|
|
175
|
+
"GET", _EXPORT_PATH.format(dataflow_id), headers=dict(ACCEPT_ANY)
|
|
176
|
+
)
|
|
177
|
+
return response.content
|
|
178
|
+
|
|
179
|
+
def import_lead_reporters_file(
|
|
180
|
+
self, dataflow_id: int, group_id: int, file: str | os.PathLike[str]
|
|
181
|
+
) -> bytes:
|
|
182
|
+
"""POST /representative/import/{dataflowId}/group/{groupId}: add lead reporters from a CSV.
|
|
183
|
+
|
|
184
|
+
`RepresentativeControllerImpl#importFileCountryTemplate` (hidden),
|
|
185
|
+
`RepresentativeServiceImpl#importLeadReportersFile`; multipart `file`
|
|
186
|
+
in `export_lead_reporters_file()`'s format (`Representing,Email`).
|
|
187
|
+
`group_id` is the provider group whose codes `Representing` holds.
|
|
188
|
+
Only adds: nothing is removed or changed.
|
|
189
|
+
|
|
190
|
+
The backend discards the first line (so a BOM there is harmless),
|
|
191
|
+
splits the rest on `\\n` and each line on the delimiter without reading
|
|
192
|
+
quotes, and strips `\\r` only from the email. This method therefore
|
|
193
|
+
checks and cleans the file before sending it (see `_upload`): `.csv`
|
|
194
|
+
name, header first, line ends turned into `\\n`, blank lines dropped,
|
|
195
|
+
exactly two comma-separated values per data line; otherwise it raises
|
|
196
|
+
`ValueError` and sends nothing.
|
|
197
|
+
|
|
198
|
+
Returns the backend's result CSV (`Representing,Email,Imported`), one
|
|
199
|
+
line per data line, `Imported` being:
|
|
200
|
+
- "OK - imported": a lead reporter was added, or a new representative
|
|
201
|
+
was created (also for a line with a blank email);
|
|
202
|
+
- "KO - invalid email": the email is malformed. **For a provider the
|
|
203
|
+
dataflow doesn't have yet, the representative is still created**, so
|
|
204
|
+
a KO line does not mean nothing changed;
|
|
205
|
+
- empty: that email is already a lead reporter of that representative
|
|
206
|
+
in Reportnet (nothing done);
|
|
207
|
+
- "KO - imported user already exists ... for the same Representative.":
|
|
208
|
+
the same email appears twice for one provider within this file;
|
|
209
|
+
- "KO - imported Representative already exists": a blank email for a
|
|
210
|
+
provider the dataflow already has;
|
|
211
|
+
- "Error during import, country (company) code doesn't correspond to
|
|
212
|
+
a country (company) in ReportNet": unknown code for `group_id`.
|
|
213
|
+
|
|
214
|
+
Errors: an empty file or an unknown dataflow answers 400, but an I/O
|
|
215
|
+
error while reading is only logged and answers 200 with a partial
|
|
216
|
+
result, and a line with more than two values has its email ignored
|
|
217
|
+
(refused here before sending), so read the result lines. Not retried.
|
|
218
|
+
Requires DATAFLOW_CUSTODIAN, DATAFLOW_STEWARD or DATAFLOW_STEWARD_SUPPORT.
|
|
219
|
+
"""
|
|
220
|
+
response = self._client.request(
|
|
221
|
+
"POST",
|
|
222
|
+
_IMPORT_PATH.format(dataflow_id, group_id),
|
|
223
|
+
files=_upload(file),
|
|
224
|
+
headers=dict(ACCEPT_ANY),
|
|
225
|
+
)
|
|
226
|
+
return response.content
|
|
227
|
+
|
|
228
|
+
def create_representative(
|
|
229
|
+
self, dataflow_id: int, *, data_provider_id: int, data_provider_group_id: int | None = None
|
|
230
|
+
) -> int:
|
|
231
|
+
"""POST /representative/{dataflowId}: add a representative (data provider) to a dataflow.
|
|
232
|
+
|
|
233
|
+
`RepresentativeControllerImpl#createRepresentative` (hidden), body a
|
|
234
|
+
`RepresentativeVO` with `dataProviderId` (a `data_providers()` item's
|
|
235
|
+
`id`). Reportnet never reads `dataProviderGroupId`: it is sent only
|
|
236
|
+
when given, for the record. Returns the new representative's id, for
|
|
237
|
+
`create_lead_reporter()`. It starts without lead reporters, datasets
|
|
238
|
+
or receipt.
|
|
239
|
+
|
|
240
|
+
Errors: a provider the dataflow already has answers 400
|
|
241
|
+
(REPRESENTATIVE_DUPLICATED behind a generic message); an unknown
|
|
242
|
+
`data_provider_id` answers 500 (a foreign-key violation passed on by a
|
|
243
|
+
catch-all). `@LockMethod` without lock criteria: the lock is global,
|
|
244
|
+
across all dataflows, so concurrent calls (from anyone) can get 423.
|
|
245
|
+
Requires DATAFLOW_CUSTODIAN, DATAFLOW_STEWARD or DATAFLOW_STEWARD_SUPPORT.
|
|
246
|
+
"""
|
|
247
|
+
response = self._client.request(
|
|
248
|
+
"POST",
|
|
249
|
+
f"/representative/{dataflow_id}",
|
|
250
|
+
json=_representative_body(data_provider_id, data_provider_group_id),
|
|
251
|
+
)
|
|
252
|
+
return _created_id(response, "representative")
|
|
253
|
+
|
|
254
|
+
def create_lead_reporter(
|
|
255
|
+
self, *, representative_id: int, dataflow_id: int, email: str, check: bool = True
|
|
256
|
+
) -> int:
|
|
257
|
+
"""POST /representative/{representativeId}/leadReporter/dataflow/{dataflowId}: add a lead reporter.
|
|
258
|
+
|
|
259
|
+
`RepresentativeControllerImpl#createLeadReporter` (hidden), body
|
|
260
|
+
`{"email": ...}`. Returns the new lead reporter's id. Keyword-only:
|
|
261
|
+
Reportnet authorizes on `dataflow_id` but loads the representative by
|
|
262
|
+
`representative_id` alone, so swapped or mismatched ids would silently
|
|
263
|
+
add the lead reporter to another dataflow's representative. With
|
|
264
|
+
`check` (default) this first reads `representatives(dataflow_id)` and
|
|
265
|
+
raises `ValueError` if `representative_id` isn't among them.
|
|
266
|
+
|
|
267
|
+
The backend lower-cases the email and checks its form (400 otherwise);
|
|
268
|
+
the same email twice on one representative answers 400. An email
|
|
269
|
+
without a Reportnet account is still added, marked `invalid`, and gets
|
|
270
|
+
its permissions later through `validate_lead_reporters()`.
|
|
271
|
+
`@LockMethod` per representative. The email is personal data.
|
|
272
|
+
Requires DATAFLOW_CUSTODIAN, DATAFLOW_STEWARD or DATAFLOW_STEWARD_SUPPORT.
|
|
273
|
+
"""
|
|
274
|
+
if check:
|
|
275
|
+
_check_representative(representative_id, dataflow_id, self.representatives(dataflow_id))
|
|
276
|
+
response = self._client.request(
|
|
277
|
+
"POST", _CREATE_LEAD_PATH.format(representative_id, dataflow_id), json={"email": email}
|
|
278
|
+
)
|
|
279
|
+
return _created_id(response, "lead reporter")
|
|
280
|
+
|
|
281
|
+
def validate_lead_reporters(self, dataflow_id: int) -> None:
|
|
282
|
+
"""PUT /representative/validateLeadReporters/dataflow/{dataflowId}: recheck lead reporters.
|
|
283
|
+
|
|
284
|
+
`RepresentativeControllerImpl#validateLeadReporters` (hidden; the
|
|
285
|
+
mapping is declared on the `RepresentativeController` interface).
|
|
286
|
+
Rechecks the dataflow's `invalid` lead reporters against the registered
|
|
287
|
+
users and grants the ones now found their permissions. Asynchronous
|
|
288
|
+
(`@Async`): answers 200 at once, and the outcome only reaches the web
|
|
289
|
+
app as a notification (VALIDATE_LEAD_REPORTERS_COMPLETED/FAILED_EVENT),
|
|
290
|
+
so reread `representatives()` to see it.
|
|
291
|
+
Requires DATAFLOW_CUSTODIAN, DATAFLOW_STEWARD or DATAFLOW_STEWARD_SUPPORT.
|
|
292
|
+
"""
|
|
293
|
+
self._client.request("PUT", _VALIDATE_PATH.format(dataflow_id))
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
class AsyncRepresentativeResource:
|
|
297
|
+
def __init__(self, client: AsyncReportnetClient) -> None:
|
|
298
|
+
self._client = client
|
|
299
|
+
|
|
300
|
+
async def representatives(self, dataflow_id: int) -> list[dict[str, Any]]:
|
|
301
|
+
"""Async counterpart of RepresentativeResource.representatives."""
|
|
302
|
+
response = await self._client.request("GET", f"/representative/v1/dataflow/{dataflow_id}")
|
|
303
|
+
return cast(list[dict[str, Any]], response.json())
|
|
304
|
+
|
|
305
|
+
async def data_provider_groups(self) -> list[dict[str, Any]]:
|
|
306
|
+
"""Async counterpart of RepresentativeResource.data_provider_groups."""
|
|
307
|
+
response = await self._client.request("GET", "/representative/dataProvider/countryGroups")
|
|
308
|
+
return cast(list[dict[str, Any]], response.json())
|
|
309
|
+
|
|
310
|
+
async def data_providers(self, group_id: int) -> list[dict[str, Any]]:
|
|
311
|
+
"""Async counterpart of RepresentativeResource.data_providers."""
|
|
312
|
+
response = await self._client.request("GET", f"/representative/dataProvider/{group_id}")
|
|
313
|
+
return cast(list[dict[str, Any]], response.json())
|
|
314
|
+
|
|
315
|
+
async def data_provider(self, data_provider_id: int) -> dict[str, Any]:
|
|
316
|
+
"""Async counterpart of RepresentativeResource.data_provider."""
|
|
317
|
+
response = await self._client.request(
|
|
318
|
+
"GET", f"/representative/dataProvider/id/{data_provider_id}"
|
|
319
|
+
)
|
|
320
|
+
return cast(dict[str, Any], response.json())
|
|
321
|
+
|
|
322
|
+
async def export_lead_reporters_file(self, dataflow_id: int) -> bytes:
|
|
323
|
+
"""Async counterpart of RepresentativeResource.export_lead_reporters_file."""
|
|
324
|
+
response = await self._client.request(
|
|
325
|
+
"GET", _EXPORT_PATH.format(dataflow_id), headers=dict(ACCEPT_ANY)
|
|
326
|
+
)
|
|
327
|
+
return response.content
|
|
328
|
+
|
|
329
|
+
async def import_lead_reporters_file(
|
|
330
|
+
self, dataflow_id: int, group_id: int, file: str | os.PathLike[str]
|
|
331
|
+
) -> bytes:
|
|
332
|
+
"""Async counterpart of RepresentativeResource.import_lead_reporters_file."""
|
|
333
|
+
response = await self._client.request(
|
|
334
|
+
"POST",
|
|
335
|
+
_IMPORT_PATH.format(dataflow_id, group_id),
|
|
336
|
+
files=_upload(file),
|
|
337
|
+
headers=dict(ACCEPT_ANY),
|
|
338
|
+
)
|
|
339
|
+
return response.content
|
|
340
|
+
|
|
341
|
+
async def create_representative(
|
|
342
|
+
self, dataflow_id: int, *, data_provider_id: int, data_provider_group_id: int | None = None
|
|
343
|
+
) -> int:
|
|
344
|
+
"""Async counterpart of RepresentativeResource.create_representative."""
|
|
345
|
+
response = await self._client.request(
|
|
346
|
+
"POST",
|
|
347
|
+
f"/representative/{dataflow_id}",
|
|
348
|
+
json=_representative_body(data_provider_id, data_provider_group_id),
|
|
349
|
+
)
|
|
350
|
+
return _created_id(response, "representative")
|
|
351
|
+
|
|
352
|
+
async def create_lead_reporter(
|
|
353
|
+
self, *, representative_id: int, dataflow_id: int, email: str, check: bool = True
|
|
354
|
+
) -> int:
|
|
355
|
+
"""Async counterpart of RepresentativeResource.create_lead_reporter."""
|
|
356
|
+
if check:
|
|
357
|
+
reps = await self.representatives(dataflow_id)
|
|
358
|
+
_check_representative(representative_id, dataflow_id, reps)
|
|
359
|
+
response = await self._client.request(
|
|
360
|
+
"POST", _CREATE_LEAD_PATH.format(representative_id, dataflow_id), json={"email": email}
|
|
361
|
+
)
|
|
362
|
+
return _created_id(response, "lead reporter")
|
|
363
|
+
|
|
364
|
+
async def validate_lead_reporters(self, dataflow_id: int) -> None:
|
|
365
|
+
"""Async counterpart of RepresentativeResource.validate_lead_reporters."""
|
|
366
|
+
await self._client.request("PUT", _VALIDATE_PATH.format(dataflow_id))
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Hand-written User resource (login-token refresh).
|
|
2
|
+
|
|
3
|
+
The UserManagement tag is absent from spec/api-docs.json ("Known
|
|
4
|
+
spec-snapshot gap"), and `/user/refreshToken` is `@ApiOperation(hidden =
|
|
5
|
+
true)` in `UserManagementControllerImpl` (`user-management-service`). This
|
|
6
|
+
file is never touched by the generators.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import TYPE_CHECKING, Any
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from ..async_client import AsyncReportnetClient
|
|
15
|
+
from ..client import ReportnetClient
|
|
16
|
+
|
|
17
|
+
_REFRESH_PATH = "/user/refreshToken"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class UserResource:
|
|
21
|
+
def __init__(self, client: ReportnetClient) -> None:
|
|
22
|
+
self._client = client
|
|
23
|
+
|
|
24
|
+
def refresh_token(self, refresh_token: str) -> dict[str, Any]:
|
|
25
|
+
"""POST /user/refreshToken: renew a web-app login.
|
|
26
|
+
|
|
27
|
+
`refresh_token` is the refresh token (a JWT) issued together with the
|
|
28
|
+
login, not the UUID `accessToken`. Returns the `TokenVO` dict: a new UUID
|
|
29
|
+
`accessToken` (Reportnet's cache key for the fresh JWT), a new
|
|
30
|
+
`refreshToken` (use it for the next refresh), `accessTokenExpiration`,
|
|
31
|
+
`preferredUsername`, `userId`, `roles`, `groups`.
|
|
32
|
+
|
|
33
|
+
The route has no auth rule (the web app calls it after a 401, with
|
|
34
|
+
its expired token), so the client's own credential doesn't matter.
|
|
35
|
+
"""
|
|
36
|
+
response = self._client.request(
|
|
37
|
+
"POST", _REFRESH_PATH, params={"refreshToken": refresh_token}
|
|
38
|
+
)
|
|
39
|
+
result: dict[str, Any] = response.json()
|
|
40
|
+
return result
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class AsyncUserResource:
|
|
44
|
+
def __init__(self, client: AsyncReportnetClient) -> None:
|
|
45
|
+
self._client = client
|
|
46
|
+
|
|
47
|
+
async def refresh_token(self, refresh_token: str) -> dict[str, Any]:
|
|
48
|
+
"""Async counterpart of UserResource.refresh_token."""
|
|
49
|
+
response = await self._client.request(
|
|
50
|
+
"POST", _REFRESH_PATH, params={"refreshToken": refresh_token}
|
|
51
|
+
)
|
|
52
|
+
result: dict[str, Any] = response.json()
|
|
53
|
+
return result
|