Skip to content

Commit

Permalink
implement cursory api
Browse files Browse the repository at this point in the history
  • Loading branch information
asjohnston-asf committed May 1, 2024
1 parent 7ccada6 commit 6817c6b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
25 changes: 0 additions & 25 deletions apps/api/cloudformation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,6 @@ Resources:
ProtocolType: HTTP
Target: !GetAtt Lambda.Arn
CredentialsArn: !GetAtt ApiRole.Arn
Body:
openapi: 3.1.0
info:
title: Sample API
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
version: 0.1.9
servers:
- url: http://api.example.com/v1
description: Optional server description, e.g. Main (production) server
- url: http://staging-api.example.com
description: Optional server description, e.g. Internal staging server for testing
paths:
/users:
get:
summary: Returns a list of users.
description: Optional extended description in CommonMark or HTML.
responses:
'200': # status code
description: A JSON array of user names
content:
application/json:
schema:
type: array
items:
type: string

ApiOverrides:
Type: AWS::ApiGatewayV2::ApiGatewayManagedOverrides
Expand Down
50 changes: 49 additions & 1 deletion apps/api/src/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
import os

import boto3

s3 = boto3.client('s3')

def get_orbit_for_granule(granule: str, bucket: str, orbit_type: str):
platform = granule[0:3]
start_date = granule[17:32]
end_date = granule[33:48]

paginator = s3.get_paginator('list_objects_v2')
page_iterator = paginator.paginate(
Bucket=bucket,
Prefix=f'{orbit_type}/{platform}',
)
for page in page_iterator:
for item in page['Contents']:
filename = os.path.basename(item['Key'])
start = filename[42:57]
end = filename[58:73]
if start <= start_date <= end_date <= end:
return item['Key']
return None


def get_url(granule, bucket):
for orbit_type in ['AUX_POEORB', 'AUX_RESORB', 'AUX_PREORB']:
key = get_orbit_for_granule(granule, bucket, orbit_type)
if key:
return f'https://{bucket}.s3.amazonaws.com/{key}'
return None


def lambda_handler(event, context):
print(event)
bucket = os.environ['BUCKET_NAME']
granule = os.path.basename(event['rawpath'])
url = get_url(granule, bucket)

if url:
response = {
'isBase64Encoded': False,
'statusCode': 302,
'headers': {
'location': url,
}
}
else:
response = {
'isBase64Encoded': False,
'statusCode': 404,
'body': f'No orbit file found for {granule}'
}

return response

0 comments on commit 6817c6b

Please sign in to comment.