From 10ce66cb50fa977c7138a5a172d4503db2b2b0ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Greinhofer?= Date: Sat, 7 Dec 2024 15:05:13 -0600 Subject: [PATCH] Update BNA REST API client (#19) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updates the BNA REST API client to the current version. Drive-by: - Updates runtime dependencies. - Adds test-log to the dev dependencies. Signed-off-by: Rémy Greinhofer --- Cargo.lock | 113 +++++++++++++++++++++++++++++++- Cargo.toml | 6 +- lambdas/Cargo.toml | 3 + lambdas/src/bna-save-results.rs | 23 +++++-- 4 files changed, 136 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6bfe51b..ddf6871 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,6 +93,55 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys 0.59.0", +] + [[package]] name = "anyhow" version = "1.0.92" @@ -743,7 +792,7 @@ dependencies = [ [[package]] name = "bnaclient" version = "1.0.0" -source = "git+https://github.com/PeopleForBikes/bna-api?rev=c8ca326#c8ca32611e7239c7ac8c8b46fa3ee18a4881079c" +source = "git+https://github.com/PeopleForBikes/bna-api?rev=1b40300#1b4030044b807d8a8bf1540b5dae976051a24d28" dependencies = [ "bytes", "chrono", @@ -811,6 +860,7 @@ dependencies = [ "simple-error", "slug", "svg2pdf", + "test-log", "tokio", "tracing", "tracing-subscriber", @@ -999,6 +1049,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "const-oid" version = "0.9.6" @@ -1330,6 +1386,27 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", +] + +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -2205,6 +2282,12 @@ version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.12.1" @@ -4108,6 +4191,28 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "test-log" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dffced63c2b5c7be278154d76b479f9f9920ed34e7574201407f0b14e2bbb93" +dependencies = [ + "env_logger", + "test-log-macros", + "tracing-subscriber", +] + +[[package]] +name = "test-log-macros" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5999e24eaa32083191ba4e425deb75cdf25efefabe5aaccb7446dd0d4122a3f5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.89", +] + [[package]] name = "thiserror" version = "1.0.66" @@ -4638,6 +4743,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.11.0" diff --git a/Cargo.toml b/Cargo.toml index c4dcfef..213dd18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,8 @@ aws-sdk-s3 = "1.65.0" aws-sdk-sqs = "1.50.0" aws-smithy-types-convert = "0.60.8" axum = "0.7.7" -bnaclient = { git = "https://github.com/PeopleForBikes/bna-api", rev = "c8ca326" } +bnaclient = { git = "https://github.com/PeopleForBikes/bna-api", rev = "1b40300" } +# bnaclient = { path = "/Users/rgreinhofer/projects/PeopleForBikes/bna-api/bnaclient" } bnacore = { git = "https://github.com/PeopleForBikes/brokenspoke", rev = "c20ec31" } chrono = "0.4.19" clap = "4.5.20" @@ -44,7 +45,8 @@ serde_with = "3.11.0" simple-error = "0.3.0" slug = "0.1.6" svg2pdf = "0.12.0" -thiserror = "2.0.4" +test-log = "0.2.16" +thiserror = "2.0.5" tokio = "1.42.0" tower = "0.5.1" tower-cookies = "0.10.0" diff --git a/lambdas/Cargo.toml b/lambdas/Cargo.toml index 7ed4263..01b8778 100644 --- a/lambdas/Cargo.toml +++ b/lambdas/Cargo.toml @@ -42,6 +42,9 @@ url = { workspace = true, features = ["serde"] } uuid = { workspace = true, features = ["v4", "serde"] } usvg = { workspace = true } +[dev-dependencies] +test-log = { workspace = true, features = ["trace"] } + [[bin]] name = "bna-fargate-run" path = "src/bna-fargate-run.rs" diff --git a/lambdas/src/bna-save-results.rs b/lambdas/src/bna-save-results.rs index bd5c91d..312b657 100644 --- a/lambdas/src/bna-save-results.rs +++ b/lambdas/src/bna-save-results.rs @@ -10,8 +10,7 @@ use bnalambdas::{create_service_account_bna_client, AnalysisParameters, Context, use csv::ReaderBuilder; use heck::ToTitleCase; use lambda_runtime::{run, service_fn, Error, LambdaEvent}; -use rust_decimal::prelude::ToPrimitive; -use rust_decimal::Decimal; +use rust_decimal::{prelude::ToPrimitive, Decimal}; use rust_decimal_macros::dec; use serde::Deserialize; use simple_error::SimpleError; @@ -180,9 +179,10 @@ async fn get_or_create_city( region: &str, name: &str, ) -> Result { + let normalized_country = Country::from_str(country.to_title_case().as_str())?; let response = client .get_city() - .country(country) + .country(normalized_country) .region(region) .name(name) .send() @@ -199,20 +199,21 @@ async fn get_or_create_city( } }; info!("City: {:#?}", city); + dbg!(&city); let city_id: Uuid; if let Some(city) = city { info!("The city exists, update the population..."); - city_id = city.city_id.expect("a city id to be present"); + city_id = city.id; } else { info!("Create a new city..."); // Create the city. let c = CityPost::builder() - .country(Country::from_str(country.to_title_case().as_str())?) + .country(normalized_country) .state(Some(region.to_title_case())) .name(name.to_title_case()); let city = client.post_city().body(c).send().await?; - city_id = city.city_id.expect("a city id to be present"); + city_id = city.id; } Ok(city_id) @@ -358,6 +359,7 @@ async fn main() -> Result<(), Error> { mod tests { use super::*; use aws_sdk_s3::primitives::DateTime; + use test_log::test; #[test] fn test_input_deserialization() { @@ -648,4 +650,13 @@ mod tests { // let event = LambdaEvent::new(payload, context); // function_handler(event).await.unwrap(); // } + + // #[test(tokio::test)] + // async fn test_get_or_create_city() { + // let client = bnaclient::Client::new("http://localhost:3000"); + // let city = get_or_create_city(&client, "United States", "New Mexico", "Santa Rosa") + // .await + // .unwrap(); + // dbg!(city); + // } }