-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Submit STAC using transactions API #297
Open
ividito
wants to merge
2
commits into
dev
Choose a base branch
from
feat/transactions-api
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
dags/veda_data_pipeline/utils/submit_stac_transactions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import json | ||
import logging | ||
import requests | ||
from typing import List, TypedDict | ||
|
||
import boto3 | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
class Creds(TypedDict): | ||
access_token: str | ||
expires_in: int | ||
token_type: str | ||
|
||
class AppConfig(TypedDict): | ||
cognito_domain: str | ||
client_id: str | ||
client_secret: str | ||
scope: str | ||
|
||
class TransactionsApi: | ||
|
||
@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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Raise should be derived from a baseException otherwise it won't raise a string 🤔 raise RuntimeError(f"Error, {ex}") |
||
return response.json() | ||
|
||
def __init__(self, stac_ingestor_api_url: str, cognito_app_secret: str = None): | ||
""" | ||
:param stac_endpoint: Base URL of the STAC API (e.g., 'https://example.com/stac'). | ||
:param token: Optional Bearer token for authenticated STAC APIs. | ||
""" | ||
self.stac_ingestor_api_url = stac_ingestor_api_url.rstrip('/') | ||
self.cognito_app_secret = cognito_app_secret | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where are we using |
||
|
||
def post_items(self, collection_id: str, items: List[dict]) -> dict: | ||
""" | ||
Perform a PUT request to update or create a STAC Item in the given collection. | ||
|
||
:param collection_id: The target collection ID. | ||
:param item_id: The target item ID. | ||
:param item_body: The full STAC Item JSON body. | ||
:return: The JSON response (as a dict) from the STAC API. | ||
:raises RuntimeError: If the response is not 200/201. | ||
""" | ||
url = f"{self.base_url.rstrip('/')}{self.stac_ingestor_api_url}/collections/{collection_id}/bulk_items" | ||
headers = {"Content-Type": "application/json"} | ||
|
||
if self.token: | ||
headers["Authorization"] = f"Bearer {self.token}" | ||
|
||
logging.info(f"PUT {url}") | ||
response = requests.post(url, headers=headers, json=items) | ||
|
||
if response.status_code not in (200, 201): | ||
logging.error("Failed PUT request: %s %s", response.status_code, response.text) | ||
raise RuntimeError(f"PUT request failed: {response.text}") | ||
|
||
return response.json() | ||
|
||
|
||
def submit_transactions_handler( | ||
event, | ||
cognito_app_secret=None, | ||
stac_ingestor_api_url=None, | ||
): | ||
""" | ||
Handler function that can be integrated in the same way as the existing `submission_handler`, | ||
but uses the TransactionsApi to perform a PUT request to STAC's Transactions endpoint. | ||
|
||
:param event: A dict containing the data needed for STAC item submission, | ||
including collection_id, item_id, and the STAC item body itself. | ||
:param context: (Optional) context object, for AWS Lambda or similar environments. | ||
:return: A dict representing the API response. | ||
""" | ||
|
||
collection_id = event[0].get("collection") | ||
api = TransactionsApi(stac_ingestor_api_url, cognito_app_secret) | ||
try: | ||
response = api.post_items(collection_id, event) | ||
logging.info("STAC Item POST completed successfully.") | ||
except RuntimeError as err: | ||
logging.error("Error while performing POST: %s", str(err)) | ||
raise | ||
return { | ||
"statusCode": 200, | ||
"body": json.dumps({ | ||
"message": "POST request completed successfully", | ||
"response": response | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the return type hint be "TransactionsApi" instead of "IngestionApi"