lib/lifen/client.rb in lifen-2.1.0 vs lib/lifen/client.rb in lifen-2.2.0
- old
+ new
@@ -6,21 +6,22 @@
response = faraday_client.send(mode) do |req|
req.url url
req.headers['Authorization'] = "Bearer #{bearer}"
- req.headers['Content-Type'] = "application/json"
- req.headers['Accept'] = "application/json"
+ req.headers['Accept'] = use_and_remove_accept(params)
+ if mode == :post
+ req.headers['Content-Type'] = "application/json"
+ end
+
req.body = JSON.generate(params)
end
handle_errors(response, params)
- json = JSON.parse response.body
-
- json
+ handle_response(response)
end
def post(url, params = {})
request(:post, url, params)
end
@@ -44,10 +45,18 @@
raise Error, "Error 500, Internal server error (trace ID: #{trace_id}), #{response_error(response, params)}"
end
end
+ def handle_response(response)
+ if response.headers['Content-Type'].match "json"
+ JSON.parse response.body
+ else
+ response.body
+ end
+ end
+
def faraday_client
@faraday_client ||= Faraday.new(faraday_options) do |faraday|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
end
@@ -87,7 +96,12 @@
def bearer
raise "A bearer method must be defined"
end
+ def use_and_remove_accept(params)
+ return params.delete :accept if params.is_a?(Hash) and params.has_key? :accept
+
+ "application/json"
+ end
end
-end
\ No newline at end of file
+end