dlt-source-elhub 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.
- dlt_source_elhub-0.1.0/LICENSE +202 -0
- dlt_source_elhub-0.1.0/PKG-INFO +140 -0
- dlt_source_elhub-0.1.0/README.md +113 -0
- dlt_source_elhub-0.1.0/pyproject.toml +103 -0
- dlt_source_elhub-0.1.0/setup.cfg +4 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub/__init__.py +9 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub/columns.py +22 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub/elhub.py +138 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub/maskinporten.py +87 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub/py.typed +0 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub.egg-info/PKG-INFO +140 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub.egg-info/SOURCES.txt +21 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub.egg-info/dependency_links.txt +1 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub.egg-info/requires.txt +2 -0
- dlt_source_elhub-0.1.0/src/dlt_source_elhub.egg-info/top_level.txt +1 -0
- dlt_source_elhub-0.1.0/tests/test_end_to_end.py +42 -0
- dlt_source_elhub-0.1.0/tests/test_integration.py +70 -0
- dlt_source_elhub-0.1.0/tests/test_maskinporten.py +80 -0
- dlt_source_elhub-0.1.0/tests/test_metering_points.py +73 -0
- dlt_source_elhub-0.1.0/tests/test_metering_values_hour.py +202 -0
- dlt_source_elhub-0.1.0/tests/test_packaged_examples.py +114 -0
- dlt_source_elhub-0.1.0/tests/test_param_surface.py +41 -0
- dlt_source_elhub-0.1.0/tests/test_schema_leniency.py +34 -0
|
@@ -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 Havbruksdataforeningen
|
|
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.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dlt-source-elhub
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: dlt source package for Elhub's Energy Data API: metering points and hourly metering values, via Maskinporten
|
|
5
|
+
Author: Havbruksdataforeningen
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/Havbruksdataforeningen/dlt-sources/tree/main/packages/dlt-source-elhub
|
|
8
|
+
Project-URL: Repository, https://github.com/Havbruksdataforeningen/dlt-sources
|
|
9
|
+
Project-URL: Changelog, https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/CHANGELOG.md
|
|
10
|
+
Project-URL: Issues, https://github.com/Havbruksdataforeningen/dlt-sources/issues
|
|
11
|
+
Keywords: dlt,elhub,maskinporten,electricity,energy,etl,elt,data-ingestion
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Database
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: dlt>=1.2
|
|
25
|
+
Requires-Dist: PyJWT[crypto]>=2.8
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# dlt-source-elhub
|
|
29
|
+
|
|
30
|
+
[](https://pypi.org/project/dlt-source-elhub/)
|
|
31
|
+
|
|
32
|
+
A [dlt](https://dlthub.com/) source for [Elhub's Energy Data API](https://api.elhub.no/energy-data-api/metering-points): your organisation's electricity metering points and their hourly metering values, authenticated through Maskinporten, into any dlt destination.
|
|
33
|
+
|
|
34
|
+
**Records land as the API returns them** — nothing renamed, nothing dropped, no invented child tables. Column names are the API's own, in dlt's usual snake_case. Reshaping belongs in your transform layer, where you can change it without waiting for a release.
|
|
35
|
+
|
|
36
|
+
The package handles the Maskinporten token, the headers Elhub insists on, envelope unwrapping and a cursor per metering point. It depends on dlt and PyJWT.
|
|
37
|
+
|
|
38
|
+
## How to start
|
|
39
|
+
|
|
40
|
+
Before any code: your organisation needs a Maskinporten client with the scope `elhub:enduser`, which Elhub grants to an organisation number on request ([Elhub's guide](https://api.elhub.no/energy-data-api/dataset-guidelines)). The client gives you a client id, a key id and a private key.
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
uv add dlt-source-elhub "dlt[duckdb]" # any dlt destination works; DuckDB is the one below
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Then two files in a `.dlt/` directory beside your script.
|
|
47
|
+
|
|
48
|
+
`.dlt/config.toml`:
|
|
49
|
+
|
|
50
|
+
```toml
|
|
51
|
+
[sources.elhub]
|
|
52
|
+
user_agent = "my-company-elhub-pipeline" # Elhub refuses a request without one
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`.dlt/secrets.toml`:
|
|
56
|
+
|
|
57
|
+
```toml
|
|
58
|
+
[sources.elhub.credentials]
|
|
59
|
+
client_id = "00000000-0000-0000-0000-000000000000"
|
|
60
|
+
kid = "00000000-0000-0000-0000-000000000000"
|
|
61
|
+
private_key = """-----BEGIN PRIVATE KEY-----
|
|
62
|
+
...
|
|
63
|
+
-----END PRIVATE KEY-----"""
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Then one script, [`daily_load.py`](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/examples/daily_load.py), on a timer:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import dlt
|
|
70
|
+
from dlt_source_elhub import elhub_source
|
|
71
|
+
|
|
72
|
+
pipeline = dlt.pipeline(pipeline_name="elhub", destination="duckdb", dataset_name="elhub_data")
|
|
73
|
+
pipeline.run(elhub_source())
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
There is no separate backfill step. The first run loads everything Elhub retains for every metering point — three years and 41 days, one request per point — and every run after loads what was inserted since, per point. Schedule it after 03:00 Europe/Oslo: yesterday's hours are inserted around 02:05. Run it soon: Elhub deletes data older than three years and 41 days, so each day a first load waits, a day of history is gone.
|
|
77
|
+
|
|
78
|
+
Two more examples, a page each: [`backfill.py`](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/examples/backfill.py) re-loads one window without touching the cursors, and [`companies.py`](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/examples/companies.py) reads several companies with one client. From a checkout, run one with `python examples/<name>.py`.
|
|
79
|
+
|
|
80
|
+
## What it loads
|
|
81
|
+
|
|
82
|
+
| Resource | Endpoint | Load strategy | Key |
|
|
83
|
+
|---|---|---|---|
|
|
84
|
+
| `metering_points` | `GET /metering-points` | merge | `id` |
|
|
85
|
+
| `metering_values_hour` | `GET /metering-points/{id}` | merge | `meteringPointId`, `startTime`, `direction` |
|
|
86
|
+
|
|
87
|
+
Both read the dataset `METERING_VALUES_HOUR_RESTRICTED`, the only one the `elhub:enduser` scope answers; the day, month and year datasets are `403` under it, so aggregate downstream.
|
|
88
|
+
|
|
89
|
+
Three things worth knowing before your first query:
|
|
90
|
+
|
|
91
|
+
- **`metering_points` is the list of ids, found the only way Elhub offers.** There is no list endpoint; the dataset call without an id returns every point with values in the last 24 hours. A point lands as the API's `id` and `type`, with the values left to the other table. A point gone from the list keeps its row; its `_dlt_load_id` says when it was last seen.
|
|
92
|
+
- **`metering_values_hour` is one row per value**, with the point's `id` as `meteringPointId`. `resolution` is `PT60M` on most points and `PT15M` on some, so never assume 24 rows a day. `direction` is `Out` for consumption and `In` for production, both loaded. A corrected value overwrites the old one, because the key is the interval, not the insert.
|
|
93
|
+
- **The cursor is per metering point**, stored in dlt's resource state under the point's id. That is what makes a new point load its history on first contact, and several companies safe in one pipeline — [how it works](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/REFERENCE.md#the-cursor-is-per-metering-point).
|
|
94
|
+
|
|
95
|
+
## Configuring a resource
|
|
96
|
+
|
|
97
|
+
Each resource takes its endpoint's params in snake_case. A date is `YYYY-MM-DD`, read as midnight in Europe/Oslo; an ISO 8601 instant with an offset works too.
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
source = elhub_source(consumer_org="999999991")
|
|
101
|
+
source.metering_points.bind(metering_point_ids=["707057500000000001"])
|
|
102
|
+
source.metering_values_hour.bind(start_date="2026-10-01", end_date="2026-10-03")
|
|
103
|
+
pipeline.run(source)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
| Param | What to know |
|
|
107
|
+
|---|---|
|
|
108
|
+
| `consumer_org` | On the source. The organisation number of a company that delegated the scope to your client's organisation in Altinn; the token is then issued on its behalf. Leave it out to read the client's own organisation. One source per company. |
|
|
109
|
+
| `initial_insert_date_time` | On the source. Where a point the pipeline has not seen starts: values inserted at or after this instant. Left out, everything Elhub retains. |
|
|
110
|
+
| `metering_point_ids` | On `metering_points`. Keep only these ids; `metering_values_hour` then reads only them. For one new point's history, or a backfill of a few. |
|
|
111
|
+
| `start_date`, `end_date` | On both resources. On `metering_points` they move the discovery window, at most one day wide. On `metering_values_hour` they make the request a backfill: the window is sent as given, and the stored cursor is neither read nor advanced. The end is exclusive on `startTime` in practice. |
|
|
112
|
+
| `insert_date_time` | On `metering_values_hour`. Sent instead of the cursor, which is then neither read nor advanced: values inserted at or after it, over the API's widest window when no dates are bound. |
|
|
113
|
+
| `params` | On every resource, merged into the query string last, so it wins over every named param. The escape hatch for a param the API grows later. |
|
|
114
|
+
| `user_agent`, `headers` | On the source. Elhub answers `403` to a request without a `User-Agent`; `headers` adds any other, such as `X-Elhub-GLN` under another scope. |
|
|
115
|
+
|
|
116
|
+
Params can also be set in config, per resource:
|
|
117
|
+
|
|
118
|
+
```toml
|
|
119
|
+
[sources.elhub.metering_values_hour]
|
|
120
|
+
start_date = "2026-10-01"
|
|
121
|
+
end_date = "2026-10-03"
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Compatibility
|
|
125
|
+
|
|
126
|
+
| `dlt-source-elhub` | Energy Data API |
|
|
127
|
+
|---|---|
|
|
128
|
+
| 0.1.x | v0 |
|
|
129
|
+
|
|
130
|
+
The API is v0, and Elhub says breaking changes may come. Built against the dated copy of its documentation in [`specs/`](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/specs/README.md) and run live against it, last on 2026-10-13, with the scope `elhub:enduser`. The `elhub:thirdparty` scope is not verified.
|
|
131
|
+
|
|
132
|
+
## Read next
|
|
133
|
+
|
|
134
|
+
- [**API quirks worth knowing**](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/specs/README.md#api-quirks-worth-knowing) — what was measured against the live API: windows, the inclusive `insertDateTime` filter, `204` for empty, when yesterday arrives.
|
|
135
|
+
- [**Reference**](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/REFERENCE.md) — the cursor, backfilling, several companies, nesting, what the source does not expose, logging and column types.
|
|
136
|
+
- [**Changelog**](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/CHANGELOG.md) and [**contributing**](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/CONTRIBUTING.md).
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
[Apache-2.0](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/LICENSE). The files in `specs/` are Elhub's own API documentation, included as the spec this package is built against. They are Elhub's material, and the licence does not extend to them.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# dlt-source-elhub
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/dlt-source-elhub/)
|
|
4
|
+
|
|
5
|
+
A [dlt](https://dlthub.com/) source for [Elhub's Energy Data API](https://api.elhub.no/energy-data-api/metering-points): your organisation's electricity metering points and their hourly metering values, authenticated through Maskinporten, into any dlt destination.
|
|
6
|
+
|
|
7
|
+
**Records land as the API returns them** — nothing renamed, nothing dropped, no invented child tables. Column names are the API's own, in dlt's usual snake_case. Reshaping belongs in your transform layer, where you can change it without waiting for a release.
|
|
8
|
+
|
|
9
|
+
The package handles the Maskinporten token, the headers Elhub insists on, envelope unwrapping and a cursor per metering point. It depends on dlt and PyJWT.
|
|
10
|
+
|
|
11
|
+
## How to start
|
|
12
|
+
|
|
13
|
+
Before any code: your organisation needs a Maskinporten client with the scope `elhub:enduser`, which Elhub grants to an organisation number on request ([Elhub's guide](https://api.elhub.no/energy-data-api/dataset-guidelines)). The client gives you a client id, a key id and a private key.
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
uv add dlt-source-elhub "dlt[duckdb]" # any dlt destination works; DuckDB is the one below
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Then two files in a `.dlt/` directory beside your script.
|
|
20
|
+
|
|
21
|
+
`.dlt/config.toml`:
|
|
22
|
+
|
|
23
|
+
```toml
|
|
24
|
+
[sources.elhub]
|
|
25
|
+
user_agent = "my-company-elhub-pipeline" # Elhub refuses a request without one
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`.dlt/secrets.toml`:
|
|
29
|
+
|
|
30
|
+
```toml
|
|
31
|
+
[sources.elhub.credentials]
|
|
32
|
+
client_id = "00000000-0000-0000-0000-000000000000"
|
|
33
|
+
kid = "00000000-0000-0000-0000-000000000000"
|
|
34
|
+
private_key = """-----BEGIN PRIVATE KEY-----
|
|
35
|
+
...
|
|
36
|
+
-----END PRIVATE KEY-----"""
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Then one script, [`daily_load.py`](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/examples/daily_load.py), on a timer:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
import dlt
|
|
43
|
+
from dlt_source_elhub import elhub_source
|
|
44
|
+
|
|
45
|
+
pipeline = dlt.pipeline(pipeline_name="elhub", destination="duckdb", dataset_name="elhub_data")
|
|
46
|
+
pipeline.run(elhub_source())
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
There is no separate backfill step. The first run loads everything Elhub retains for every metering point — three years and 41 days, one request per point — and every run after loads what was inserted since, per point. Schedule it after 03:00 Europe/Oslo: yesterday's hours are inserted around 02:05. Run it soon: Elhub deletes data older than three years and 41 days, so each day a first load waits, a day of history is gone.
|
|
50
|
+
|
|
51
|
+
Two more examples, a page each: [`backfill.py`](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/examples/backfill.py) re-loads one window without touching the cursors, and [`companies.py`](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/examples/companies.py) reads several companies with one client. From a checkout, run one with `python examples/<name>.py`.
|
|
52
|
+
|
|
53
|
+
## What it loads
|
|
54
|
+
|
|
55
|
+
| Resource | Endpoint | Load strategy | Key |
|
|
56
|
+
|---|---|---|---|
|
|
57
|
+
| `metering_points` | `GET /metering-points` | merge | `id` |
|
|
58
|
+
| `metering_values_hour` | `GET /metering-points/{id}` | merge | `meteringPointId`, `startTime`, `direction` |
|
|
59
|
+
|
|
60
|
+
Both read the dataset `METERING_VALUES_HOUR_RESTRICTED`, the only one the `elhub:enduser` scope answers; the day, month and year datasets are `403` under it, so aggregate downstream.
|
|
61
|
+
|
|
62
|
+
Three things worth knowing before your first query:
|
|
63
|
+
|
|
64
|
+
- **`metering_points` is the list of ids, found the only way Elhub offers.** There is no list endpoint; the dataset call without an id returns every point with values in the last 24 hours. A point lands as the API's `id` and `type`, with the values left to the other table. A point gone from the list keeps its row; its `_dlt_load_id` says when it was last seen.
|
|
65
|
+
- **`metering_values_hour` is one row per value**, with the point's `id` as `meteringPointId`. `resolution` is `PT60M` on most points and `PT15M` on some, so never assume 24 rows a day. `direction` is `Out` for consumption and `In` for production, both loaded. A corrected value overwrites the old one, because the key is the interval, not the insert.
|
|
66
|
+
- **The cursor is per metering point**, stored in dlt's resource state under the point's id. That is what makes a new point load its history on first contact, and several companies safe in one pipeline — [how it works](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/REFERENCE.md#the-cursor-is-per-metering-point).
|
|
67
|
+
|
|
68
|
+
## Configuring a resource
|
|
69
|
+
|
|
70
|
+
Each resource takes its endpoint's params in snake_case. A date is `YYYY-MM-DD`, read as midnight in Europe/Oslo; an ISO 8601 instant with an offset works too.
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
source = elhub_source(consumer_org="999999991")
|
|
74
|
+
source.metering_points.bind(metering_point_ids=["707057500000000001"])
|
|
75
|
+
source.metering_values_hour.bind(start_date="2026-10-01", end_date="2026-10-03")
|
|
76
|
+
pipeline.run(source)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
| Param | What to know |
|
|
80
|
+
|---|---|
|
|
81
|
+
| `consumer_org` | On the source. The organisation number of a company that delegated the scope to your client's organisation in Altinn; the token is then issued on its behalf. Leave it out to read the client's own organisation. One source per company. |
|
|
82
|
+
| `initial_insert_date_time` | On the source. Where a point the pipeline has not seen starts: values inserted at or after this instant. Left out, everything Elhub retains. |
|
|
83
|
+
| `metering_point_ids` | On `metering_points`. Keep only these ids; `metering_values_hour` then reads only them. For one new point's history, or a backfill of a few. |
|
|
84
|
+
| `start_date`, `end_date` | On both resources. On `metering_points` they move the discovery window, at most one day wide. On `metering_values_hour` they make the request a backfill: the window is sent as given, and the stored cursor is neither read nor advanced. The end is exclusive on `startTime` in practice. |
|
|
85
|
+
| `insert_date_time` | On `metering_values_hour`. Sent instead of the cursor, which is then neither read nor advanced: values inserted at or after it, over the API's widest window when no dates are bound. |
|
|
86
|
+
| `params` | On every resource, merged into the query string last, so it wins over every named param. The escape hatch for a param the API grows later. |
|
|
87
|
+
| `user_agent`, `headers` | On the source. Elhub answers `403` to a request without a `User-Agent`; `headers` adds any other, such as `X-Elhub-GLN` under another scope. |
|
|
88
|
+
|
|
89
|
+
Params can also be set in config, per resource:
|
|
90
|
+
|
|
91
|
+
```toml
|
|
92
|
+
[sources.elhub.metering_values_hour]
|
|
93
|
+
start_date = "2026-10-01"
|
|
94
|
+
end_date = "2026-10-03"
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Compatibility
|
|
98
|
+
|
|
99
|
+
| `dlt-source-elhub` | Energy Data API |
|
|
100
|
+
|---|---|
|
|
101
|
+
| 0.1.x | v0 |
|
|
102
|
+
|
|
103
|
+
The API is v0, and Elhub says breaking changes may come. Built against the dated copy of its documentation in [`specs/`](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/specs/README.md) and run live against it, last on 2026-10-13, with the scope `elhub:enduser`. The `elhub:thirdparty` scope is not verified.
|
|
104
|
+
|
|
105
|
+
## Read next
|
|
106
|
+
|
|
107
|
+
- [**API quirks worth knowing**](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/specs/README.md#api-quirks-worth-knowing) — what was measured against the live API: windows, the inclusive `insertDateTime` filter, `204` for empty, when yesterday arrives.
|
|
108
|
+
- [**Reference**](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/REFERENCE.md) — the cursor, backfilling, several companies, nesting, what the source does not expose, logging and column types.
|
|
109
|
+
- [**Changelog**](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/CHANGELOG.md) and [**contributing**](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/CONTRIBUTING.md).
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
[Apache-2.0](https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/LICENSE). The files in `specs/` are Elhub's own API documentation, included as the spec this package is built against. They are Elhub's material, and the licence does not extend to them.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "dlt-source-elhub"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "dlt source package for Elhub's Energy Data API: metering points and hourly metering values, via Maskinporten"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
# PEP 639: an SPDX expression plus the file, not a classifier. The two forms
|
|
8
|
+
# cannot be combined, so there is deliberately no "License ::" classifier below.
|
|
9
|
+
license = "Apache-2.0"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
authors = [{ name = "Havbruksdataforeningen" }]
|
|
12
|
+
keywords = ["dlt", "elhub", "maskinporten", "electricity", "energy", "etl", "elt", "data-ingestion"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Programming Language :: Python :: 3.11",
|
|
17
|
+
"Programming Language :: Python :: 3.12",
|
|
18
|
+
"Programming Language :: Python :: 3.13",
|
|
19
|
+
"Programming Language :: Python :: 3.14",
|
|
20
|
+
"Topic :: Database",
|
|
21
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
dependencies = [
|
|
25
|
+
# >=1.2 for the RESTClient and configspec helpers used here.
|
|
26
|
+
"dlt>=1.2",
|
|
27
|
+
# The Maskinporten grant is a JWT signed with the client's RSA key. PyJWT is what dlt's own
|
|
28
|
+
# OAuthJWTAuth signs with; [crypto] brings the cryptography package that does the RS256.
|
|
29
|
+
"PyJWT[crypto]>=2.8",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = "https://github.com/Havbruksdataforeningen/dlt-sources/tree/main/packages/dlt-source-elhub"
|
|
34
|
+
Repository = "https://github.com/Havbruksdataforeningen/dlt-sources"
|
|
35
|
+
Changelog = "https://github.com/Havbruksdataforeningen/dlt-sources/blob/main/packages/dlt-source-elhub/CHANGELOG.md"
|
|
36
|
+
Issues = "https://github.com/Havbruksdataforeningen/dlt-sources/issues"
|
|
37
|
+
|
|
38
|
+
[dependency-groups]
|
|
39
|
+
dev = [
|
|
40
|
+
"dlt[duckdb]>=1.2",
|
|
41
|
+
"pytest>=8.0",
|
|
42
|
+
"pytest-cov>=6.0",
|
|
43
|
+
"pyright>=1.1",
|
|
44
|
+
"ruff>=0.8",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[tool.setuptools.packages.find]
|
|
48
|
+
where = ["src"]
|
|
49
|
+
|
|
50
|
+
# Ships the PEP 561 marker, so a consumer's type checker reads this package's
|
|
51
|
+
# annotations instead of treating it as untyped.
|
|
52
|
+
[tool.setuptools.package-data]
|
|
53
|
+
dlt_source_elhub = ["py.typed"]
|
|
54
|
+
|
|
55
|
+
[tool.ruff]
|
|
56
|
+
line-length = 120
|
|
57
|
+
target-version = "py311"
|
|
58
|
+
src = ["src", "tests"]
|
|
59
|
+
|
|
60
|
+
[tool.ruff.lint]
|
|
61
|
+
# S is ruff's port of bandit: hardcoded credentials, requests without a
|
|
62
|
+
# timeout, disabled TLS verification — the risks of code that talks to a
|
|
63
|
+
# supplier's API with credentials. Don't add bandit itself without a check it
|
|
64
|
+
# catches that S does not.
|
|
65
|
+
select = ["E", "W", "F", "I", "B", "C4", "UP", "SIM", "RUF", "S"]
|
|
66
|
+
ignore = ["E501", "B008"]
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint.per-file-ignores]
|
|
69
|
+
# Tests use assert (S101) — that is what a test is. They also query their own
|
|
70
|
+
# local duckdb with f-string SQL (S608): table names cannot be bound as
|
|
71
|
+
# parameters, and the interpolated values are test constants.
|
|
72
|
+
"tests/**" = ["S101", "S608"]
|
|
73
|
+
|
|
74
|
+
[tool.ruff.lint.isort]
|
|
75
|
+
known-first-party = ["dlt_source_elhub"]
|
|
76
|
+
|
|
77
|
+
[tool.ruff.format]
|
|
78
|
+
quote-style = "double"
|
|
79
|
+
indent-style = "space"
|
|
80
|
+
|
|
81
|
+
[tool.pyright]
|
|
82
|
+
pythonVersion = "3.11"
|
|
83
|
+
typeCheckingMode = "basic"
|
|
84
|
+
include = ["src", "tests", "examples"]
|
|
85
|
+
exclude = [".venv", ".dlt", ".agent"]
|
|
86
|
+
venvPath = "."
|
|
87
|
+
venv = ".venv"
|
|
88
|
+
|
|
89
|
+
[tool.pytest.ini_options]
|
|
90
|
+
testpaths = ["tests"]
|
|
91
|
+
# The default run is offline — it needs no Elhub credentials, so it means
|
|
92
|
+
# the same thing in CI and on a laptop. Run the live tests with -m integration.
|
|
93
|
+
addopts = "-m 'not integration'"
|
|
94
|
+
markers = [
|
|
95
|
+
"integration: marks tests that hit the real Elhub API (run with -m integration)",
|
|
96
|
+
]
|
|
97
|
+
filterwarnings = [
|
|
98
|
+
"ignore::DeprecationWarning:dlt.extract.validation",
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
[build-system]
|
|
102
|
+
requires = ["setuptools>=77.0.3"] # >=77 for PEP 639 license / license-files
|
|
103
|
+
build-backend = "setuptools.build_meta"
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""dlt source package for Elhub's Energy Data API."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
from dlt_source_elhub.elhub import ALL_RETAINED_HISTORY, elhub_source
|
|
6
|
+
from dlt_source_elhub.maskinporten import MaskinportenAuth, MaskinportenCredentials
|
|
7
|
+
|
|
8
|
+
__version__ = version("dlt-source-elhub")
|
|
9
|
+
__all__ = ["ALL_RETAINED_HISTORY", "MaskinportenAuth", "MaskinportenCredentials", "elhub_source"]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Column hints: the API's own field names, typed per `specs/meta-metering-points.json`."""
|
|
2
|
+
|
|
3
|
+
from dlt.common.schema.typing import TTableSchemaColumns
|
|
4
|
+
|
|
5
|
+
METERING_POINT_COLUMNS: TTableSchemaColumns = {
|
|
6
|
+
"id": {"data_type": "text", "nullable": False},
|
|
7
|
+
"type": {"data_type": "text"},
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
# `meteringPointId` is the id of the point the value is nested under, named as Elhub's other datasets name it.
|
|
11
|
+
METERING_VALUES_HOUR_COLUMNS: TTableSchemaColumns = {
|
|
12
|
+
"meteringPointId": {"data_type": "text", "nullable": False},
|
|
13
|
+
"startTime": {"data_type": "text", "nullable": False},
|
|
14
|
+
"endTime": {"data_type": "text", "nullable": False},
|
|
15
|
+
"direction": {"data_type": "text", "nullable": False},
|
|
16
|
+
"quantityKwh": {"data_type": "double"},
|
|
17
|
+
"resolution": {"data_type": "text"},
|
|
18
|
+
"measurementName": {"data_type": "text"},
|
|
19
|
+
"productCode": {"data_type": "bigint"},
|
|
20
|
+
"endUserId": {"data_type": "text"},
|
|
21
|
+
"insertDateTime": {"data_type": "text"},
|
|
22
|
+
}
|