17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/cover_my_meds/api_request.rb', line 17
def request(http_method, host, path, params={}, auth_type = :basic, &block)
params = params.symbolize_keys
= params.delete(:headers) || {}
conn = Faraday.new host do |faraday|
faraday.request :multipart
faraday.request :url_encoded
faraday.response :follow_redirects
faraday.adapter :typhoeus
end
case auth_type
when :basic
conn.basic_auth @username, @password
when :bearer
conn.authorization :Bearer, "#{@username}+#{params.delete(:token_id)}"
end
response = conn.send http_method do |request|
request.url path
request.options.timeout = DEFAULT_TIMEOUT
request.params = params
request..merge!
request.body = block_given? ? yield : {}
end
raise Error::HTTPError.new(response.status, response.body, http_method, conn) unless response.success?
parse_response response
end
|