Current section
Files
Jump to
Current section
Files
src/flwr_oauth2@response.erl
-module(flwr_oauth2@response).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/flwr_oauth2/response.gleam").
-export([parse_error_response/1, parse_token_response/1, to_string/1]).
-export_type([access_token_response/0, response_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type access_token_response() :: {access_token_response,
binary(),
binary(),
gleam@option:option(integer()),
gleam@option:option(binary()),
list(binary())}.
-type response_error() :: {error_response,
integer(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary())} |
{parse_error, gleam@json:decode_error()}.
-file("src/flwr_oauth2/response.gleam", 86).
?DOC(" Parses an OAuth 2.0 error response defined in [RFC6749 Error Response](https://datatracker.ietf.org/doc/html/rfc6749#section-5.2).\n").
-spec parse_error_response(gleam@http@response:response(binary())) -> response_error().
parse_error_response(Response) ->
Error_decoder = begin
gleam@dynamic@decode:field(
<<"error"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Error) ->
gleam@dynamic@decode:optional_field(
<<"error_description"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Error_description) ->
gleam@dynamic@decode:optional_field(
<<"error_uri"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Error_uri) ->
gleam@dynamic@decode:success(
{error_response,
erlang:element(2, Response),
Error,
Error_description,
Error_uri}
)
end
)
end
)
end
)
end,
_pipe = gleam@json:parse(erlang:element(4, Response), Error_decoder),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(Field@0) -> {parse_error, Field@0} end
),
flwr_oauth2@helpers:unwrap_both(_pipe@1).
-file("src/flwr_oauth2/response.gleam", 48).
-spec parse_token_success_response(gleam@http@response:response(binary())) -> {ok,
access_token_response()} |
{error, response_error()}.
parse_token_success_response(Response) ->
Token_decoder = begin
gleam@dynamic@decode:field(
<<"access_token"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Access_token) ->
gleam@dynamic@decode:field(
<<"token_type"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Token_type) ->
gleam@dynamic@decode:optional_field(
<<"expires_in"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_int/1}
),
fun(Expires_in) ->
gleam@dynamic@decode:optional_field(
<<"refresh_token"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Refresh_token) ->
gleam@dynamic@decode:optional_field(
<<"scope"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Scope) ->
Scope@1 = begin
_pipe = Scope,
_pipe@1 = gleam@option:map(
_pipe,
fun flwr_oauth2@common:parse_scope/1
),
gleam@option:unwrap(
_pipe@1,
[]
)
end,
gleam@dynamic@decode:success(
{access_token_response,
Access_token,
Token_type,
Expires_in,
Refresh_token,
Scope@1}
)
end
)
end
)
end
)
end
)
end
)
end,
_pipe@2 = gleam@json:parse(erlang:element(4, Response), Token_decoder),
gleam@result:map_error(_pipe@2, fun(Field@0) -> {parse_error, Field@0} end).
-file("src/flwr_oauth2/response.gleam", 39).
?DOC(" Parses a token response and returns the access and refresh token if valid response, otherwise the error response.\n").
-spec parse_token_response(gleam@http@response:response(binary())) -> {ok,
access_token_response()} |
{error, response_error()}.
parse_token_response(Response) ->
case erlang:element(2, Response) of
200 ->
parse_token_success_response(Response);
_ ->
_pipe = parse_error_response(Response),
{error, _pipe}
end.
-file("src/flwr_oauth2/response.gleam", 114).
?DOC(" Creates a String representation of a token request\n").
-spec to_string(access_token_response()) -> binary().
to_string(Resp) ->
<<<<<<<<<<<<<<<<<<<<"AccessTokenResponse(
access_token: "/utf8,
(erlang:element(2, Resp))/binary>>/binary,
",
token_type: "/utf8>>/binary,
(erlang:element(3, Resp))/binary>>/binary,
",
expires_in: "/utf8>>/binary,
(gleam@option:unwrap(
gleam@option:map(
erlang:element(4, Resp),
fun erlang:integer_to_binary/1
),
<<"None"/utf8>>
))/binary>>/binary,
"),
refresh_token: "/utf8>>/binary,
(gleam@option:unwrap(
erlang:element(5, Resp),
<<"None"/utf8>>
))/binary>>/binary,
",
scope: ["/utf8>>/binary,
(gleam@string:join(erlang:element(6, Resp), <<", "/utf8>>))/binary>>/binary,
"],
)"/utf8>>.