From 9d2932afdb552c051c1bb5c4a12b811e0e012c2f Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 26 Jul 2021 20:23:15 +0200 Subject: [PATCH] YoutubeAPI: use proxy_region from client_config And accept gzip compressed data, to help save on bandwidth --- src/invidious/helpers/youtube_api.cr | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/invidious/helpers/youtube_api.cr b/src/invidious/helpers/youtube_api.cr index 2ce63d13..fb5e0143 100644 --- a/src/invidious/helpers/youtube_api.cr +++ b/src/invidious/helpers/youtube_api.cr @@ -333,13 +333,27 @@ module YoutubeAPI # Use the default client config if nil is passed client_config ||= DEFAULT_CLIENT_CONFIG - # Send the POST request and parse result - response = YT_POOL.client &.post( - "#{endpoint}?key=#{client_config.api_key}", - headers: HTTP::Headers{"content-type" => "application/json; charset=UTF-8"}, - body: data.to_json - ) + # Query parameters + url = "#{endpoint}?key=#{client_config.api_key}" + headers = HTTP::Headers{ + "Content-Type" => "application/json; charset=UTF-8", + "Accept-Encoding" => "gzip", + } + + # Send the POST request + if client_config.proxy_region + response = YT_POOL.client( + client_config.proxy_region, + &.post(url, headers: headers, body: data.to_json) + ) + else + response = YT_POOL.client &.post( + url, headers: headers, body: data.to_json + ) + end + + # Convert result to Hash initial_data = JSON.parse(response.body).as_h # Error handling