Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ categories = ["utils"]
license = "MIT"

[dependencies]
aws-config = { version = "1.1.2", features = ["behavior-version-latest"] }
aws-sdk-sqs = "1.9.0"
aws-types = "1.1.2"
aws_lambda_events = "0.7.2"
lambda_http = "0.7.0"
mongodb = "2.3.1"
once_cell = "1.17.0"
serde_json = "1.0.79"
aws-config = { version = "1.1.2", features = ["behavior-version-latest"], optional = true }
aws-sdk-sqs = { version = "1.9.0", optional = true }
aws-types = { version = "1.1.2", optional = true }
aws_lambda_events = { version = "0.7.2", optional = true }
lambda_http = { version = "0.7.0", optional = true }
mongodb = { version = "2.3.1", optional = true }
once_cell = { version = "1.17.0", optional = true }
serde_json = { version = "1.0.79", optional = true }

[dev-dependencies]
tokio = { version = "1.17.0", features = ["full", "test-util"] }

[features]
headers = ["lambda_http", "serde_json"]
mongodb = ["dep:mongodb", "once_cell"]
network = ["aws_lambda_events", "lambda_http"]
sqs = ["aws-sdk-sqs", "aws-config", "serde_json"]
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Lambda Utils is a collection of utility modules designed to facilitate common ta

## Included Utils

### header.rs
### headers.rs
- **Description:** A utility module for parsing HTTP headers.
- **Usage:** Helps in extracting and processing headers from incoming HTTP requests.

Expand All @@ -23,11 +23,10 @@ Lambda Utils is a collection of utility modules designed to facilitate common ta
## Getting Started
To start using Lambda Utils in your AWS Rust Lambda projects, follow these steps:

1. Add Lambda Utils as a dependency in your `Cargo.toml` file:
1. Add Lambda Utils as a dependency in your project:

```toml
[dependencies]
lambda-utils = "0.1.0"
```sh
cargo add lambda-utils
```

2. Import the desired utility modules into your Rust Lambda project:
Expand Down
12 changes: 8 additions & 4 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ use lambda_http::{Request};
use std::collections::{HashMap};
use serde_json::{to_string};

pub fn get_header_user_agent(request: &Request) -> String {
pub fn get_header_value(request: &Request, value: String) -> String {
return request
.headers()
.get("User-Agent")
.get(value)
.map_or_else(
|| "Unknown User-Agent".to_string(),
|header_value| header_value.to_str().unwrap_or("Invalid User-Agent").to_string(),
|| "Unknown header value".to_string(),
|header_value| header_value.to_str().unwrap_or("Invalid header value").to_string(),
);


pub fn get_header_user_agent(request: &Request) -> String {
return get_header_value("User-Agent")
}

pub fn get_header_cookies(request: &Request) -> String {
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#[cfg(feature = "headers")]
pub mod headers;

#[cfg(feature = "mongodb")]
pub mod mongodb;

#[cfg(feature = "network")]
pub mod network;

#[cfg(feature = "sqs")]
pub mod sqs;
14 changes: 8 additions & 6 deletions src/mongodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ pub fn get_mongodb_client() -> Result<&'static Client, Box<dyn Error + Send + Sy
}

#[cfg(test)]
#[tokio::test]
async fn test_create_mongodb_client() {
env::set_var(get_mongodb_uri_env_key(), "mongodb+srv://foo:bar@cluster0.irqdk.mongodb.net/test");
mod tests {
#[tokio::test]
async fn test_create_mongodb_client() {
env::set_var(get_mongodb_uri_env_key(), "mongodb+srv://foo:bar@cluster0.irqdk.mongodb.net/test");

let client = create_mongodb_client().await;
let client = create_mongodb_client().await;

assert!(client.is_ok());
assert!(MONGODB_CLIENT.get().is_some());
assert!(client.is_ok());
assert!(MONGODB_CLIENT.get().is_some());
}
}