forked from Open-EO/openeo-eodc-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_setup.py
61 lines (45 loc) · 1.95 KB
/
api_setup.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
import os
import json
import requests
def get_headers(backend_url, username, password):
# Get token with Basic auth
response = requests.get(backend_url + "/credentials/basic", auth=(username, password))
if "v0.4" in backend_url:
headers = {'Authorization': 'Bearer ' + response.json()['token']}
else:
headers = {'Authorization': 'Bearer basic//' + response.json()['access_token']}
return headers
def add_processes(backend_url, auth_header):
# Load process list supported
processes = json.load(open("supported_processes.json"))["processes"]
processes_to_add = {process_name:{} for process_name in processes}
if "v0.4" in backend_url:
response = requests.post(backend_url + "/processes", json=processes_to_add, headers=auth_header)
if response.ok:
print('All processes added to back-end.')
print(response.headers['Location'])
else:
print(response.text)
else:
for process_name in processes:
response = requests.put(backend_url + "/processes/" + process_name, headers=auth_header)
if response.ok:
print(f'Successfully added process {process_name} to back-end.')
else:
print(process_name)
print(response.text)
print('\n')
def add_collections(backend_url, auth_header):
response = requests.post(backend_url + "/collections", headers=auth_header)
print(response.text)
if __name__ == '__main__':
# Get env variables
backend_url = os.environ['BACKEND_URL']
username = os.environ['USER_BASIC_ADMIN'] # user MUST have admin rights
password = os.environ['PASSWORD_BASIC_ADMIN']
# Generate auth headers
auth_header = get_headers(backend_url, username, password)
# Add processes
add_processes(backend_url, auth_header)
# Add collections
add_collections(backend_url, auth_header)