Use make_client instead of calling HTTP::Client

Using `make_client` to create `HTTP::Client`, allows for a simple way to
easily add logic to all `HTTP::Client` initialized within Invidious.
This commit is contained in:
syeopite 2024-05-22 11:29:28 -07:00
parent c5fdd9ea65
commit 1124dd645d
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
2 changed files with 5 additions and 12 deletions

View File

@ -31,9 +31,7 @@ module Invidious::Routes::API::V1::Search
query = env.params.query["q"]? || "" query = env.params.query["q"]? || ""
begin begin
client = HTTP::Client.new("suggestqueries-clients6.youtube.com") client = make_client(URI.parse("https://suggestqueries-clients6.youtube.com"), force_youtube_headers = true)
client.before_request { |r| add_yt_headers(r) }
url = "/complete/search?client=youtube&hl=en&gl=#{region}&q=#{URI.encode_www_form(query)}&gs_ri=youtube&ds=yt" url = "/complete/search?client=youtube&hl=en&gl=#{region}&q=#{URI.encode_www_form(query)}&gs_ri=youtube&ds=yt"
response = client.get(url).body response = client.get(url).body

View File

@ -30,11 +30,8 @@ struct YoutubeConnectionPool
response = yield conn response = yield conn
rescue ex rescue ex
conn.close conn.close
conn = HTTP::Client.new(url) conn = make_client(url)
conn.family = CONFIG.force_resolve
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
response = yield conn response = yield conn
ensure ensure
pool.release(conn) pool.release(conn)
@ -45,16 +42,14 @@ struct YoutubeConnectionPool
private def build_pool private def build_pool
DB::Pool(HTTP::Client).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do DB::Pool(HTTP::Client).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do
conn = HTTP::Client.new(url) conn = make_client(url, force_solve = true)
conn.family = CONFIG.force_resolve
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
conn conn
end end
end end
end end
def make_client(url : URI, region = nil, force_resolve : Bool = false) def make_client(url : URI, region = nil, force_resolve : Bool = false, force_youtube_header : Bool = false)
client = HTTP::Client.new(url) client = HTTP::Client.new(url)
# Force the usage of a specific configured IP Family # Force the usage of a specific configured IP Family
@ -62,7 +57,7 @@ def make_client(url : URI, region = nil, force_resolve : Bool = false)
client.family = CONFIG.force_resolve client.family = CONFIG.force_resolve
end end
client.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com" client.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com" || force_youtube_header
client.read_timeout = 10.seconds client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds client.connect_timeout = 10.seconds