This commit is contained in:
parent
616a4b5476
commit
d5e6cbc95f
4 changed files with 23 additions and 32 deletions
|
@ -15,4 +15,4 @@ gleam_time = ">= 1.2.0 and < 2.0.0"
|
|||
|
||||
[dev-dependencies]
|
||||
gleeunit = ">= 1.5.1 and < 2.0.0"
|
||||
dot_env = ">= 1.2.0 and < 2.0.0"
|
||||
envoy = ">= 1.0.2 and < 2.0.0"
|
||||
|
|
|
@ -2,8 +2,7 @@
|
|||
# You typically do not need to edit this file
|
||||
|
||||
packages = [
|
||||
{ name = "dot_env", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "simplifile"], otp_app = "dot_env", source = "hex", outer_checksum = "F2B4815F1B5AF8F20A6EADBB393E715C4C35203EBD5BE8200F766EA83A0B18DE" },
|
||||
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
|
||||
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
|
||||
{ name = "gleam_erlang", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "7E6A5234F927C4B24F8054AB1E4572206C41F9E6D5C6C02273CB7531E7E5CED0" },
|
||||
{ name = "gleam_http", version = "4.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "0A62451FC85B98062E0907659D92E6A89F5F3C0FBE4AB8046C99936BF6F91DBC" },
|
||||
{ name = "gleam_httpc", version = "4.1.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "C670EBD46FC1472AD5F1F74F1D3938D1D0AC1C7531895ED1D4DDCB6F07279F43" },
|
||||
|
@ -11,11 +10,10 @@ packages = [
|
|||
{ name = "gleam_stdlib", version = "0.60.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "621D600BB134BC239CB2537630899817B1A42E60A1D46C5E9F3FAE39F88C800B" },
|
||||
{ name = "gleam_time", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_time", source = "hex", outer_checksum = "D71F1AFF7FEB534FF55E5DC58E534E9201BA75A444619788A2E4DEA4EBD87D16" },
|
||||
{ name = "gleeunit", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D33B7736CF0766ED3065F64A1EBB351E72B2E8DE39BAFC8ADA0E35E92A6A934F" },
|
||||
{ name = "simplifile", version = "2.2.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "C88E0EE2D509F6D86EB55161D631657675AA7684DAB83822F7E59EB93D9A60E3" },
|
||||
]
|
||||
|
||||
[requirements]
|
||||
dot_env = { version = ">= 1.2.0 and < 2.0.0" }
|
||||
envoy = { version = ">= 1.0.2 and < 2.0.0" }
|
||||
gleam_http = { version = ">= 4.0.0 and < 5.0.0" }
|
||||
gleam_httpc = { version = ">= 4.1.1 and < 5.0.0" }
|
||||
gleam_json = { version = ">= 3.0.1 and < 4.0.0" }
|
||||
|
|
|
@ -16,7 +16,7 @@ pub fn error_codes_test() {
|
|||
|
||||
pub fn account_test() {
|
||||
let assert Ok(_) =
|
||||
spacetraders_api.get_account(env.load_dotenv_unsafe().agent_token)
|
||||
spacetraders_api.get_account(env.load_env_unsafe().agent_token)
|
||||
}
|
||||
|
||||
pub fn supply_chain_test() {
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import dot_env
|
||||
import dot_env/env
|
||||
import envoy
|
||||
import gleam/result
|
||||
import spacetraders_sdk.{type AccountToken, type AgentToken}
|
||||
import spacetraders_sdk.{
|
||||
type AccountToken, type AgentToken, type TokenParseError,
|
||||
}
|
||||
|
||||
pub type Env {
|
||||
Env(account_token: AccountToken, agent_token: AgentToken)
|
||||
|
@ -9,25 +10,27 @@ pub type Env {
|
|||
|
||||
pub type EnvError {
|
||||
MissingVar(name: String)
|
||||
InvalidVar(name: String)
|
||||
InvalidTokenVar(name: String, error: TokenParseError)
|
||||
}
|
||||
|
||||
fn get(name: String) -> Result(String, EnvError) {
|
||||
envoy.get(name)
|
||||
|> result.replace_error(MissingVar(name:))
|
||||
}
|
||||
|
||||
fn get_token(name: String, parse: fn(String) -> Result(token, TokenParseError)) {
|
||||
get(name)
|
||||
|> result.then(fn(value) {
|
||||
parse(value) |> result.map_error(InvalidTokenVar(name:, error: _))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_account_token() -> Result(AccountToken, EnvError) {
|
||||
env.get_string("ACCOUNT_TOKEN")
|
||||
|> result.replace_error(MissingVar("ACCOUNT_TOKEN"))
|
||||
|> result.then(fn(value) {
|
||||
spacetraders_sdk.parse_account_token(value)
|
||||
|> result.replace_error(InvalidVar("ACCOUNT_TOKEN"))
|
||||
})
|
||||
get_token("ACCOUNT_TOKEN", spacetraders_sdk.parse_account_token)
|
||||
}
|
||||
|
||||
pub fn get_agent_token() -> Result(AgentToken, EnvError) {
|
||||
env.get_string("AGENT_TOKEN")
|
||||
|> result.replace_error(MissingVar("AGENT_TOKEN"))
|
||||
|> result.then(fn(value) {
|
||||
spacetraders_sdk.parse_agent_token(value)
|
||||
|> result.replace_error(InvalidVar("AGENT_TOKEN"))
|
||||
})
|
||||
get_token("AGENT_TOKEN", spacetraders_sdk.parse_agent_token)
|
||||
}
|
||||
|
||||
pub fn load_env() -> Result(Env, EnvError) {
|
||||
|
@ -36,11 +39,6 @@ pub fn load_env() -> Result(Env, EnvError) {
|
|||
Ok(Env(account_token:, agent_token:))
|
||||
}
|
||||
|
||||
pub fn load_dotenv() -> Result(Env, EnvError) {
|
||||
dot_env.load_default()
|
||||
load_env()
|
||||
}
|
||||
|
||||
pub fn load_env_unsafe() -> Env {
|
||||
let assert Ok(account_token) = get_account_token()
|
||||
as "Missing or invalid account token"
|
||||
|
@ -48,8 +46,3 @@ pub fn load_env_unsafe() -> Env {
|
|||
as "Missing or invalid agent token"
|
||||
Env(account_token:, agent_token:)
|
||||
}
|
||||
|
||||
pub fn load_dotenv_unsafe() -> Env {
|
||||
dot_env.load_default()
|
||||
load_env_unsafe()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue