http: split requests into internal and external requests.

Split requests made by Danbooru::Http into either internal or external
requests. Internal requests are API calls to internal services run by
Danbooru. External requests are requests to external websites, for
example fetching sources or downloading files. External requests may use
a HTTP proxy if one is configured. Internal requests don't.

Fixes a few source extractors not using the HTTP proxy for certain API calls.
This commit is contained in:
evazion
2022-10-18 22:38:19 -05:00
parent 4001701d18
commit 412b7f2727
17 changed files with 38 additions and 29 deletions

View File

@@ -9,7 +9,7 @@ class AuthorizeNetClient
attr_reader :login_id, :transaction_key, :test_mode, :http attr_reader :login_id, :transaction_key, :test_mode, :http
def initialize(login_id: Danbooru.config.authorize_net_login_id, transaction_key: Danbooru.config.authorize_net_transaction_key, test_mode: Danbooru.config.authorize_net_test_mode, http: Danbooru::Http.new) def initialize(login_id: Danbooru.config.authorize_net_login_id, transaction_key: Danbooru.config.authorize_net_transaction_key, test_mode: Danbooru.config.authorize_net_test_mode, http: Danbooru::Http.external)
@login_id = login_id @login_id = login_id
@transaction_key = transaction_key @transaction_key = transaction_key
@test_mode = test_mode @test_mode = test_mode

View File

@@ -7,7 +7,7 @@
class AutotaggerClient class AutotaggerClient
attr_reader :autotagger_url, :http attr_reader :autotagger_url, :http
def initialize(autotagger_url: Danbooru.config.autotagger_url.to_s, http: Danbooru::Http.new) def initialize(autotagger_url: Danbooru.config.autotagger_url.to_s, http: Danbooru::Http.internal)
@autotagger_url = autotagger_url.chomp("/") @autotagger_url = autotagger_url.chomp("/")
@http = http @http = http
end end

View File

@@ -40,21 +40,33 @@ module Danbooru
attr_accessor :max_size, :http attr_accessor :max_size, :http
class << self class << self
delegate :get, :head, :put, :post, :delete, :cache, :follow, :max_size, :timeout, :auth, :basic_auth, :headers, :cookies, :use, :public_only, :with_legacy_ssl, :download_media, to: :new delegate :get, :head, :put, :post, :delete, :cache, :follow, :max_size, :timeout, :auth, :basic_auth, :headers, :cookies, :use, :proxy, :public_only, :with_legacy_ssl, :download_media, to: :new
end end
def initialize # The default HTTP client.
@http ||= def self.default
::Danbooru::Http::ApplicationClient.new Danbooru::Http::ApplicationClient.new
.timeout(DEFAULT_TIMEOUT) .timeout(DEFAULT_TIMEOUT)
.headers("Accept-Encoding" => "gzip") .headers("Accept-Encoding": "gzip")
.headers("User-Agent": "#{Danbooru.config.canonical_app_name}/#{Rails.application.config.x.git_hash}")
#.headers("User-Agent": Danbooru.config.canonical_app_name)
.use(:auto_inflate) .use(:auto_inflate)
.use(redirector: { max_redirects: MAX_REDIRECTS }) .use(redirector: { max_redirects: MAX_REDIRECTS })
.use(:session) .use(:session)
end end
# The default HTTP client for requests to external websites. This includes API calls to external services, fetching source data, and downloading images.
def self.external
new.proxy.public_only.headers("User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0")
end
# The default HTTP client for API calls to internal services controlled by Danbooru.
def self.internal
new.headers("User-Agent": "#{Danbooru.config.canonical_app_name}/#{Rails.application.config.x.git_hash}")
end
def initialize
@http ||= Danbooru::Http.default
end
def get(url, **options) def get(url, **options)
request(:get, url, **options) request(:get, url, **options)
end end

View File

@@ -3,7 +3,7 @@
class DeepDanbooruClient class DeepDanbooruClient
attr_reader :http attr_reader :http
def initialize(http: Danbooru::Http.new) def initialize(http: Danbooru::Http.internal)
@http = http @http = http
end end

View File

@@ -21,10 +21,10 @@ class DeviantArtApiClient
class Error < StandardError; end class Error < StandardError; end
BASE_URL = "https://www.deviantart.com/api/v1/oauth2/" BASE_URL = "https://www.deviantart.com/api/v1/oauth2/"
attr_reader :client_id, :client_secret attr_reader :client_id, :client_secret, :http
def initialize(client_id, client_secret) def initialize(client_id, client_secret, http)
@client_id, @client_secret = client_id, client_secret @client_id, @client_secret, @http = client_id, client_secret, http
end end
# https://www.deviantart.com/developers/http/v1/20160316/deviation_single/bcc296bdf3b5e40636825a942a514816 # https://www.deviantart.com/developers/http/v1/20160316/deviation_single/bcc296bdf3b5e40636825a942a514816
@@ -54,7 +54,7 @@ class DeviantArtApiClient
params = { access_token: access_token.token, **params } params = { access_token: access_token.token, **params }
url = URI.join(BASE_URL, url).to_s url = URI.join(BASE_URL, url).to_s
response = Danbooru::Http.cache(1.minute).get(url, params: params) response = http.cache(1.minute).get(url, params: params)
response.parse.with_indifferent_access response.parse.with_indifferent_access
end end

View File

@@ -14,7 +14,7 @@ class DiscordApiClient
attr_reader :application_id, :bot_token, :http attr_reader :application_id, :bot_token, :http
def initialize(application_id: Danbooru.config.discord_application_client_id, bot_token: Danbooru.config.discord_bot_token, http: Danbooru::Http.new) def initialize(application_id: Danbooru.config.discord_application_client_id, bot_token: Danbooru.config.discord_bot_token, http: Danbooru::Http.external)
@application_id = application_id @application_id = application_id
@bot_token = bot_token @bot_token = bot_token
@http = http @http = http

