Skip to content

Commit

Permalink
Changed HTTP Client deps to use Req
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapasquale committed May 15, 2024
1 parent d9183d5 commit 08576ad
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 113 deletions.
101 changes: 59 additions & 42 deletions lib/botchini/services/twitch/twitch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@ defmodule Botchini.Services.Twitch do
Handles communication with Twitch API
"""

# use Tesla

alias Botchini.Services.Twitch.AuthMiddleware
alias Botchini.Services.Twitch.Structs.{Channel, Stream, User}

# plug(Tesla.Middleware.JSON)
# plug(Tesla.Middleware.Logger)
# plug(Botchini.Services.Twitch.AuthMiddleware)
# plug(Tesla.Middleware.BaseUrl, "https://api.twitch.tv/helix")

# plug(Tesla.Middleware.Headers, [
# {"Client-ID", Application.fetch_env!(:botchini, :twitch_client_id)}
# ])

@spec search_channels(String.t()) :: list(Channel.t())
def search_channels(term) do
resp = api() |> Req.get!(url: "/search/channels", params: [query: term, first: 10])
resp =
Req.get!(
api(),
url: "/search/channels",
params: [query: term, first: 10]
)

resp.body
|> Map.get("data")
Expand All @@ -28,7 +22,12 @@ defmodule Botchini.Services.Twitch do

@spec get_user(String.t()) :: User.t() | nil
def get_user(user_id) do
resp = api() |> Req.get!(url: "/users", params: [id: user_id])
resp =
Req.get!(
api(),
url: "/users",
params: [id: user_id]
)

user =
resp.body
Expand All @@ -40,50 +39,68 @@ defmodule Botchini.Services.Twitch do

@spec get_user_by_user_login(String.t()) :: User.t() | nil
def get_user_by_user_login(user_login) do
# {:ok, %{body: body}} = get("/users", query: [login: String.downcase(user_login)])
resp =
Req.get!(
api(),
url: "/users",
params: [login: String.downcase(user_login)]
)

# user =
# body
# |> Map.get("data")
# |> List.first()
user =
resp.body
|> Map.get("data")
|> List.first()

# if user != nil, do: User.new(user), else: nil
if user != nil, do: User.new(user), else: nil
end

@spec get_stream(String.t()) :: Stream.t() | nil
def get_stream(user_id) do
# {:ok, %{body: body}} = get("/streams", query: [user_id: user_id])

# stream =
# body
# |> Map.get("data")
# |> List.first()
resp =
Req.get!(
api(),
url: "/streams",
params: [user_id: user_id]
)

stream =
resp.body
|> Map.get("data")
|> List.first()

# if stream != nil, do: Stream.new(stream), else: nil
if stream != nil, do: Stream.new(stream), else: nil
end

@spec add_stream_webhook(String.t()) :: any()
def add_stream_webhook(user_id) do
# {:ok, %{body: body}} =
# post("/eventsub/subscriptions", %{
# type: "stream.online",
# version: "1",
# condition: %{broadcaster_user_id: user_id},
# transport: %{
# method: "webhook",
# callback: Application.fetch_env!(:botchini, :host) <> "/api/twitch/webhooks/callback",
# secret: Application.fetch_env!(:botchini, :twitch_webhook_secret)
# }
# })

# body
# |> Map.get("data")
# |> List.first()
resp =
Req.post!(
api(),
url: "/eventsub/subscriptions",
json: %{
type: "stream.online",
version: "1",
condition: %{broadcaster_user_id: user_id},
transport: %{
method: "webhook",
callback: Application.fetch_env!(:botchini, :host) <> "/api/twitch/webhooks/callback",
secret: Application.fetch_env!(:botchini, :twitch_webhook_secret)
}
}
)

resp.body
|> Map.get("data")
|> List.first()
end

@spec delete_stream_webhook(String.t()) :: any()
def delete_stream_webhook(subscription_id) do
# delete("/eventsub/subscriptions", query: [id: subscription_id])
Req.delete!(
api(),
url: "/eventsub/subscriptions",
params: [id: subscription_id]
)
end

defp api() do

Check warning on line 106 in lib/botchini/services/twitch/twitch.ex

View workflow job for this annotation

GitHub Actions / Test on OTP 26.2.1 / Elixir 1.16.1

Do not use parentheses when defining a function which has no arguments.
Expand Down
135 changes: 68 additions & 67 deletions lib/botchini/services/youtube/youtube.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,85 @@ defmodule Botchini.Services.Youtube do
Handles communication with YouTube API
"""

# use Tesla
# alias Tesla.Multipart
require Logger

alias Botchini.Services.Youtube.Structs.{Channel, Video}

# plug(Tesla.Middleware.JSON)
# plug(Tesla.Middleware.Logger)

# plug(Tesla.Middleware.Query,
# key: Application.fetch_env!(:botchini, :youtube_api_key)
# )

