Packages

Official Gleam SDK for atradio.fm, over the atradio_erl NIF package

Current section

Files

Jump to
atradio_gleam src atradio_gleam.erl
Raw

src/atradio_gleam.erl

-module(atradio_gleam).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atradio_gleam.gleam").
-export([recent_stations/1, popular_stations/1, global_recently_played/1, favorites/2, favorite_rkey/1, login/4, favorite/2, unfavorite/2, set_play_status/2, comment/3]).
-export_type([station/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.
?MODULEDOC(
" Official Gleam SDK for atradio.fm.\n"
"\n"
" Typed bindings over the `atradio_erl` NIF package (the shared Rust core),\n"
" so the auth / record / reconcile logic is identical to the Rust, Go,\n"
" TypeScript, Python, Ruby, Clojure, and Erlang SDKs. `atradio_erl` downloads\n"
" the matching native library from the GitHub release on first load.\n"
).
-type station() :: {station, binary(), binary(), binary(), binary()}.
-file("src/atradio_gleam.gleam", 36).
?DOC(" The station nested inside a StationView / PlayView / PopularItem.\n").
-spec station_decoder() -> gleam@dynamic@decode:decoder(station()).
station_decoder() ->
gleam@dynamic@decode:field(
<<"stationId"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Name) ->
gleam@dynamic@decode:field(
<<"streamUrl"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Stream_url) ->
gleam@dynamic@decode:optional_field(
<<"source"/utf8>>,
<<""/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Source) ->
gleam@dynamic@decode:success(
{station, Id, Name, Stream_url, Source}
)
end
)
end
)
end
)
end
).
-file("src/atradio_gleam.gleam", 44).
-spec view_decoder() -> gleam@dynamic@decode:decoder(station()).
view_decoder() ->
gleam@dynamic@decode:at([<<"station"/utf8>>], station_decoder()).
-file("src/atradio_gleam.gleam", 48).
-spec decode_list(gleam@dynamic:dynamic_()) -> list(station()).
decode_list(Dyn) ->
case gleam@dynamic@decode:run(
Dyn,
gleam@dynamic@decode:list(view_decoder())
) of
{ok, List} ->
List;
{error, _} ->
[]
end.
-file("src/atradio_gleam.gleam", 56).
?DOC(" Newest stations platform-wide.\n").
-spec recent_stations(integer()) -> list(station()).
recent_stations(Limit) ->
decode_list(atradio:recent_stations(Limit, <<""/utf8>>)).
-file("src/atradio_gleam.gleam", 61).
?DOC(" Most-favorited stations platform-wide.\n").
-spec popular_stations(integer()) -> list(station()).
popular_stations(Limit) ->
decode_list(atradio:popular_stations(Limit, <<""/utf8>>)).
-file("src/atradio_gleam.gleam", 66).
?DOC(" Platform-wide who's-listening feed.\n").
-spec global_recently_played(integer()) -> list(station()).
global_recently_played(Limit) ->
decode_list(atradio:global_recently_played(Limit, <<""/utf8>>)).
-file("src/atradio_gleam.gleam", 71).
?DOC(" An actor's favorited stations.\n").
-spec favorites(binary(), integer()) -> list(station()).
favorites(Actor, Limit) ->
Decoder = gleam@dynamic@decode:at(
[<<"items"/utf8>>],
gleam@dynamic@decode:list(view_decoder())
),
case gleam@dynamic@decode:run(
atradio:favorites(Actor, Limit, <<""/utf8>>),
Decoder
) of
{ok, List} ->
List;
{error, _} ->
[]
end.
-file("src/atradio_gleam.gleam", 82).
?DOC(" The deterministic favorite record key — identical across every atradio SDK.\n").
-spec favorite_rkey(binary()) -> binary().
favorite_rkey(Station_id) ->
atradio:favorite_rkey(Station_id).
-file("src/atradio_gleam.gleam", 88).
?DOC(" Log in with an app password, persisting the session at `session_path`.\n").
-spec login(binary(), binary(), binary(), binary()) -> gleam@dynamic:dynamic_().
login(Session_path, Identifier, Password, Appview) ->
atradio:login(Session_path, Identifier, Password, Appview).
-file("src/atradio_gleam.gleam", 95).
-spec station_to_map(station()) -> gleam@dict:dict(binary(), binary()).
station_to_map(S) ->
maps:from_list(
[{<<"stationId"/utf8>>, erlang:element(2, S)},
{<<"name"/utf8>>, erlang:element(3, S)},
{<<"streamUrl"/utf8>>, erlang:element(4, S)},
{<<"source"/utf8>>, erlang:element(5, S)}]
).
-file("src/atradio_gleam.gleam", 117).
?DOC(" Favorite a station (idempotent; deterministic record key).\n").
-spec favorite(gleam@dynamic:dynamic_(), station()) -> nil.
favorite(Agent, Station) ->
atradio:favorite(Agent, station_to_map(Station)),
nil.
-file("src/atradio_gleam.gleam", 123).
?DOC(" Unfavorite a station (removes every record for its stationId).\n").
-spec unfavorite(gleam@dynamic:dynamic_(), station()) -> nil.
unfavorite(Agent, Station) ->
atradio:unfavorite(Agent, station_to_map(Station)),
nil.
-file("src/atradio_gleam.gleam", 129).
?DOC(" Update the actor's play-status singleton.\n").
-spec set_play_status(gleam@dynamic:dynamic_(), station()) -> nil.
set_play_status(Agent, Station) ->
atradio:set_play_status(Agent, station_to_map(Station)),
nil.
-file("src/atradio_gleam.gleam", 135).
?DOC(" Post a comment on a station.\n").
-spec comment(gleam@dynamic:dynamic_(), station(), binary()) -> nil.
comment(Agent, Station, Text) ->
atradio:comment(Agent, station_to_map(Station), Text),
nil.