42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/scribesend.rb', line 42
def self.request(method, url, api_key, params={}, ={}, api_base_url=nil)
api_base_url = api_base_url || @api_base
unless api_key ||= @api_key
raise Error.new("No API key provided. " \
"Set your API key using 'Scribesend.api_key = <API-KEY>'.")
end
params = Util.objects_to_ids(params)
url = api_url(url, api_base_url)
case method.to_s.downcase.to_sym
when :get, :head, :delete
url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
payload = nil
else
if [:content_type] && ([:content_type] == "multipart/form-data" || [:content_type] == "application/json")
payload = params
else
payload = uri_encode(params)
end
end
if [:content_type] && [:content_type] == "application/json"
content_type = [:content_type]
else
content_type = "application/x-www-form-urlencoded"
end
= {
:user_agent => "Scribesend/v0 RubyClient/#{Scribesend::VERSION}",
:authorization => "Bearer #{api_key}",
:content_type => content_type
}.update()
request_opts = {
:method => method,
:headers => ,
:url => url,
:payload => payload,
:verify_ssl => false,
:open_timeout => 30,
:timeout => 80,
}
begin
response = execute_request(request_opts)
rescue SocketError => e
handle_restclient_error(e, api_base_url)
rescue NoMethodError => e
if e.message =~ /\WRequestFailed\W/
e = Error.new('Unexpected HTTP response code')
handle_restclient_error(e, api_base_url)
else
raise
end
rescue RestClient::ExceptionWithResponse => e
if rcode = e.http_code and rbody = e.http_body
handle_api_error(rcode, rbody)
else
handle_restclient_error(e, api_base_url)
end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
handle_restclient_error(e, api_base_url)
end
[parse(response), api_key]
end
|