Current section
Files
Jump to
Current section
Files
src/glitch@api@eventsub.erl
-module(glitch@api@eventsub).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([create_eventsub_subscription/2]).
-export_type([create_event_subscription_request/0, create_event_sub_subscription_response/0]).
-type create_event_subscription_request() :: {create_event_subscription_request,
glitch@types@subscription:subscription_type(),
binary(),
glitch@types@condition:condition(),
glitch@types@transport:transport()}.
-type create_event_sub_subscription_response() :: {create_event_sub_subscription_response,
binary(),
glitch@types@subscription:subscription_status(),
glitch@types@subscription:subscription_type(),
binary(),
glitch@types@condition:condition(),
binary(),
integer()}.
-spec send_message_request_to_json(create_event_subscription_request()) -> binary().
send_message_request_to_json(Request) ->
_pipe = gleam@json:object(
[{<<"type"/utf8>>,
glitch@types@subscription:subscription_type_to_json(
erlang:element(2, Request)
)},
{<<"version"/utf8>>, gleam@json:string(erlang:element(3, Request))},
{<<"condition"/utf8>>,
glitch@types@condition:to_json(erlang:element(4, Request))},
{<<"transport"/utf8>>,
glitch@types@transport:to_json(erlang:element(5, Request))}]
),
gleam@json:to_string(_pipe).
-spec create_eventsub_subscription_response_decoder() -> fun((gleam@dynamic:dynamic_()) -> {ok,
create_event_sub_subscription_response()} |
{error, list(gleam@dynamic:decode_error())}).
create_eventsub_subscription_response_decoder() ->
gleam@dynamic:decode7(
fun(Field@0, Field@1, Field@2, Field@3, Field@4, Field@5, Field@6) -> {create_event_sub_subscription_response, Field@0, Field@1, Field@2, Field@3, Field@4, Field@5, Field@6} end,
gleam@dynamic:field(<<"id"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(
<<"status"/utf8>>,
glitch@types@subscription:subscription_status_decoder()
),
gleam@dynamic:field(
<<"type"/utf8>>,
glitch@types@subscription:subscription_type_decoder()
),
gleam@dynamic:field(<<"version"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(
<<"condition"/utf8>>,
glitch@types@condition:decoder()
),
gleam@dynamic:field(<<"created_at"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"cost"/utf8>>, fun gleam@dynamic:int/1)
).
-spec create_eventsub_subscription(
glitch@api@client:client(),
create_event_subscription_request()
) -> {ok,
glitch@api@api_response:event_sub_data(list(create_event_sub_subscription_response()))} |
{error, glitch@error:twitch_error()}.
create_eventsub_subscription(Client, Request) ->
Api_req = begin
_pipe = glitch@api@api_request:new_helix_request(),
_pipe@1 = glitch@api@api_request:set_body(
_pipe,
send_message_request_to_json(Request)
),
glitch@api@api_request:set_path(
_pipe@1,
<<"eventsub/subscriptions"/utf8>>
)
end,
gleam@result:'try'(
glitch@api@client:post(Client, Api_req),
fun(Response) ->
glitch@api@api_response:get_eventsub_data(
Response,
create_eventsub_subscription_response_decoder()
)
end
).