forked from consometers/data-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataconnect.py
223 lines (178 loc) · 7.65 KB
/
dataconnect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#################################################################################
# Author : G. HUSSON - Liberasys - contact_dataconnect _@_ liberasys.com - 2019 #
# License : EUPL v1.2 - see : https://eupl.eu/1.2/en/ #
#################################################################################
# This is a simple Python code in order to tests requests to the
# Enedis data connect test API
import requests
import json
import re
import sys
import os
from urllib import parse as urlparse
# Before launch this script you must export your credentials
print(f"{os.environ['client_id']}\n{os.environ['redirect_uri']}")
client_id = os.environ['client_id']
client_secret = os.environ['client_secret']
redirect_uri = os.environ['redirect_uri']
authorize_url = "https://gw.hml.api.enedis.fr/group/espace-particuliers/consentement-linky/oauth2/authorize"
endpoint_token_url = "https://gw.hml.api.enedis.fr/v1/oauth2/token"
metering_data_base_url = "https://gw.hml.api.enedis.fr"
def pretty_print_request(req):
print('{}\n{}\n{}\n{}\n{}\n'.format(
'-----------REQ START-----------',
req.method + ' ' + req.url,
'\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
req.body,
'-----------REQ STOP-----------',
))
########### Authorize (green button, call back by the customer browser)
print("################################################################################")
print("# Authorize ((green button, call back by the customer browser) #")
print("################################################################################")
authorize_duration = "P1Y4M"
state = 'abcd12e'
req_params = {
'client_id': client_id,
'response_type': 'code',
'redirect_uri':redirect_uri,
'state': state,
'duration': authorize_duration
}
# Forge and print request
http_request = requests.Request("GET", authorize_url, params=req_params)
http_request_prepared = http_request.prepare()
pretty_print_request(http_request_prepared)
# Send request
http_session = requests.Session()
http_return = http_session.send(http_request_prepared)
# Print request result
print(http_return.status_code)
print(http_return.text)
#print(http_return.json)
# Parse request result
callback_found = re.findall(r'var url = \".*\"', http_return.text)
callback_url = callback_found[0].split('\"')[1]
callback_url_parsed = urlparse.urlparse(callback_url)
callback_authorize_code = urlparse.parse_qs(callback_url_parsed.query, True)['code'][0]
callback_state = urlparse.parse_qs(callback_url_parsed.query, True)['state'][0]
callback_usage_point_id = urlparse.parse_qs(callback_url_parsed.query, True)['usage_point_id'][0]
print ("callback auth_code = ", callback_authorize_code)
print ("callback state = ", callback_state)
print ("callback usage_point_id = ", callback_usage_point_id)
print("################################################################################")
print("# Request new Endpoint Token ###################################################")
print("################################################################################")
req_head = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/html'
}
req_params = {
'redirect_uri': redirect_uri,
}
req_data = {
'grant_type': 'authorization_code',
'client_id': client_id,
'client_secret': client_secret,
'code': callback_authorize_code
}
# Forge and print request
http_request = requests.Request("POST", endpoint_token_url, headers=req_head, params=req_params, data=req_data)
http_request_prepared = http_request.prepare()
pretty_print_request(http_request_prepared)
# Send request
http_session = requests.Session()
http_return = http_session.send(http_request_prepared)
# Print request result
print(http_return.status_code)
print(http_return.text)
#print(http_return.json)
# Parse request result
endpoint_token_returned_json = json.loads(http_return.text)
print("################################################################################")
print("# Request data : consumption max power #########################################")
print("################################################################################")
start_date = '2021-09-14'
end_date = '2021-09-15'
metering_data_api_path = f'/v4/metering_data/daily_consumption_max_power'
request_url = metering_data_base_url + metering_data_api_path
token_type = endpoint_token_returned_json['token_type']
token = endpoint_token_returned_json['access_token']
req_head = {
'Accept': 'application/json',
'Authorization': token_type + ' ' + token
}
req_params = {
'start': start_date,
'end': end_date,
'usage_point_id':callback_usage_point_id
}
# Forge and print request
http_request = requests.Request("GET", request_url, headers=req_head, params=req_params)
http_request_prepared = http_request.prepare()
pretty_print_request(http_request_prepared)
# Send request
http_session = requests.Session()
http_return = http_session.send(http_request_prepared)
# Print request result
print(http_return.status_code)
print(http_return.text)
#print(http_return.json)
print("################################################################################")
print("# Request data : consumption load curve ########################################")
print("################################################################################")
start_date = '2021-09-14'
end_date = '2021-09-15'
metering_data_api_path = '/v4/metering_data/consumption_load_curve'
request_url = metering_data_base_url + metering_data_api_path
token_type = endpoint_token_returned_json['token_type']
token = endpoint_token_returned_json['access_token']
req_head = {
'Accept': 'application/json',
'Authorization': token_type + ' ' + token
}
req_params = {
'start': start_date,
'end': end_date,
'usage_point_id':callback_usage_point_id
}
# Forge and print request
http_request = requests.Request("GET", request_url, headers=req_head, params=req_params)
http_request_prepared = http_request.prepare()
pretty_print_request(http_request_prepared)
# Send request
http_session = requests.Session()
http_return = http_session.send(http_request_prepared)
# Print request result
print(http_return.status_code)
print(http_return.text)
#print(http_return.json)
print("################################################################################")
print("# Request data : daily consumption ########################################")
print("################################################################################")
start_date = '2021-09-14'
end_date = '2021-09-15'
metering_data_api_path = f'/v4/metering_data/daily_consumption'
request_url = metering_data_base_url + metering_data_api_path
token_type = endpoint_token_returned_json['token_type']
token = endpoint_token_returned_json['access_token']
req_head = {
'Accept': 'application/json',
'Authorization': token_type + ' ' + token
}
req_params = {
'start': start_date,
'end': end_date,
'usage_point_id':callback_usage_point_id
}
# Forge and print request
http_request = requests.Request("GET", request_url, headers=req_head, params=req_params)
http_request_prepared = http_request.prepare()
pretty_print_request(http_request_prepared)
# Send request
http_session = requests.Session()
http_return = http_session.send(http_request_prepared)
# Print request result
print(http_return.status_code)
print(http_return.text)
#print(http_return.json)