View File

@@ -6,7 +6,7 @@
class DiscordWebhookService class DiscordWebhookService
attr_reader :webhook_id, :webhook_secret, :http attr_reader :webhook_id, :webhook_secret, :http
def initialize(webhook_id: Danbooru.config.discord_webhook_id, webhook_secret: Danbooru.config.discord_webhook_secret, http: Danbooru::Http.new) def initialize(webhook_id: Danbooru.config.discord_webhook_id, webhook_secret: Danbooru.config.discord_webhook_secret, http: Danbooru::Http.external)
@webhook_id = webhook_id @webhook_id = webhook_id
@webhook_secret = webhook_secret @webhook_secret = webhook_secret
@http = http @http = http

View File

@@ -11,7 +11,7 @@ class IqdbClient
# Create a new IQDB API client. # Create a new IQDB API client.
# @param iqdb_url [String] the base URL of the IQDB server # @param iqdb_url [String] the base URL of the IQDB server
# @param http [Danbooru::Http] the HTTP client to use # @param http [Danbooru::Http] the HTTP client to use
def initialize(iqdb_url: Danbooru.config.iqdb_url.to_s, http: Danbooru::Http.new) def initialize(iqdb_url: Danbooru.config.iqdb_url.to_s, http: Danbooru::Http.internal)
@iqdb_url = iqdb_url.chomp("/") @iqdb_url = iqdb_url.chomp("/")
@http = http @http = http
end end

View File

@@ -7,7 +7,7 @@ class NicoSeigaApiClient
attr_reader :http, :user_session attr_reader :http, :user_session
def initialize(work_id:, type:, user_session: Danbooru.config.nico_seiga_user_session, http: Danbooru::Http.new) def initialize(work_id:, type:, user_session: Danbooru.config.nico_seiga_user_session, http:)
@work_id = work_id @work_id = work_id
@work_type = type @work_type = type
@user_session = user_session @user_session = user_session

View File

@@ -365,7 +365,7 @@ class PixivAjaxClient
# @param phpsessid [String] the Pixiv login cookie # @param phpsessid [String] the Pixiv login cookie
# @param http [Danbooru::Http] the HTTP client to use for Pixiv # @param http [Danbooru::Http] the HTTP client to use for Pixiv
def initialize(phpsessid, http: Danbooru::Http.new) def initialize(phpsessid, http:)
@phpsessid = phpsessid @phpsessid = phpsessid
@http = http @http = http
end end

View File

@@ -7,7 +7,7 @@
class ReportbooruService class ReportbooruService
attr_reader :http, :reportbooru_server attr_reader :http, :reportbooru_server
def initialize(http: Danbooru::Http.new, reportbooru_server: Danbooru.config.reportbooru_server) def initialize(http: Danbooru::Http.internal, reportbooru_server: Danbooru.config.reportbooru_server)
@reportbooru_server = reportbooru_server @reportbooru_server = reportbooru_server
@http = http.timeout(1) @http = http.timeout(1)
end end

View File

@@ -196,7 +196,7 @@ module Source
# A http client for API requests. # A http client for API requests.
def http def http
Danbooru::Http.new.proxy.public_only Danbooru::Http.external
end end
# A http client for downloading files. # A http client for downloading files.

View File

@@ -189,10 +189,7 @@ module Source
memoize :uuid memoize :uuid
def api_client def api_client
api_client = DeviantArtApiClient.new( api_client = DeviantArtApiClient.new(Danbooru.config.deviantart_client_id, Danbooru.config.deviantart_client_secret, http)
Danbooru.config.deviantart_client_id,
Danbooru.config.deviantart_client_secret
)
api_client.access_token = Cache.get("da-access-token", 11.weeks) do api_client.access_token = Cache.get("da-access-token", 11.weeks) do
api_client.access_token.to_hash api_client.access_token.to_hash
end end

View File

@@ -157,7 +157,7 @@ class Source::Extractor
end end
def http def http
Danbooru::Http.new.cookies(_session_id: Danbooru.config.fantia_session_id) super.cookies(_session_id: Danbooru.config.fantia_session_id)
end end
end end
end end

View File

@@ -67,7 +67,7 @@ class Source::Extractor
end end
def http def http
Danbooru::Http.new.cookies(a: Danbooru.config.furaffinity_cookie_a, b: Danbooru.config.furaffinity_cookie_b, sfw: 0) super.cookies(a: Danbooru.config.furaffinity_cookie_a, b: Danbooru.config.furaffinity_cookie_b, sfw: 0)
end end
end end
end end

View File

@@ -130,7 +130,7 @@ class Source::Extractor
end end
def api_client def api_client
TwitterApiClient.new(Danbooru.config.twitter_api_key, Danbooru.config.twitter_api_secret) TwitterApiClient.new(Danbooru.config.twitter_api_key, Danbooru.config.twitter_api_secret, http: http)
end end
def api_response def api_response

View File

@@ -10,7 +10,7 @@ class TwitterApiClient
# Create a Twitter API client # Create a Twitter API client
# @param api_key [String] the Twitter API key # @param api_key [String] the Twitter API key
# @param api_secret [String] the Twitter API secret # @param api_secret [String] the Twitter API secret
def initialize(api_key, api_secret, http: Danbooru::Http.new) def initialize(api_key, api_secret, http:)
@api_key, @api_secret, @http = api_key, api_secret, http @api_key, @api_secret, @http = api_key, api_secret, http
end end