Skip to content
Closed
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import httpc
///
pub fn main() {
let creds = bucket.credentials(
host: "s3-api-host.example.com",
base_url: "https://s3-api-host.example.com",
access_key_id: "YOUR_ACCESS_KEY",
secret_access_key: "YOUR_SECRET_ACCESS_KEY",
)
Expand All @@ -50,7 +50,7 @@ pub fn main() {
let assert Ok(Found(object)) = get_object.response(response)

// Print the string contents
let assert Ok(text) = bit_array.from_string(object)
let assert Ok(text) = bit_array.to_string(object)
io.println(text)
}
```
Expand Down
28 changes: 10 additions & 18 deletions src/bucket.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gleam/http.{type Scheme}
import gleam/http/response.{type Response}
import gleam/option.{type Option}
import gleam/uri

pub type BucketError {
InvalidXmlSyntaxError(String)
Expand All @@ -22,9 +22,9 @@ pub type ErrorObject {
/// The creds used to connect to an S3 API.
pub type Credentials {
Credentials(
scheme: Scheme,
scheme: Option(String),
port: Option(Int),
host: String,
host: Option(String),
region: String,
access_key_id: String,
secret_access_key: String,
Expand All @@ -33,17 +33,19 @@ pub type Credentials {
}

pub fn credentials(
host: String,
base_url: String,
access_key_id: String,
secret_access_key: String,
) -> Credentials {
let assert Ok(parsed_url) = uri.parse(base_url)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Libraries must never panic!


Credentials(
host:,
host: parsed_url.host,
port: parsed_url.port,
scheme: parsed_url.scheme,
region: "eu-west-1",
access_key_id:,
secret_access_key:,
region: "eu-west-1",
port: option.None,
scheme: http.Https,
session_token: option.None,
)
}
Expand All @@ -53,16 +55,6 @@ pub fn with_region(creds: Credentials, region: String) -> Credentials {
Credentials(..creds, region:)
}

/// Set the port for the credentials.
pub fn with_port(creds: Credentials, port: Int) -> Credentials {
Credentials(..creds, port: option.Some(port))
}

/// Set the scheme for the credentials. You should use HTTPS unless not possible.
pub fn with_scheme(creds: Credentials, scheme: http.Scheme) -> Credentials {
Credentials(..creds, scheme:)
}

/// Set the optional session token, which could have given via a task or
/// instance role if these are used within your deployment environment.
pub fn with_session_token(
Expand Down
20 changes: 12 additions & 8 deletions src/bucket/internal.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import gleam/http/request.{type Request, Request}
import gleam/http/response.{type Response}
import gleam/list
import gleam/option.{type Option}
import gleam/result
import gleam/uri
import xmlm

Expand Down Expand Up @@ -43,14 +44,17 @@ pub fn request(
}
let request =
Request(
method,
headers,
body,
creds.scheme,
creds.host,
creds.port,
path,
query,
method:,
headers:,
body:,
scheme: result.unwrap(
http.scheme_from_string(option.unwrap(creds.scheme, "https")),
http.Https,
),
host: option.unwrap(creds.host, ""),
port: creds.port,
path:,
query:,
)
aws4_request.signer(
creds.access_key_id,
Expand Down
9 changes: 4 additions & 5 deletions test/helpers.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ import bucket/head_object
import bucket/list_buckets
import bucket/list_objects
import bucket/put_object
import gleam/http
import gleam/httpc
import gleam/list
import gleam/option

pub const creds = bucket.Credentials(
scheme: http.Http,
scheme: option.Some("http"),
port: option.Some(9000),
host: "localhost",
host: option.Some("localhost"),
region: "us-east-1",
access_key_id: "minioadmin",
secret_access_key: "miniopass",
session_token: option.None,
)

pub const bad_creds = bucket.Credentials(
scheme: http.Http,
scheme: option.Some("http"),
port: option.Some(9000),
host: "localhost",
host: option.Some("localhost"),
region: "us-east-1",
access_key_id: "unknown",
secret_access_key: "nope",
Expand Down