Remove reexports
This commit is contained in:
parent
c41a0c5dba
commit
c1a7debb84
3 changed files with 94 additions and 111 deletions
|
@ -56,10 +56,11 @@ import spacetraders_models/waypoint_modifier.{type WaypointModifier}
|
|||
import spacetraders_models/waypoint_symbol.{type WaypointSymbol}
|
||||
import spacetraders_models/waypoint_trait_symbol.{type WaypointTraitSymbol}
|
||||
import spacetraders_models/waypoint_type.{type WaypointType}
|
||||
import spacetraders_sdk/internal/api.{
|
||||
import spacetraders_sdk.{
|
||||
type AccountToken, type AgentToken, type ApiResponse, type PagedData,
|
||||
AccountAuth, AgentAuth, NoAuth,
|
||||
}
|
||||
import spacetraders_sdk/internal/api
|
||||
import spacetraders_sdk/internal/time
|
||||
|
||||
pub fn get_account(token: AgentToken) -> ApiResponse(Account) {
|
||||
|
@ -103,7 +104,10 @@ pub fn register_new_agent(
|
|||
case response.status {
|
||||
201 ->
|
||||
api.parse_data_response(response, {
|
||||
use token <- decode.field("token", api.agent_token_decoder())
|
||||
use token <- decode.field(
|
||||
"token",
|
||||
spacetraders_sdk.agent_token_decoder(),
|
||||
)
|
||||
use agent <- decode.field("agent", agent.decoder())
|
||||
use faction <- decode.field("faction", faction.decoder())
|
||||
use contract <- decode.field("contract", contract.decoder())
|
||||
|
|
|
@ -1,33 +1,93 @@
|
|||
import spacetraders_sdk/internal/api
|
||||
import gleam/dynamic.{type Dynamic}
|
||||
import gleam/dynamic/decode.{type Decoder}
|
||||
import gleam/http/request.{type Request}
|
||||
import gleam/httpc.{type HttpError}
|
||||
import gleam/json.{type DecodeError}
|
||||
import gleam/option.{type Option}
|
||||
import gleam/result
|
||||
import spacetraders_models/meta.{type Meta}
|
||||
import spacetraders_sdk/internal/jwt
|
||||
|
||||
pub type ApiResponse(data) =
|
||||
api.ApiResponse(data)
|
||||
pub type ErrorResponse {
|
||||
ErrorResponse(
|
||||
code: Int,
|
||||
message: String,
|
||||
data: Option(Dynamic),
|
||||
request_id: Option(String),
|
||||
)
|
||||
}
|
||||
|
||||
pub type ApiError =
|
||||
api.ApiError
|
||||
pub type ApiError {
|
||||
HttpcError(HttpError)
|
||||
JsonDecodeError(DecodeError)
|
||||
ResponseError(ErrorResponse)
|
||||
}
|
||||
|
||||
pub type ErrorResponse =
|
||||
api.ErrorResponse
|
||||
pub type ApiResponse(a) =
|
||||
Result(a, ApiError)
|
||||
|
||||
pub type PagedData(data) =
|
||||
api.PagedData(data)
|
||||
pub type PagedData(data) {
|
||||
PagedData(data: data, meta: Meta)
|
||||
}
|
||||
|
||||
pub type AccountToken =
|
||||
api.AccountToken
|
||||
pub opaque type AccountToken {
|
||||
AccountToken(token: String)
|
||||
}
|
||||
|
||||
pub type AgentToken =
|
||||
api.AgentToken
|
||||
pub opaque type AgentToken {
|
||||
AgentToken(token: String)
|
||||
}
|
||||
|
||||
pub type TokenParseError =
|
||||
api.TokenParseError
|
||||
pub type TokenParseError {
|
||||
InvalidToken
|
||||
IncorrectType
|
||||
}
|
||||
|
||||
pub type AuthMethod =
|
||||
api.AuthMethod
|
||||
pub type AuthMethod {
|
||||
AccountAuth(AccountToken)
|
||||
AgentAuth(AgentToken)
|
||||
NoAuth
|
||||
}
|
||||
|
||||
pub const parse_account_token = api.parse_account_token
|
||||
pub fn parse_account_token(
|
||||
value: String,
|
||||
) -> Result(AccountToken, TokenParseError) {
|
||||
use jwt <- result.try(jwt.parse(value) |> result.replace_error(InvalidToken))
|
||||
case jwt.payload.subject {
|
||||
"account-token" -> Ok(AccountToken(value))
|
||||
_ -> Error(IncorrectType)
|
||||
}
|
||||
}
|
||||
|
||||
pub const account_token_decoder = api.account_token_decoder
|
||||
pub fn account_token_decoder() -> Decoder(AccountToken) {
|
||||
use value <- decode.then(decode.string)
|
||||
case parse_account_token(value) {
|
||||
Ok(token) -> decode.success(token)
|
||||
Error(_) -> decode.failure(AccountToken("invalid"), "AccountToken")
|
||||
}
|
||||
}
|
||||
|
||||
pub const parse_agent_token = api.parse_agent_token
|
||||
pub fn parse_agent_token(value: String) -> Result(AgentToken, TokenParseError) {
|
||||
use jwt <- result.try(jwt.parse(value) |> result.replace_error(InvalidToken))
|
||||
case jwt.payload.subject {
|
||||
"agent-token" -> Ok(AgentToken(value))
|
||||
_ -> Error(IncorrectType)
|
||||
}
|
||||
}
|
||||
|
||||
pub const agent_token_decoder = api.agent_token_decoder
|
||||
pub fn agent_token_decoder() -> Decoder(AgentToken) {
|
||||
use value <- decode.then(decode.string)
|
||||
case parse_agent_token(value) {
|
||||
Ok(token) -> decode.success(token)
|
||||
Error(_) -> decode.failure(AgentToken("invalid"), "AgentToken")
|
||||
}
|
||||
}
|
||||
|
||||
@internal
|
||||
pub fn set_request_auth(req: Request(a), auth_method: AuthMethod) -> Request(a) {
|
||||
case auth_method {
|
||||
NoAuth -> req
|
||||
AccountAuth(AccountToken(token)) | AgentAuth(AgentToken(token)) ->
|
||||
req |> request.set_header("Authorization", "Bearer " <> token)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,99 +1,18 @@
|
|||
import gleam/bit_array
|
||||
import gleam/dynamic.{type Dynamic}
|
||||
import gleam/dynamic/decode.{type Decoder}
|
||||
import gleam/http.{type Method}
|
||||
import gleam/http/request.{type Request}
|
||||
import gleam/http/response.{type Response}
|
||||
import gleam/httpc.{type HttpError}
|
||||
import gleam/httpc
|
||||
import gleam/int
|
||||
import gleam/json.{type DecodeError, type Json}
|
||||
import gleam/json.{type Json}
|
||||
import gleam/option.{type Option}
|
||||
import gleam/result
|
||||
import gleam/string_tree
|
||||
import spacetraders_models/meta.{type Meta}
|
||||
import spacetraders_sdk/internal/jwt
|
||||
|
||||
pub type ErrorResponse {
|
||||
ErrorResponse(
|
||||
code: Int,
|
||||
message: String,
|
||||
data: Option(Dynamic),
|
||||
request_id: Option(String),
|
||||
)
|
||||
}
|
||||
|
||||
pub type ApiError {
|
||||
HttpcError(HttpError)
|
||||
JsonDecodeError(DecodeError)
|
||||
ResponseError(ErrorResponse)
|
||||
}
|
||||
|
||||
pub type ApiResponse(a) =
|
||||
Result(a, ApiError)
|
||||
|
||||
pub type PagedData(data) {
|
||||
PagedData(data: data, meta: Meta)
|
||||
}
|
||||
|
||||
pub opaque type AccountToken {
|
||||
AccountToken(token: String)
|
||||
}
|
||||
|
||||
pub opaque type AgentToken {
|
||||
AgentToken(token: String)
|
||||
}
|
||||
|
||||
pub type TokenParseError {
|
||||
InvalidToken
|
||||
IncorrectType
|
||||
}
|
||||
|
||||
pub type AuthMethod {
|
||||
AccountAuth(AccountToken)
|
||||
AgentAuth(AgentToken)
|
||||
NoAuth
|
||||
}
|
||||
|
||||
pub fn parse_account_token(
|
||||
value: String,
|
||||
) -> Result(AccountToken, TokenParseError) {
|
||||
use jwt <- result.try(jwt.parse(value) |> result.replace_error(InvalidToken))
|
||||
case jwt.payload.subject {
|
||||
"account-token" -> Ok(AccountToken(value))
|
||||
_ -> Error(IncorrectType)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn account_token_decoder() -> Decoder(AccountToken) {
|
||||
use value <- decode.then(decode.string)
|
||||
case parse_account_token(value) {
|
||||
Ok(token) -> decode.success(token)
|
||||
Error(_) -> decode.failure(AccountToken("invalid"), "AccountToken")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_agent_token(value: String) -> Result(AgentToken, TokenParseError) {
|
||||
use jwt <- result.try(jwt.parse(value) |> result.replace_error(InvalidToken))
|
||||
case jwt.payload.subject {
|
||||
"agent-token" -> Ok(AgentToken(value))
|
||||
_ -> Error(IncorrectType)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn agent_token_decoder() -> Decoder(AgentToken) {
|
||||
use value <- decode.then(decode.string)
|
||||
case parse_agent_token(value) {
|
||||
Ok(token) -> decode.success(token)
|
||||
Error(_) -> decode.failure(AgentToken("invalid"), "AgentToken")
|
||||
}
|
||||
}
|
||||
|
||||
fn set_auth(req: Request(a), auth_method: AuthMethod) -> Request(a) {
|
||||
case auth_method {
|
||||
NoAuth -> req
|
||||
AccountAuth(AccountToken(token)) | AgentAuth(AgentToken(token)) ->
|
||||
req |> request.set_header("Authorization", "Bearer " <> token)
|
||||
}
|
||||
import spacetraders_models/meta
|
||||
import spacetraders_sdk.{
|
||||
type ApiResponse, type AuthMethod, type ErrorResponse, type PagedData,
|
||||
ErrorResponse, HttpcError, JsonDecodeError, PagedData, ResponseError,
|
||||
}
|
||||
|
||||
const base_request = request.Request(
|
||||
|
@ -135,7 +54,7 @@ fn set_json_body(req: Request(a), json: Json) {
|
|||
fn make_request(method: Method, auth_method: AuthMethod, path: String) {
|
||||
base_request
|
||||
|> request.set_method(method)
|
||||
|> set_auth(auth_method)
|
||||
|> spacetraders_sdk.set_request_auth(auth_method)
|
||||
|> request.set_path(base_request.path <> path)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue