-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Guillaume Hivert <[email protected]>
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 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,40 @@ | ||
import aws4_request | ||
import backend/config | ||
import birl | ||
import gleam/http | ||
import gleam/http/request | ||
import gleam/httpc | ||
import gleam/option.{type Option, None, Some} | ||
import gleam/result | ||
|
||
fn request(url: String, method: http.Method, body: Option(BitArray)) { | ||
let bucket_uri = config.bucket_uri() | ||
let date = birl.to_erlang_universal_datetime(birl.now()) | ||
let #(access_key, secret_key) = config.scaleway_keys() | ||
request.new() | ||
|> request.set_method(method) | ||
|> request.set_path(url) | ||
|> request.set_body(option.unwrap(body, <<>>)) | ||
|> request.set_host(bucket_uri) | ||
|> request.set_scheme(http.Https) | ||
|> request.set_header("content-type", "application/octet-stream") | ||
|> aws4_request.sign(date, access_key, secret_key, "fr-par", "s3") | ||
|> httpc.send_bits() | ||
|> result.nil_error() | ||
} | ||
|
||
pub fn get(name: String) { | ||
use res <- result.try(request("/" <> name, http.Get, None)) | ||
case res.status { | ||
200 -> Ok(res.body) | ||
_ -> Error(Nil) | ||
} | ||
} | ||
|
||
pub fn put(name: String, content: BitArray) { | ||
use res <- result.try(request("/" <> name, http.Put, Some(content))) | ||
case res.status { | ||
200 -> Ok(res.body) | ||
_ -> Error(Nil) | ||
} | ||
} |