-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: HTTP supports anonymous and named arguments
- Loading branch information
1 parent
02c971f
commit bd313ef
Showing
21 changed files
with
810 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.beam | ||
*.ez | ||
/build | ||
erl_crash.dump |
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,24 @@ | ||
# app | ||
|
||
[![Package Version](https://img.shields.io/hexpm/v/app)](https://hex.pm/packages/app) | ||
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/app/) | ||
|
||
```sh | ||
gleam add app@1 | ||
``` | ||
```gleam | ||
import app | ||
pub fn main() { | ||
// TODO: An example of the project in use | ||
} | ||
``` | ||
|
||
Further documentation can be found at <https://hexdocs.pm/app>. | ||
|
||
## Development | ||
|
||
```sh | ||
gleam run # Run the project | ||
gleam test # Run the tests | ||
``` |
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,27 @@ | ||
name = "app" | ||
version = "1.0.0" | ||
|
||
# Fill out these fields if you intend to generate HTML documentation or publish | ||
# your project to the Hex package manager. | ||
# | ||
# description = "" | ||
# licences = ["Apache-2.0"] | ||
# repository = { type = "github", user = "", repo = "" } | ||
# links = [{ title = "Website", href = "" }] | ||
# | ||
# For a full reference of all the available options, you can have a look at | ||
# https://gleam.run/writing-gleam/gleam-toml/. | ||
|
||
[dependencies] | ||
gleam_stdlib = ">= 0.34.0 and < 2.0.0" | ||
glenv = ">= 0.4.0 and < 1.0.0" | ||
gleam_httpc = ">= 2.2.0 and < 3.0.0" | ||
# glibsql = ">= 0.5.1 and < 1.0.0" | ||
glibsql = { path = "../../" } | ||
decode = ">= 0.2.0 and < 1.0.0" | ||
dot_env = ">= 1.0.0 and < 2.0.0" | ||
thoas = ">= 1.2.1 and < 2.0.0" | ||
birl = ">= 1.7.1 and < 2.0.0" | ||
|
||
[dev-dependencies] | ||
gleeunit = ">= 1.0.0 and < 2.0.0" |
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,32 @@ | ||
# This file was generated by Gleam | ||
# You typically do not need to edit this file | ||
|
||
packages = [ | ||
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" }, | ||
{ name = "decode", version = "0.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "decode", source = "hex", outer_checksum = "965F517F67B8C172CA27A5C8E34C73733139E8C9E64736181B8C3179281F9793" }, | ||
{ name = "dot_env", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "simplifile"], otp_app = "dot_env", source = "hex", outer_checksum = "E7B84DC7B579553AF3B9F0A03B2F8DDB9B44521F553CCFBE633AA595C27F1A05" }, | ||
{ name = "envoy", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "CFAACCCFC47654F7E8B75E614746ED924C65BD08B1DE21101548AC314A8B6A41" }, | ||
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" }, | ||
{ name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" }, | ||
{ name = "gleam_httpc", version = "2.2.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "CF76C71002DEECF6DC5D9CA83D962728FAE166B57926BE442D827004D3C7DF1B" }, | ||
{ name = "gleam_javascript", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "483631D3001FCE8EB12ADEAD5E1B808440038E96F93DA7A32D326C82F480C0B2" }, | ||
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" }, | ||
{ name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" }, | ||
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, | ||
{ name = "glenv", version = "0.4.0", build_tools = ["gleam"], requirements = ["decode", "envoy", "gleam_stdlib"], otp_app = "glenv", source = "hex", outer_checksum = "46164B9FFEB08927FD2CEBD96C3AFBE082AD5CC2C6F2FC4A78EFDA9EB61E3510" }, | ||
{ name = "glibsql", version = "0.5.1", build_tools = ["gleam"], requirements = ["decode", "gleam_http", "gleam_javascript", "gleam_json", "gleam_stdlib"], source = "local", path = "../.." }, | ||
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" }, | ||
{ name = "simplifile", version = "2.0.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5FFEBD0CAB39BDD343C3E1CCA6438B2848847DC170BA2386DF9D7064F34DF000" }, | ||
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" }, | ||
] | ||
|
||
[requirements] | ||
birl = { version = ">= 1.7.1 and < 2.0.0"} | ||
decode = { version = ">= 0.2.0 and < 1.0.0" } | ||
dot_env = { version = ">= 1.0.0 and < 2.0.0" } | ||
gleam_httpc = { version = ">= 2.2.0 and < 3.0.0" } | ||
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } | ||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" } | ||
glenv = { version = ">= 0.4.0 and < 1.0.0" } | ||
glibsql = { path = "../../" } | ||
thoas = { version = ">= 1.2.1 and < 2.0.0" } |
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,98 @@ | ||
import app/internal/env | ||
import birl | ||
import gleam/httpc | ||
import gleam/list | ||
import gleam/option.{type Option, None, Some} | ||
import gleam/result | ||
import glibsql/http as glibsql | ||
|
||
pub type User { | ||
User( | ||
id: Int, | ||
email: String, | ||
created_at: birl.Time, | ||
updated_at: Option(birl.Time), | ||
) | ||
} | ||
|
||
pub fn main() { | ||
let env = case env.load() { | ||
Ok(env) -> env | ||
Error(reason) -> { | ||
panic as reason | ||
} | ||
} | ||
|
||
let statement = | ||
glibsql.new_statement() | ||
|> glibsql.with_query( | ||
"SELECT id, email, created_at, updated_at FROM users WHERE id = ?", | ||
) | ||
|> glibsql.with_argument(glibsql.AnonymousArgument(glibsql.Integer(1))) | ||
|
||
let request = | ||
glibsql.new_request() | ||
|> glibsql.with_database(env.database_name) | ||
|> glibsql.with_organization(env.database_organization) | ||
|> glibsql.with_token(env.database_auth_token) | ||
|> glibsql.with_statement(statement) | ||
|> glibsql.with_statement(glibsql.CloseStatement) | ||
|> glibsql.build | ||
|
||
let assert Ok(request) = request | ||
|
||
use response <- result.try(httpc.send(request)) | ||
|
||
let users = | ||
glibsql.decode_response(response.body) | ||
|> result.map(fn(resp) { | ||
list.filter(resp.results, fn(res) { | ||
case res { | ||
glibsql.ExecuteResponse(_, _) -> True | ||
glibsql.CloseResponse -> False | ||
} | ||
}) | ||
|> list.flat_map(fn(res) { | ||
case res { | ||
glibsql.ExecuteResponse(_columns, rows) -> { | ||
list.map(rows, fn(row) { | ||
let assert [id, email, created_at, updated_at] = row.values | ||
|
||
User( | ||
id: case id { | ||
glibsql.Integer(value) -> value | ||
_ -> panic as "Unexpected type" | ||
}, | ||
email: case email { | ||
glibsql.Text(value) -> value | ||
_ -> panic as "Unexpected type" | ||
}, | ||
created_at: case created_at { | ||
glibsql.Datetime(value) -> to_time(value) | ||
_ -> panic as "Unexpected type" | ||
}, | ||
updated_at: case updated_at { | ||
glibsql.Datetime(value) -> Some(to_time(value)) | ||
glibsql.Null -> None | ||
_ -> panic as "Unexpected type" | ||
}, | ||
) | ||
}) | ||
} | ||
_ -> [] | ||
} | ||
}) | ||
}) | ||
|> result.unwrap([]) | ||
|
||
let assert [ | ||
User(1, "[email protected]", _user_1_created_at, None), | ||
] = users | ||
|
||
Ok(Nil) | ||
} | ||
|
||
fn to_time(value: String) -> birl.Time { | ||
let assert Ok(time) = birl.parse(value) | ||
time | ||
} |
60 changes: 60 additions & 0 deletions
60
examples/01-http-select-anonymous-arguments/src/app/internal/env.gleam
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,60 @@ | ||
import decode | ||
import dot_env | ||
import gleam/list | ||
import gleam/string | ||
import glenv | ||
|
||
pub type Env { | ||
Env( | ||
database_name: String, | ||
database_organization: String, | ||
database_auth_token: String, | ||
) | ||
} | ||
|
||
const definitions = [ | ||
#("DATABASE_NAME", glenv.String), #("DATABASE_ORGANIZATION", glenv.String), | ||
#("DATABASE_AUTH_TOKEN", glenv.String), | ||
] | ||
|
||
pub fn load() -> Result(Env, String) { | ||
dot_env.load_default() | ||
|
||
let decoder = | ||
decode.into({ | ||
use database_name <- decode.parameter | ||
use database_organization <- decode.parameter | ||
use database_auth_token <- decode.parameter | ||
|
||
Env(database_name, database_organization, database_auth_token) | ||
}) | ||
|> decode.field("DATABASE_NAME", decode.string) | ||
|> decode.field("DATABASE_ORGANIZATION", decode.string) | ||
|> decode.field("DATABASE_AUTH_TOKEN", decode.string) | ||
|
||
case glenv.load(decoder, definitions) { | ||
Ok(env) -> Ok(env) | ||
Error(err) -> { | ||
let reason = case err { | ||
glenv.NotFoundError(key) -> "Environment variable not found: " <> key | ||
glenv.ParseError(key, _) -> | ||
"Failed to parse environment variable: " <> key | ||
glenv.DefinitionMismatchError(errors) -> { | ||
let errors = | ||
list.map(errors, fn(error) { | ||
error.expected | ||
<> " expected, got " | ||
<> error.found | ||
<> " at " | ||
<> string.join(error.path, "->") | ||
}) | ||
|
||
"Failed to match environment definition: " | ||
<> string.join(errors, ", ") | ||
} | ||
} | ||
|
||
Error(reason) | ||
} | ||
} | ||
} |
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,8 @@ | ||
-module(app_ffi). | ||
|
||
-export([ | ||
decode/1 | ||
]). | ||
|
||
decode(Json) -> | ||
thoas:decode(Json). |
12 changes: 12 additions & 0 deletions
12
examples/01-http-select-anonymous-arguments/test/app_test.gleam
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,12 @@ | ||
import gleeunit | ||
import gleeunit/should | ||
|
||
pub fn main() { | ||
gleeunit.main() | ||
} | ||
|
||
// gleeunit test functions end in `_test` | ||
pub fn hello_world_test() { | ||
1 | ||
|> should.equal(1) | ||
} |
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,4 @@ | ||
*.beam | ||
*.ez | ||
/build | ||
erl_crash.dump |
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,24 @@ | ||
# app | ||
|
||
[![Package Version](https://img.shields.io/hexpm/v/app)](https://hex.pm/packages/app) | ||
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/app/) | ||
|
||
```sh | ||
gleam add app@1 | ||
``` | ||
```gleam | ||
import app | ||
pub fn main() { | ||
// TODO: An example of the project in use | ||
} | ||
``` | ||
|
||
Further documentation can be found at <https://hexdocs.pm/app>. | ||
|
||
## Development | ||
|
||
```sh | ||
gleam run # Run the project | ||
gleam test # Run the tests | ||
``` |
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,27 @@ | ||
name = "app" | ||
version = "1.0.0" | ||
|
||
# Fill out these fields if you intend to generate HTML documentation or publish | ||
# your project to the Hex package manager. | ||
# | ||
# description = "" | ||
# licences = ["Apache-2.0"] | ||
# repository = { type = "github", user = "", repo = "" } | ||
# links = [{ title = "Website", href = "" }] | ||
# | ||
# For a full reference of all the available options, you can have a look at | ||
# https://gleam.run/writing-gleam/gleam-toml/. | ||
|
||
[dependencies] | ||
gleam_stdlib = ">= 0.34.0 and < 2.0.0" | ||
glenv = ">= 0.4.0 and < 1.0.0" | ||
gleam_httpc = ">= 2.2.0 and < 3.0.0" | ||
# glibsql = ">= 0.5.1 and < 1.0.0" | ||
glibsql = { path = "../../" } | ||
decode = ">= 0.2.0 and < 1.0.0" | ||
dot_env = ">= 1.0.0 and < 2.0.0" | ||
thoas = ">= 1.2.1 and < 2.0.0" | ||
birl = ">= 1.7.1 and < 2.0.0" | ||
|
||
[dev-dependencies] | ||
gleeunit = ">= 1.0.0 and < 2.0.0" |
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,32 @@ | ||
# This file was generated by Gleam | ||
# You typically do not need to edit this file | ||
|
||
packages = [ | ||
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" }, | ||
{ name = "decode", version = "0.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "decode", source = "hex", outer_checksum = "965F517F67B8C172CA27A5C8E34C73733139E8C9E64736181B8C3179281F9793" }, | ||
{ name = "dot_env", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "simplifile"], otp_app = "dot_env", source = "hex", outer_checksum = "E7B84DC7B579553AF3B9F0A03B2F8DDB9B44521F553CCFBE633AA595C27F1A05" }, | ||
{ name = "envoy", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "CFAACCCFC47654F7E8B75E614746ED924C65BD08B1DE21101548AC314A8B6A41" }, | ||
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" }, | ||
{ name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" }, | ||
{ name = "gleam_httpc", version = "2.2.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "CF76C71002DEECF6DC5D9CA83D962728FAE166B57926BE442D827004D3C7DF1B" }, | ||
{ name = "gleam_javascript", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "483631D3001FCE8EB12ADEAD5E1B808440038E96F93DA7A32D326C82F480C0B2" }, | ||
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" }, | ||
{ name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" }, | ||
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, | ||
{ name = "glenv", version = "0.4.0", build_tools = ["gleam"], requirements = ["decode", "envoy", "gleam_stdlib"], otp_app = "glenv", source = "hex", outer_checksum = "46164B9FFEB08927FD2CEBD96C3AFBE082AD5CC2C6F2FC4A78EFDA9EB61E3510" }, | ||
{ name = "glibsql", version = "0.5.1", build_tools = ["gleam"], requirements = ["decode", "gleam_http", "gleam_javascript", "gleam_json", "gleam_stdlib"], source = "local", path = "../.." }, | ||
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" }, | ||
{ name = "simplifile", version = "2.0.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5FFEBD0CAB39BDD343C3E1CCA6438B2848847DC170BA2386DF9D7064F34DF000" }, | ||
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" }, | ||
] | ||
|
||
[requirements] | ||
birl = { version = ">= 1.7.1 and < 2.0.0"} | ||
decode = { version = ">= 0.2.0 and < 1.0.0" } | ||
dot_env = { version = ">= 1.0.0 and < 2.0.0" } | ||
gleam_httpc = { version = ">= 2.2.0 and < 3.0.0" } | ||
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } | ||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" } | ||
glenv = { version = ">= 0.4.0 and < 1.0.0" } | ||
glibsql = { path = "../../" } | ||
thoas = { version = ">= 1.2.1 and < 2.0.0" } |
Oops, something went wrong.