diff --git a/examples/info/src/info.gleam b/examples/info/src/info.gleam index 4ef87fe..908ca02 100644 --- a/examples/info/src/info.gleam +++ b/examples/info/src/info.gleam @@ -1,6 +1,7 @@ -import apkallone import gleam/io -import mastodon/instance + +import apkallone +import apkallone/mastodon/instance pub fn main() { io.println_error("Fetching...") diff --git a/src/apkallone/internal.gleam b/src/apkallone/internal.gleam index e18e0e4..d9a79d6 100644 --- a/src/apkallone/internal.gleam +++ b/src/apkallone/internal.gleam @@ -3,7 +3,6 @@ import gleam/http/request.{type Request} import gleam/http/response.{type Response} import gleam/httpc import gleam/json -import gleam/pair import gleam/result import gleam/string @@ -48,21 +47,26 @@ pub fn decode_response( fn require_content_type( res: Response(String), - expected_type: String, + content_type: String, fun: fn() -> Result(a, Error), ) -> Result(a, Error) { - use actual_type <- result.try( + use actual_content_type <- result.try( response.get_header(res, "content-type") |> result.replace_error(apkallone.MissingContentType), ) - let actual_type = - string.split_once(actual_type, on: ";") - |> result.map(pair.first) - |> result.unwrap(or: actual_type) + let actual_content_type = + strip_parameters_from_content_type(actual_content_type) - case actual_type == expected_type { + case actual_content_type == content_type { True -> fun() - False -> Error(apkallone.UnexpectedContentType(actual_type)) + False -> Error(apkallone.UnexpectedContentType(actual_content_type)) + } +} + +fn strip_parameters_from_content_type(content_type: String) -> String { + case string.split_once(content_type, on: ";") { + Ok(#(before_parameters, _parameters)) -> before_parameters + Error(Nil) -> content_type } } diff --git a/src/mastodon/instance.gleam b/src/apkallone/mastodon/instance.gleam similarity index 85% rename from src/mastodon/instance.gleam rename to src/apkallone/mastodon/instance.gleam index 404fa35..e49acea 100644 --- a/src/mastodon/instance.gleam +++ b/src/apkallone/mastodon/instance.gleam @@ -2,7 +2,7 @@ import gleam/result import apkallone import apkallone/internal -import mastodon/model/instance.{type Instance} +import apkallone/mastodon/model/instance.{type Instance} pub fn get_instance( client: apkallone.Client, diff --git a/src/mastodon/model/instance.gleam b/src/apkallone/mastodon/model/instance.gleam similarity index 100% rename from src/mastodon/model/instance.gleam rename to src/apkallone/mastodon/model/instance.gleam