sources: replace HttpartyCache with Danbooru::Http.

This commit is contained in:
evazion
2019-12-15 15:40:30 -06:00
parent 0adec60259
commit 41378bc8e3
7 changed files with 35 additions and 56 deletions

View File

@@ -1,12 +0,0 @@
module HttpartyCache
extend self
def get(url, headers: {}, params: {}, base_uri: nil, format: :html, expiry: 60)
key = Cache.hash({url: url, headers: headers, params: params, base_uri: base_uri, format: format}.to_s)
Cache.get("cachedget:#{key}", expiry) do
resp = HTTParty.get(url, Danbooru.config.httparty_options.deep_merge(query: params, headers: headers, base_uri: base_uri, format: format))
body = resp.body.force_encoding("utf-8")
[body, resp.code]
end
end
end

View File

@@ -65,24 +65,19 @@ class NicoSeigaApiClient
end
def illust_xml
uri = "#{BASE_URL}/illust/info?id=#{illust_id}"
body, code = HttpartyCache.get(uri)
if code == 200
Hash.from_xml(body)
else
raise "nico seiga api call failed (code=#{code}, body=#{body})"
end
get("#{BASE_URL}/illust/info?id=#{illust_id}")
end
memoize :illust_xml
def artist_xml
uri = "#{BASE_URL}/user/info?id=#{user_id}"
body, code = HttpartyCache.get(uri)
if code == 200
Hash.from_xml(body)
else
raise "nico seiga api call failed (code=#{code}, body=#{body})"
end
get("#{BASE_URL}/user/info?id=#{user_id}")
end
memoize :artist_xml
def get(url)
response = Danbooru::Http.cache(1.minute).get(url)
raise "nico seiga api call failed (code=#{response.code}, body=#{response.body.to_s})" if response.code != 200
Hash.from_xml(response.to_s)
end
memoize :artist_xml, :illust_xml
end

View File

@@ -38,23 +38,23 @@ class NicoSeigaMangaApiClient
body = NicoSeigaApiClient.agent.get(uri).body
Hash.from_xml(body)
end
memoize :theme_data_xml
def theme_info_xml
uri = "#{BASE_URL}/theme/info?id=#{theme_id}"
body = NicoSeigaApiClient.agent.get(uri).body
Hash.from_xml(body)
end
memoize :theme_info_xml
def artist_xml
uri = "#{BASE_URL}/user/info?id=#{user_id}"
body, code = HttpartyCache.get(uri)
if code == 200
Hash.from_xml(body)
else
raise "nico seiga api call failed (code=#{code}, body=#{body})"
end
get("#{BASE_URL}/user/info?id=#{user_id}")
end
memoize :artist_xml
def get(url)
response = Danbooru::Http.cache(1.minute).get(url)
raise "nico seiga api call failed (code=#{response.code}, body=#{response.body.to_s})" if response.code != 200
Hash.from_xml(response.to_s)
end
memoize :theme_data_xml, :theme_info_xml, :artist_xml
end

View File

@@ -169,19 +169,19 @@ class PixivApiClient
}
url = "https://public-api.secure.pixiv.net/v#{API_VERSION}/works/#{illust_id.to_i}.json"
body, code = HttpartyCache.get(url, headers: headers, params: params)
json = JSON.parse(body)
response = Danbooru::Http.cache(1.minute).headers(headers).get(url, params: params)
json = response.parse
if code == 200
if response.code == 200
WorkResponse.new(json["response"][0])
elsif json["status"] == "failure" && json.dig("errors", "system", "message") =~ /対象のイラストは見つかりませんでした。/
raise BadIDError.new("Pixiv ##{illust_id} not found: work was deleted, made private, or ID is invalid.")
else
raise Error.new("Pixiv API call failed (status=#{code} body=#{body})")
raise Error.new("Pixiv API call failed (status=#{response.code} body=#{response.body.to_s})")
end
rescue JSON::ParserError
raise Error.new("Pixiv API call failed (status=#{code} body=#{body})")
raise Error.new("Pixiv API call failed (status=#{response.code} body=#{response.body.to_s})")
end
def fanbox(fanbox_id)

View File

@@ -110,10 +110,10 @@ module Sources::Strategies
def api_response
return {} unless project_id.present?
resp, code = HttpartyCache.get("https://www.artstation.com/projects/#{project_id}.json")
return {} if code != 200
resp = Danbooru::Http.cache(1.minute).get("https://www.artstation.com/projects/#{project_id}.json")
return {} if resp.code != 200
JSON.parse(resp, symbolize_names: true)
resp.parse.with_indifferent_access
end
memoize :api_response

View File

@@ -101,8 +101,8 @@ module Sources
return {}
end
body, code = HttpartyCache.get("/post.json", base_uri: "https://#{site_name}", params: params)
post = JSON.parse(body, symbolize_names: true).first
response = Danbooru::Http.cache(1.minute).get("https://#{site_name}/post.json", params: params)
post = response.parse.first&.with_indifferent_access
post || {}
end
memoize :api_response

View File

@@ -166,17 +166,13 @@ module Sources::Strategies
return {} unless self.class.enabled?
return {} unless blog_name.present? && post_id.present?
body, code = HttpartyCache.get("/#{blog_name}/posts",
response = Danbooru::Http.cache(1.minute).get(
"https://api.tumblr.com/v2/blog/#{blog_name}/posts",
params: { id: post_id, api_key: Danbooru.config.tumblr_consumer_key },
base_uri: "https://api.tumblr.com/v2/blog/"
)
if code == 200
return JSON.parse(body, symbolize_names: true)
else
Rails.logger.debug("TumblrApiClient call failed (code=#{code}, body=#{body}, blog_name=#{blog_name}, post_id=#{post_id})")
return {}
end
return {} if response.code != 200
response.parse.with_indifferent_access
end
memoize :api_response