@youtube_api "https://www.googleapis.com/youtube/v3"

@spec search_channels(String.t()) :: list(Channel.t())
def search_channels(term) do
# {:ok, %{body: body}} =
# get("#{@youtube_api}/search",
# query: [part: "snippet", type: "channel", q: term]
# )

# case Map.get(body, "items") do
# nil ->
# []

# items ->
# Enum.map(items, fn item ->
# Channel.new(%{id: item["id"]["channelId"], snippet: item["snippet"]})
# end)
# end
resp =
Req.get!(
api(),
url: "/search",
params: [part: "snippet", type: "channel", q: term]
)

case Map.get(resp.body, "items") do
nil ->
[]

items ->
Enum.map(items, fn item ->
Channel.new(%{id: item["id"]["channelId"], snippet: item["snippet"]})
end)
end
end

@spec get_channel(String.t()) :: Channel.t() | nil
def get_channel(channel_id) do
# {:ok, %{body: body}} =
# get("#{@youtube_api}/channels",
# query: [part: "snippet", id: channel_id]
# )

# case Map.get(body, "items") do
# nil ->
# nil

# items ->
# List.first(items)
# |> Channel.new()
# end
resp =
Req.get!(
api(),
url: "/channels",
params: [part: "snippet", id: channel_id]
)

case Map.get(resp.body, "items") do
nil ->
nil

items ->
List.first(items)
|> Channel.new()
end
end

@spec get_video(String.t()) :: Video.t() | nil
def get_video(video_id) do
# {:ok, %{body: body}} =
# get("https://www.googleapis.com/youtube/v3/videos",
# query: [part: "snippet,liveStreamingDetails", id: video_id]
# )

# case Map.get(body, "items") do
# nil ->
# nil

# items ->
# List.first(items)
# |> Video.new()
# end
resp =
Req.get!(
api(),
url: "/videos",
params: [part: "snippet,liveStreamingDetails", id: video_id]
)

case Map.get(resp.body, "items") do
nil ->
nil

items ->
List.first(items)
|> Video.new()
end
end

@spec manage_channel_pubsub(String.t(), boolean()) :: {:ok}
def manage_channel_pubsub(channel_id, subscribe) do
# callback_url = "#{Application.fetch_env!(:botchini, :host)}/api/youtube/webhooks/callback"
# topic_url = "https://www.youtube.com/xml/feeds/videos.xml?channel_id=#{channel_id}"

# mp =
# Multipart.new()
# |> Multipart.add_field("hub.callback", callback_url)
# |> Multipart.add_field("hub.topic", topic_url)
# |> Multipart.add_field("hub.verify", "async")
# |> Multipart.add_field("hub.mode", if(subscribe, do: "subscribe", else: "unsubscribe"))
# |> Multipart.add_field(
# "hub.secret",
# Application.fetch_env!(:botchini, :youtube_webhook_secret)
# )

# {:ok, _} = post("https://pubsubhubbub.appspot.com/subscribe", mp)
# {:ok}
callback_url = "#{Application.fetch_env!(:botchini, :host)}/api/youtube/webhooks/callback"
topic_url = "https://www.youtube.com/xml/feeds/videos.xml?channel_id=#{channel_id}"

Req.post!(
url: "https://pubsubhubbub.appspot.com/subscribe",
form: [
"hub.verify": "async",
"hub.mode": if(subscribe, do: "subscribe", else: "unsubscribe"),
"hub.callback": callback_url,
"hub.topic": topic_url,
"hub.secret": Application.fetch_env!(:botchini, :youtube_webhook_secret)
]
)

{:ok}
end

@spec get_video_id_from_url(String.t()) :: String.t()
Expand All @@ -102,4 +96,11 @@ defmodule Botchini.Services.Youtube do
List.last(match)
end
end

defp api() do

Check warning on line 100 in lib/botchini/services/youtube/youtube.ex

View workflow job for this annotation

GitHub Actions / Test on OTP 26.2.1 / Elixir 1.16.1

Do not use parentheses when defining a function which has no arguments.
Req.new(
base_url: "https://www.googleapis.com/youtube/v3",
params: [key: Application.fetch_env!(:botchini, :youtube_api_key)]
)
end
end
5 changes: 1 addition & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Botchini.MixProject do
def project do
[
app: :botchini,
version: "8.9.1",
version: "8.9.2",
elixir: "~> 1.16.1",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
Expand Down Expand Up @@ -51,9 +51,6 @@ defmodule Botchini.MixProject do
# HTTP Client
{:req, "~> 0.4.0"},
{:exconstructor, "~> 1.2.13"},
# {:tesla, "~> 1.9.0"},
# {:gun, "~> 2.1.0"},
# {:hackney, "~> 1.20.1"},
# Helpers
{:ink, "~> 1.0"},
{:quantum, "~> 3.0"},
Expand Down

0 comments on commit 08576ad

Please sign in to comment.