generated from PeopleForBikes/bna-mechanics-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new endpoint to enqueue a city onto the BNA pipeline queue. The common functions were removed from `lib.rs` and pulled from the bnacore library instead. The staging deployment workflow and the lambda fixtures were adjsuted accordingly. Signed-off-by: Rémy Greinhofer <[email protected]>
- Loading branch information
Showing
7 changed files
with
159 additions
and
70 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use aws_config::BehaviorVersion; | ||
use aws_sdk_sqs::{self}; | ||
use bnacore::aws::get_aws_parameter; | ||
use dotenv::dotenv; | ||
use lambda_http::{ | ||
http::StatusCode, run, service_fn, Body, Error, IntoResponse, Request, Response, | ||
}; | ||
use lambdas::{get_apigw_request_id, APIError, APIErrorSource, APIErrors}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
pub struct EnqueueCity { | ||
pub country: String, | ||
pub city: String, | ||
pub region: String, | ||
pub fips_code: String, | ||
} | ||
|
||
async fn function_handler(event: Request) -> Result<Response<Body>, Error> { | ||
dotenv().ok(); | ||
|
||
// Extract and serialize the data. | ||
let apigw_request_id = get_apigw_request_id(&event); | ||
let body = event.body(); | ||
let body_str = std::str::from_utf8(body).expect("invalid utf-8 sequence"); | ||
let enqueued_city = match serde_json::from_str::<EnqueueCity>(body_str) { | ||
Ok(data) => data, | ||
Err(e) => { | ||
let api_error = APIError::new( | ||
apigw_request_id, | ||
StatusCode::BAD_REQUEST, | ||
String::from("Invalid data"), | ||
format!("The following submission is invalid: {body_str}. {e}"), | ||
APIErrorSource::Pointer(event.uri().path().to_string()), | ||
); | ||
return Ok(APIErrors::new(&[api_error]).into()); | ||
} | ||
}; | ||
|
||
// Prepare the AWS client. | ||
let bna_sqs_queue = get_aws_parameter("BNA_SQS_QUEUE_URL").await?; | ||
let aws_config = aws_config::load_defaults(BehaviorVersion::latest()).await; | ||
let sqs_client = aws_sdk_sqs::Client::new(&aws_config); | ||
|
||
// Enqueue the message. | ||
let _send_message = match sqs_client | ||
.send_message() | ||
.queue_url(bna_sqs_queue) | ||
.message_body(serde_json::to_string(&enqueued_city)?) | ||
.send() | ||
.await | ||
{ | ||
Ok(message) => message, | ||
Err(e) => { | ||
let api_error = APIError::new( | ||
apigw_request_id, | ||
StatusCode::BAD_REQUEST, | ||
String::from("Invalid data"), | ||
format!("cannot enqueue the message: {e}"), | ||
APIErrorSource::Pointer(event.uri().path().to_string()), | ||
); | ||
return Ok(APIErrors::new(&[api_error]).into()); | ||
} | ||
}; | ||
|
||
Ok(json!(enqueued_city).into_response().await) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
// disable printing the name of the module in every log line. | ||
.with_target(false) | ||
// disabling time is handy because CloudWatch will add the ingestion time. | ||
.without_time() | ||
.init(); | ||
|
||
run(service_fn(function_handler)).await | ||
} |
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,63 @@ | ||
{ | ||
"version": "2.0", | ||
"routeKey": "$default", | ||
"rawPath": "/enqueue/city", | ||
"rawQueryString": "", | ||
"cookies": null, | ||
"headers": { | ||
"header1": "value1", | ||
"header2": "value1,value2" | ||
}, | ||
"queryStringParameters": { | ||
"parameter1": "value1,value2", | ||
"parameter2": "value" | ||
}, | ||
"requestContext": { | ||
"accountId": "123456789012", | ||
"apiId": "api-id", | ||
"authentication": { | ||
"clientCert": { | ||
"clientCertPem": "CERT_CONTENT", | ||
"subjectDN": "www.example.com", | ||
"issuerDN": "Example issuer", | ||
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1", | ||
"validity": { | ||
"notBefore": "May 28 12:30:02 2019 GMT", | ||
"notAfter": "Aug 5 09:36:04 2021 GMT" | ||
} | ||
} | ||
}, | ||
"authorizer": { | ||
"jwt": { | ||
"claims": { | ||
"claim1": "value1", | ||
"claim2": "value2" | ||
}, | ||
"scopes": ["scope1", "scope2"] | ||
} | ||
}, | ||
"domainName": "id.execute-api.us-east-1.amazonaws.com", | ||
"domainPrefix": "id", | ||
"http": { | ||
"method": "POST", | ||
"path": "/enqueue/city", | ||
"protocol": "HTTP/1.1", | ||
"sourceIp": "192.0.2.1", | ||
"userAgent": "agent" | ||
}, | ||
"requestId": "id", | ||
"routeKey": "$default", | ||
"stage": "$default", | ||
"time": "12/Mar/2020:19:03:58 +0000", | ||
"timeEpoch": 1583348638390 | ||
}, | ||
"body": "{\"country\": \"usa\",\"city\": \"santa rosa\",\"region\": \"new mexico\",\"fips_code\": \"3570670\"}", | ||
"pathParameters": { | ||
"parameter1": "value1" | ||
}, | ||
"isBase64Encoded": false, | ||
"stageVariables": { | ||
"stageVariable1": "value1", | ||
"stageVariable2": "value2" | ||
} | ||
} |
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