This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
forked from NASA-IMPACT/veda-data-airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_stac.py
127 lines (101 loc) · 3.28 KB
/
submit_stac.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
import json
import sys
from dataclasses import dataclass
import os
if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDict
from typing import Any, Dict, Optional, Union
import boto3
import requests
class InputBase(TypedDict):
dry_run: Optional[Any]
class S3LinkInput(InputBase):
stac_file_url: str
class StacItemInput(InputBase):
stac_item: Dict[str, Any]
class AppConfig(TypedDict):
cognito_domain: str
client_id: str
client_secret: str
scope: str
class Creds(TypedDict):
access_token: str
expires_in: int
token_type: str
@dataclass
class IngestionApi:
base_url: str
token: str
@classmethod
def from_veda_auth_secret(cls, *, secret_id: str, base_url: str) -> "IngestionApi":
cognito_details = cls._get_cognito_service_details(secret_id)
credentials = cls._get_app_credentials(**cognito_details)
return cls(token=credentials["access_token"], base_url=base_url)
@staticmethod
def _get_cognito_service_details(secret_id: str) -> AppConfig:
client = boto3.client("secretsmanager")
response = client.get_secret_value(SecretId=secret_id)
return json.loads(response["SecretString"])
@staticmethod
def _get_app_credentials(
cognito_domain: str, client_id: str, client_secret: str, scope: str, **kwargs
) -> Creds:
response = requests.post(
f"{cognito_domain}/oauth2/token",
headers={
"Content-Type": "application/x-www-form-urlencoded",
},
auth=(client_id, client_secret),
data={
"grant_type": "client_credentials",
# A space-separated list of scopes to request for the generated access token.
"scope": scope,
},
)
try:
response.raise_for_status()
except Exception as ex:
print(response.text)
raise f"Error, {ex}"
return response.json()
def submit(self, stac_item: Dict[str, Any]):
response = requests.post(
f"{self.base_url.rstrip('/')}/ingestions",
json=stac_item,
headers={"Authorization": f"bearer {self.token}"},
)
try:
response.raise_for_status()
except Exception as e:
print(response.text)
raise e
return response.json()
def submission_handler(
event: Union[S3LinkInput, StacItemInput],
cognito_app_secret=None,
stac_ingestor_api_url=None,
context={},
) -> None:
# print(f"SUBMISSION EVENT {event}")
stac_item = event
if event.get("dry_run"):
print("Dry run, not inserting, would have inserted:")
print(json.dumps(stac_item, indent=2))
return
ingestor = IngestionApi.from_veda_auth_secret(
secret_id=os.getenv("COGNITO_APP_SECRET", cognito_app_secret),
base_url=os.getenv("STAC_INGESTOR_API_URL", stac_ingestor_api_url),
)
ingestor.submit(stac_item)
# print("Successfully submitted STAC item")
if __name__ == "__main__":
filename = "example.ndjson"
sample_event = {
"stac_file_url": "example.ndjson",
# or
"stac_item": {},
"type": "collections",
}
submission_handler(sample_event)