-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
378 lines (335 loc) · 14.1 KB
/
Copy pathutils.py
File metadata and controls
378 lines (335 loc) · 14.1 KB
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
import datetime
import json
import os
import urllib.parse
from time import sleep
from typing import Final
import requests
import urllib3
from retry import retry
from auth import KeycloakToken, generate_headers
from json_getter import (PWD, get_keycloak_catalogue, get_keycloak_ordering,
get_order_body, get_order_details, get_query_details)
KEYCLOAK_ORDERING_JSON: Final = get_keycloak_ordering()
KEYCLOAK_CATALOGUE_JSON: Final = get_keycloak_catalogue()
ORDER_BODY_JSON: Final = get_order_body()
ORDER_DETAILS_JSON: Final = get_order_details()
ordering_keycloak = KeycloakToken()
catalogue_keycloak = KeycloakToken()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def cls() -> None:
"""Clears console."""
os.system("cls" if os.name == "nt" else "clear")
def generate_order_body_from_query(order_body: json, identifiers: list) -> json:
"""Generates order body and prepares IdentifierList parameter."""
order_body["IdentifierList"] = identifiers
print("Order body has been generated")
return order_body
@retry(tries=5, delay=5)
def send_post_request_to_batch_order_and_validate_response(
order_body: dict,
) -> requests.Response:
"""Creates new BatchOrder."""
print("Posting order")
response = requests.post(
f"https://{KEYCLOAK_ORDERING_JSON['host']}/odata/v1/BatchOrder/OData.CSC.Order",
json=order_body,
headers=generate_headers(ordering_keycloak, KEYCLOAK_ORDERING_JSON),
verify=False,
)
assert response.status_code == 201, (
print(
f"Order hasn't been created successfully. Received response: \n{response.json()}\nWrong status code - "
f"{response.status_code}, expected 201. Sending "
f"another request with: {order_body}"
)
if response.status_code < 500
else print(
f"Order hasn't been created successfully. Wrong status code - {response.status_code}, expected 201. Sending "
f"another request with: {order_body}"
)
)
print(
f"Order has been created successfully. Received status code: {response.status_code}. Received "
f"response:\n{response.json()}.\nOrder id: {response.json()['value']['Id']}"
)
return response
def create_batch_order_with_body() -> None:
"""Option 1 - Creates new BatchOrder with a given body."""
identifiers = []
products_ordered = 0
products = ORDER_BODY_JSON["IdentifierList"]
for product in products:
if len(identifiers) < ORDER_DETAILS_JSON["parallel_quota"]:
identifiers.append(product)
products_ordered += 1
print(f"Loaded {identifiers[-1]} to a list of products")
if (
len(identifiers) == ORDER_DETAILS_JSON["parallel_quota"]
or len(products) == products_ordered
):
order_body = generate_order_body_from_query(ORDER_BODY_JSON, identifiers)
response = send_post_request_to_batch_order_and_validate_response(
order_body
)
if "value" in response.json():
wait_for_order_to_be_processed(response.json()["value"]["Id"])
identifiers.clear()
if not ORDER_DETAILS_JSON["new_orders"]:
break
else:
break
@retry(tries=5, delay=5)
def wait_for_order_to_be_processed(identifier: int) -> None:
"""Waits for order to be processed - not in status queued or in_progress."""
print("Waiting for order to be processed...")
while True:
response = requests.get(
f"https://{KEYCLOAK_ORDERING_JSON['host']}/odata/v1/BatchOrder({identifier})",
headers=generate_headers(ordering_keycloak, KEYCLOAK_ORDERING_JSON),
verify=False,
)
assert response.status_code == 200, print(
f"Received status code {response.status_code}. Sending another request"
)
if (
response.json()["value"]["Status"] != "queued"
and response.json()["value"]["Status"] != "in_progress"
):
print(
f"Order has been processed. Status: {response.json()['value']['Status']}"
)
break
sleep(30)
@retry(tries=5, delay=5)
def get_response_from_next_link(next_link: str) -> dict:
"""Gets response from sending a GET request to the given next link."""
response = (
requests.get(
next_link,
headers=generate_headers(catalogue_keycloak, KEYCLOAK_CATALOGUE_JSON),
)
if "/stac/" in next_link
else requests.get(next_link)
)
assert response.status_code == 200, print(
f"Received status code {response.status_code}. Sending another request."
)
return response.json()
@retry(tries=5, delay=5)
def create_batch_order_with_query(hours: int | None = None) -> None:
"""Option 2 - Creates new BatchOrder with a given query_url."""
if hours:
modify_query_by_given_hour_mark(hours)
query_details_json = get_query_details()
print("Getting response from the given query_url")
response = (
requests.get(f"{query_details_json['query_url']}&$count=true")
if "/stac/" not in query_details_json["query_url"]
else requests.get(
query_details_json["query_url"],
headers=generate_headers(catalogue_keycloak, KEYCLOAK_CATALOGUE_JSON),
)
)
identifiers = []
assert response.status_code == 200, print(
f"Received status code {response.status_code}. Sending another request."
)
if query_details_json["new_orders"]:
create_batch_order_with_query_new_orders(
query_details_json, response.json(), identifiers
)
else:
create_batch_order_with_query_no_new_orders(
query_details_json, response.json(), identifiers
)
def create_batch_order_with_query_no_new_orders(
variables: json, response: json, identifiers: list
) -> None:
"""Option 2 - Creates new BatchOrder with a given query_url, without waiting for order to be processed."""
while response:
products = response["value"] if "value" in response else response["features"]
products_count = (
response["@odata.count"]
if "@odata.count" in response
else response["numberMatched"]
)
total_items = 0
for product in products:
if len(identifiers) < variables["parallel_quota"]:
(
identifiers.append(product["Name"])
if "Name" in product
else identifiers.append(product["id"])
)
total_items += 1
print(f"Loaded {identifiers[-1]} to a list of products")
if (
len(identifiers) == variables["parallel_quota"]
or products_count == total_items
):
order_body = generate_order_body_from_query(
ORDER_BODY_JSON, identifiers
)
send_post_request_to_batch_order_and_validate_response(order_body)
break
if (
"@odata.nextLink" in response
and len(identifiers) < variables["parallel_quota"]
):
print(
f"All products have been loaded to a list. Going to another link for more products: "
f"{response['@odata.nextLink']}"
)
response = get_response_from_next_link(response["@odata.nextLink"])
elif "links" in response and len(identifiers) < variables["parallel_quota"]:
for link in response["links"]:
if link["rel"] == "next":
print(
f"All products have been loaded to a list. Going to another link for more products: "
f"{link['href']}"
)
response = get_response_from_next_link(link["href"])
else:
break
def create_batch_order_with_query_new_orders(
variables: json, response: json, identifiers: list
) -> None:
"""Option 2 - Creates new BatchOrder with a given query_url, with waiting for order to be processed."""
products_count = (
response["@odata.count"]
if "@odata.count" in response
else response["numberMatched"]
)
total_items = 0
while response:
products = response["value"] if "value" in response else response["features"]
for product in products:
if len(identifiers) < variables["parallel_quota"]:
(
identifiers.append(product["Name"])
if "Name" in product
else identifiers.append(product["id"])
)
total_items += 1
print(f"Loaded {identifiers[-1]} to a list of products")
if (
len(identifiers) == variables["parallel_quota"]
or products_count == total_items
):
order_body = generate_order_body_from_query(
ORDER_BODY_JSON, identifiers
)
post_response = send_post_request_to_batch_order_and_validate_response(
order_body
)
if "value" in post_response.json():
wait_for_order_to_be_processed(post_response.json()["value"]["Id"])
identifiers.clear()
else:
break
if (
"@odata.nextLink" in response
and len(identifiers) < variables["parallel_quota"]
):
print(
f"All products have been loaded to a list. Going to another link for more products: "
f"{response['@odata.nextLink']}"
)
response = get_response_from_next_link(response["@odata.nextLink"])
elif "links" in response and len(identifiers) < variables["parallel_quota"]:
link_counter = 0
for link in response["links"]:
link_counter += 1
if link["rel"] == "next":
print(
f"All products have been loaded to a list. Going to another link for more products: "
f"{link['href']}"
)
response = get_response_from_next_link(link["href"])
elif link_counter == len(response["links"]):
break
else:
break
def add_timezone_chars_to_date(date: str) -> str:
"""Adds 'T' and 'Z' chars to the given date."""
date = date.replace(" ", "T")
date += "Z"
return date
def get_date_from_query(query_url: str, sub1: str, sub2: str) -> str:
"""Extracts date from query."""
idx1 = query_url.index(sub1)
idx2 = query_url.find(sub2, idx1)
res = ""
for idx in range(idx1 + len(sub1) + 1, idx2):
res = res + query_url[idx]
return res
def modify_query_by_given_hour_mark(hours: int) -> None:
"""Modifies query_url variable to take into account 'hours' variable specified by the user."""
with open(f"{PWD}/jsons/query_details.json", "r+") as jsonFile:
data = json.load(jsonFile)
query_url = data["query_url"]
current_date = datetime.datetime.now()
earlier_date = str(current_date - datetime.timedelta(hours=hours))
current_date = str(current_date)
current_date = add_timezone_chars_to_date(current_date)
earlier_date = add_timezone_chars_to_date(earlier_date)
if "/stac/" in query_url:
params = {"datetime": f"{earlier_date}/{current_date}"}
url_parts = urllib.parse.urlparse(query_url)
query = dict(urllib.parse.parse_qsl(url_parts.query))
query.update(params)
query_url = url_parts._replace(query=urllib.parse.urlencode(query)).geturl()
else:
if "ContentDate/Start%20ge" in query_url:
if "ContentDate/Start%20le" not in query_url:
query_url = query_url.replace(
"ContentDate/Start%20ge%20"
+ get_date_from_query(
query_url, "ContentDate/Start%20ge%2", ")%20and%20"
),
"ContentDate/Start%20ge%20"
+ f"{earlier_date}%20and%20ContentDate/Start%20le%20{current_date}",
)
else:
query_url = query_url.replace(
get_date_from_query(
query_url, "ContentDate/Start%20le%2", ")%20and%20"
),
current_date,
)
query_url = query_url.replace(
get_date_from_query(
query_url,
"ContentDate/Start%20ge%2",
"%20and%20ContentDate",
),
earlier_date,
)
elif "ContentDate/Start%20le" in query_url:
query_url = query_url.replace(
"ContentDate/Start%20le%20"
+ get_date_from_query(
query_url, "ContentDate/Start%20le%2", ")%20and%20"
),
"ContentDate/Start%20ge%20"
+ f"{earlier_date}%20and%20ContentDate/Start%20le%20{current_date}",
)
else:
query_url = query_url.replace(
"filter=",
"filter=" + f"(ContentDate/Start%20ge%20"
f"{earlier_date}%20and%20ContentDate/Start%20le%20{current_date})%20and%20",
)
data["query_url"] = query_url
jsonFile.seek(0)
json.dump(data, jsonFile)
jsonFile.truncate()
def check_order_details(order_id: int) -> None:
"""Option 3 - Outputs json from BatchOrder(order_id)"""
response = requests.get(
f"https://{KEYCLOAK_ORDERING_JSON['host']}/odata/v1/BatchOrder({order_id})",
headers=generate_headers(ordering_keycloak, KEYCLOAK_ORDERING_JSON),
verify=False,
)
print(response.json())