-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api_edr_perso.py
398 lines (350 loc) · 19.1 KB
/
test_api_edr_perso.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 3 15:27:40 2024
@author: tloree
"""
import requests
import json
from dateutil.parser import parse
from datetime import timedelta
import pyproj
class TestApiEDR():
def __init__(self,url_service, config_service,point_test=False):
self.url_service = url_service
#with open(config_service) as cf:
# self.config_service =json.load(cf)
self.retour={}
self.config_service={}
self.point_test=point_test
def accueil(self):
self.retour['accueil']={}
for i in ['','/']:
r = requests.get(self.url_service+i)
try:
self.retour['accueil']['url '+i] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code,'rqt':self.url_service+i }
if r.status_code in [200,201]:
print(self.url_service+i,"ok")
else:
print(self.url_service+i,"error")
except:
self.retour['accueil']['url '+i] = {"format json": "error"}
print(self.url_service+i,"erreur format json")
def collection(self):
self.retour['collection']={}
for i in ['/collections','/collections/']:
r = requests.get(self.url_service+i)
try:
self.retour['collection']['url '+i] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':self.url_service+i }
if r.status_code in [200,201]:
print(self.url_service+i,"ok")
self.config_service=r.json()['collections']
else:
print(self.url_service+i,"error")
except:
self.retour['collection']['url '+i] = {"format json": "error"}
print(self.url_service+i,"erreur format json")
def collection_id(self):
for i in self.config_service:
instance=i['id']
print(instance)
for j in ['/collections/'+instance,'/collections/'+instance+'/']:
r = requests.get(self.url_service+j)
try:
self.retour['collection']['url '+j] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':self.url_service+j }
if r.status_code in [200,201]:
print(self.url_service+j,"ok")
else:
print(self.url_service+j,"error")
except:
self.retour['collection']['url '+j] = {"format json": "error"}
print(self.url_service+j,"erreur")
def position(self):
print("\ntest POSITION\n")
for i in self.config_service:
instance=i['id']
print(instance)
self.retour['position csv date unitaire']={}
if self.point_test==False:
pts='POINT(%s %s)' % (i['extent']['spatial']['bbox'][0][0],i['extent']['spatial']['bbox'][0][1])
else:
pts=f"POINT({self.point_test[0]} {self.point_test[1]})"
instance=i['id']
parameter=list(i['parameter_names'][0].keys())[0]
formatage='CSV'
dateF=i['extent']['temporal']['interval'][0][0]
rqt=f"{self.url_service}/collections/{instance}/position?coords={pts}¶meter-name={parameter}&f={formatage}&datetime={dateF}"
r = requests.get(rqt)
print('requete CSV date unitaire')
try:
self.retour['position csv date unitaire'] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':rqt }
if r.status_code in [200,201]:
print("requete position csv ok")
if 'text/csv' in r.headers['Content-Type']:
print("hearder csv ok")
if parameter in r.text:
print("csv conforme")
if len(r.text.split('\n'))==2:
print('bug date unitaire ou pas de données')
self.retour['position csv date unitaire']['resultat rqt']='bug date unitaire ou pas de données'
elif len(r.text.split('\n'))>2:
print('date unitaire OK')
self.retour['position csv date unitaire']['resultat rqt']={
"value":{
"date":r.text.split('\n')[1].split(',')[0],
"result":r.text.split('\n')[1].split(',')[-1]
}
}
else:
print("requete position csv date unitaire error")
except:
self.retour['position csv date unitaire'] = {"format json": "error"}
print(rqt,"erreur")
self.retour['position csv date multiple']={}
date1=i['extent']['temporal']['interval'][0][0]
date2 =parse(date1)+timedelta(days=10)
date2=date2.strftime(format='%Y-%m-%dT%H:%M:%S')
dateF=date1+"/"+date2
rqt=f"{self.url_service}/collections/{instance}/position?coords={pts}¶meter-name={parameter}&f={formatage}&datetime={dateF}"
r = requests.get(rqt)
print('requete CSV date multiple')
try:
self.retour['position csv date multiple'] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':rqt }
if r.status_code in [200,201]:
print("requete position csv ok")
if 'text/csv' in r.headers['Content-Type']:
print("hearder csv ok")
if parameter in r.text:
print("csv conforme")
if len(r.text.split('\n'))==2:
print('bug date multiple ou pas de données')
self.retour['position csv date multiple']['resultat rqt']='bug date multiple ou pas de données'
elif len(r.text.split('\n'))>2:
print('date multiple OK')
self.retour['position csv date multiple']['resultat rqt']={
"value":{
"date":r.text.split('\n')[1].split(',')[0],
"result":r.text.split('\n')[1].split(',')[-1]
}
}
else:
print("requete position csv date multiple error")
except:
self.retour['position csv date multiple'] = {"format json": "error"}
print(rqt,"erreur")
self.retour['position csv sans date']={}
rqt=f"{self.url_service}/collections/{instance}/position?coords={pts}¶meter-name={parameter}&f={formatage}"
r = requests.get(rqt)
print('requete CSV sans date')
try:
self.retour['position csv sans date'] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':rqt }
if r.status_code in [200,201]:
print("requete position csv sans date ok")
if 'text/csv' in r.headers['Content-Type']:
print("hearder csv ok")
if parameter in r.text:
print("csv conforme")
if len(r.text.split('\n'))==2:
print('bug sans date ou pas de données')
self.retour['position csv sans date']['resultat rqt']='bug sans date ou pas de données'
elif len(r.text.split('\n'))>2:
print('sans date OK')
self.retour['position csv sans date']['resultat rqt']={
"value":{
"date":r.text.split('\n')[1].split(',')[0],
"result":r.text.split('\n')[1].split(',')[-1]
}
}
else:
print("requete position csv sans date error")
except:
self.retour['position csv sans date'] = {"format json": "error"}
print(rqt,"erreur")
self.retour['position csv liste parametre']={}
parameter=list(i['parameter_names'][0].keys())[0]+","+list(i['parameter_names'][1].keys())[0]
rqt=f"{self.url_service}/collections/{instance}/position?coords={pts}¶meter-name={parameter}&f={formatage}&datetime={dateF}"
r = requests.get(rqt)
print('requete CSV liste parametre')
try:
self.retour['position csv liste parametre'] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':rqt }
if r.status_code in [200,201]:
print("requete position csv liste parametre ok")
if 'text/csv' in r.headers['Content-Type']:
print("hearder csv ok")
if parameter in r.text:
print("csv conforme")
if len(r.text.split('\n'))==2:
print('bug liste parametre ou pas de données')
self.retour['position csv liste parametre']['resultat rqt']='bug liste parametre ou pas de données'
elif len(r.text.split('\n'))>2:
print('liste parametre OK')
self.retour['position csv liste parametre']['resultat rqt']={
"value":{
"date":r.text.split('\n')[1].split(',')[0],
"result":r.text.split('\n')[1].split(',')[-1]
}
}
else:
print("requete position csv liste parametre error")
except:
self.retour['position csv liste parametre'] = {"format json": "error"}
print(rqt,"erreur")
self.retour['position csv sans parametre']={}
rqt=f"{self.url_service}/collections/{instance}/position?coords={pts}&f={formatage}&datetime={dateF}"
r = requests.get(rqt)
print('requete CSV sans parametre')
try:
self.retour['position csv sans parametre'] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':rqt }
if r.status_code in [200,201]:
print("requete position csv sans parametre ok")
if 'text/csv' in r.headers['Content-Type']:
print("hearder csv ok")
if parameter in r.text:
print("csv conforme")
if len(r.text.split('\n'))==2:
print('bug sans parametre ou pas de données')
self.retour['position csv sans parametre']['resultat rqt']='bug sans parametre ou pas de données'
elif len(r.text.split('\n'))>2:
print('sans parametre OK')
self.retour['position csv sans parametre']['resultat rqt']={
"value":{
"date":r.text.split('\n')[1].split(',')[0],
"result":r.text.split('\n')[1].split(',')[-1]
}
}
else:
print("requete position csv sans parametre error")
except:
self.retour['position csv sans parametre'] = {"format json": "error"}
print(rqt,"erreur")
self.retour['position csv sans parametre et sans date']={}
rqt=f"{self.url_service}/collections/{instance}/position?coords={pts}&f={formatage}"
r = requests.get(rqt)
print('requete CSV sans parametre et sans date')
try:
self.retour['position csv sans parametre et sans date'] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':rqt }
if r.status_code in [200,201]:
print("requete position csv sans parametre et sans date ok")
if 'text/csv' in r.headers['Content-Type']:
print("hearder csv ok")
if parameter in r.text:
print("csv conforme")
if len(r.text.split('\n'))==2:
print('bug sans parametre et sans date ou pas de données')
self.retour['position csv sans parametre et sans date']['resultat rqt']='bug sans parametre et sans date ou pas de données'
elif len(r.text.split('\n'))>2:
print('sans parametre et sans date OK')
self.retour['position csv sans parametre et sans date']['resultat rqt']={
"value":{
"date":r.text.split('\n')[1].split(',')[0],
"result":r.text.split('\n')[1].split(',')[-1]
}
}
else:
print("requete position csv sans parametre et sans date error")
except:
self.retour['position csv sans parametre et sans date'] = {"format json": "error"}
print(rqt,"erreur")
print('requete csv CRS natif')
crs=i['extent']['spatial']['crs']
dateF=date1+"/"+date2
parameter=list(i['parameter_names'][0].keys())[0]
rqt=f"{self.url_service}/collections/{instance}/position?coords={pts}&crs={crs}¶meter-name={parameter}&f={formatage}&datetime={dateF}"
r = requests.get(rqt)
try:
self.retour['position csv CRS natif'] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':rqt }
if r.status_code in [200,201]:
print("requete position csv ok")
if 'text/csv' in r.headers['Content-Type']:
print("hearder csv ok")
if parameter in r.text:
print("csv conforme")
if len(r.text.split('\n'))==2:
print('bug CRS natif ou pas de données')
self.retour['position csv CRS natif']['resultat rqt']='bug CRS natif ou pas de données'
elif len(r.text.split('\n'))>2:
print('CRS natif OK')
self.retour['position csv CRS natif']['resultat rqt']={
"value":{
"date":r.text.split('\n')[1].split(',')[0],
"result":r.text.split('\n')[1].split(',')[-1]
}
}
else:
print("requete position csv CRS natif error")
except:
self.retour['position csv CRS natif'] = {"format json": "error"}
print(rqt,"erreur")
print('requete csv CRS convert')
dateF=date1+"/"+date2
parameter=list(i['parameter_names'][0].keys())[0]
crs="EPSG:4326"
lamb2e_to_latlon = pyproj.Transformer.from_crs(i['extent']['spatial']['crs'],crs)
if self.point_test==False:
y,x=lamb2e_to_latlon.transform(i['extent']['spatial']['bbox'][0][0], i['extent']['spatial']['bbox'][0][1])
else:
y,x=lamb2e_to_latlon.transform(self.point_test[0], self.point_test[1])
pts=f"POINT( )"
pts='POINT(%s %s)' % (x,y)
rqt=f"{self.url_service}/collections/{instance}/position?coords={pts}&crs={crs}¶meter-name={parameter}&f={formatage}&datetime={dateF}"
r = requests.get(rqt)
try:
self.retour['position csv CRS convert'] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':rqt }
if r.status_code in [200,201]:
print("requete position csv ok")
if 'text/csv' in r.headers['Content-Type']:
print("hearder csv ok")
if parameter in r.text:
print("csv conforme")
if len(r.text.split('\n'))==2:
print('bug CRS convert ou pas de données')
self.retour['position csv CRS convert']['resultat rqt']='bug CRS convert ou pas de données'
elif len(r.text.split('\n'))>2:
print('CRS convert OK')
self.retour['position csv CRS convert']['resultat rqt']={
"value":{
"date":r.text.split('\n')[1].split(',')[0],
"result":r.text.split('\n')[1].split(',')[-1]
}
}
else:
print("requete position csv CRS convert error")
except:
self.retour['position csv CRS convert'] = {"format json": "error"}
print(rqt,"erreur")
formatage='CoverageJSON'
rqt=f"{self.url_service}/collections/{instance}/position?coords={pts}&crs={crs}¶meter-name={parameter}&f={formatage}&datetime={dateF}"
r = requests.get(rqt)
try:
self.retour['position CoverageJSON CRS convert'] = {"connexion": "ok", "type":r.headers['Content-Type'], "code": r.status_code, 'rqt':rqt }
if r.status_code in [200,201]:
print("requete position CoverageJSON ok")
data=r.json()
if 'application/json' in r.headers['Content-Type']:
print("hearder CoverageJSON ok")
if parameter in data['parameters']:
print("CoverageJSON conforme")
if len(data['ranges'][parameter]['values'])==0:
print('bug CoverageJSON convert ou pas de données')
self.retour['position CoverageJSON CRS convert']['resultat rqt']='bug CRS convert ou pas de données'
elif len(data['ranges'][parameter]['values'])>0:
print('CoverageJSON convert OK')
self.retour['position CoverageJSON CRS convert']['resultat rqt']={
"value":{
"date":data['domain']['axes']['t']['values'][0],
"result":data['ranges'][parameter]['values'][0]
}
}
else:
print("requete position CoverageJSON CRS convert error")
except:
self.retour['position CoverageJSON CRS convert'] = {"format json": "error"}
print(rqt,"erreur")
break
#tester un ou deux paramètre ou aucun
def run_test(self):
self.accueil()
self.collection()
self.collection_id()
self.position()
return self.retour