531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
# File 'lib/bugly.rb', line 531
def self.request(method, url, api_key, params=nil, ={})
api_key ||= @@api_key
raise AuthenticationError.new('No API key provided. (HINT: set your API key using "Bugly.api_key = <API-KEY>". You can generate API keys from the Bugly web interface. See http://bug.ly/docs/api for details, or email [email protected] if you have any questions.)') unless api_key
raise APIConnectionError.new('No API base URL set. (HINT: set your API base URL using "Bugly.api_base = <API-URL>". Use the full URL to your account, as well as a version string, for example "https://myaccount.bug.ly/v1". See http://bug.ly/docs/api for details, or email [email protected] if you have any questions.)') unless @@api_base
if !verify_ssl_certs
unless @no_verify
$stderr.puts "WARNING: Running without SSL cert verification. Execute 'Bugly.verify_ssl_certs = true' to enable verification."
@no_verify = true
end
ssl_opts = { :verify_ssl => false }
elsif !Util.file_readable(@@ssl_bundle_path)
unless @no_bundle
$stderr.puts "WARNING: Running without SSL cert verification because #{@@ssl_bundle_path} isn't readable"
@no_bundle = true
end
ssl_opts = { :verify_ssl => false }
else
ssl_opts = {
:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
:ssl_ca_file => @@ssl_bundle_path
}
end
uname = (@@uname ||= RUBY_PLATFORM =~ /linux|darwin/i ? `uname -a 2>/dev/null`.strip : nil)
lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
ua = {
:bindings_version => Bugly::VERSION,
:lang => 'ruby',
:lang_version => lang_version,
:platform => RUBY_PLATFORM,
:publisher => 'bugly',
:uname => uname
}
params = Util.objects_to_ids(params)
case method.to_s.downcase.to_sym
when :get, :head, :delete
= { :params => params }.merge()
payload = nil
else
payload = params
end
begin
= { :x_bugly_client_user_agent => JSON.dump(ua) }.merge()
rescue => e
= {
:x_bugly_client_raw_user_agent => ua.inspect,
:error => "#{e} (#{e.class})"
}.merge()
end
= {
:user_agent => "Bugly/v1 RubyBindings/#{Bugly::VERSION}",
"X-BuglyToken" => api_key,
:accept => "application/json"
}.merge()
opts = {
:method => method,
:url => self.api_url(url),
:headers => ,
:open_timeout => 30,
:payload => payload,
:timeout => 80
}.merge(ssl_opts)
begin
response = execute_request(opts)
rescue SocketError => e
self.handle_restclient_error(e)
rescue NoMethodError => e
if e.message =~ /\WRequestFailed\W/
e = APIConnectionError.new('Unexpected HTTP response code')
self.handle_restclient_error(e)
else
raise
end
rescue RestClient::ExceptionWithResponse => e
if rcode = e.http_code and rbody = e.http_body
self.handle_api_error(rcode, rbody)
else
self.handle_restclient_error(e)
end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
self.handle_restclient_error(e)
end
rbody = response.body
rcode = response.code
begin
resp = JSON.parse(rbody)
rescue JSON::ParserError
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
end
resp = Util.symbolize_names(resp)
[resp, api_key]
end
|