Refactoring client to not parse response body automatically

parent 8538066e
...@@ -4,10 +4,8 @@ module Github ...@@ -4,10 +4,8 @@ module Github
def initialize(token) def initialize(token)
@connection = Faraday.new(url: 'https://api.github.com') do |faraday| @connection = Faraday.new(url: 'https://api.github.com') do |faraday|
faraday.adapter :net_http_persistent
faraday.response :json, content_type: /\bjson$/
faraday.authorization 'token', token faraday.authorization 'token', token
faraday.response :logger faraday.adapter :net_http
end end
end end
...@@ -15,7 +13,8 @@ module Github ...@@ -15,7 +13,8 @@ module Github
rate_limit = RateLimit.new(connection) rate_limit = RateLimit.new(connection)
sleep rate_limit.reset_in if rate_limit.exceed? sleep rate_limit.reset_in if rate_limit.exceed?
Github::Response.new(connection.get(url, query)) response = connection.get(url, query)
Github::Response.new(response.headers, response.body, response.status)
end end
end end
end end
...@@ -16,11 +16,11 @@ module Github ...@@ -16,11 +16,11 @@ module Github
end end
def remaining def remaining
@remaining ||= response.body.dig('rate', 'remaining').to_i @remaining ||= body.dig('rate', 'remaining').to_i
end end
def reset_in def reset_in
@reset ||= response.body.dig('rate', 'reset').to_i @reset ||= body.dig('rate', 'reset').to_i
end end
private private
...@@ -30,7 +30,11 @@ module Github ...@@ -30,7 +30,11 @@ module Github
end end
def response def response
@response ||= connection.get(rate_limit_url) connection.get(rate_limit_url)
end
def body
@body ||= Oj.load(response.body, class_cache: false, mode: :compat)
end end
# GitHub Rate Limit API returns 404 when the rate limit is disabled # GitHub Rate Limit API returns 404 when the rate limit is disabled
......
module Github module Github
class Response class Response
attr_reader :raw, :headers, :body, :status attr_reader :headers, :body, :status
def initialize(response) def initialize(headers, body, status)
@raw = response @headers = headers
@headers = response.headers @body = Oj.load(body, class_cache: false, mode: :compat)
@body = response.body @status = status
@status = response.status
end end
def rels def rels
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment