39
40
41
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
|
# File 'lib/logcamp.rb', line 39
def self.request(method, url, token, params={}, ={})
unless token ||= @token
raise AuthenticationError.new("No token provided")
end
if !params[:metadata].nil? && params[:metadata].class != Hash
raise InvalidMetadataError.new("Invalid metadata. Only key value pair is supported", params)
else
params[:metadata] = params[:metadata].to_json
end
url = api_url(url)
begin
uri = URI(url)
request = Net::HTTP::Post.new(uri) if method == :post
request["User-Agent"] = "Logcamp-ruby gem"
request["Authorization"] = "Token token=\"#{token}\""
request["Content-Type"] = "application/json"
request.body = params.to_json
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true if uri.scheme == "https"
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if uri.scheme == "https"
response = http.start {|h|
h.request(request)
}
handle_api_error(response.code, response.body)
rescue SocketError => e
handle_connection_error(e)
rescue NoMethodError => e
handle_connection_error(e)
rescue OpenSSL::SSL::SSLError => e
handle_connection_error(e)
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
handle_connection_error(e)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
Net::HTTPBadResponse, Net::, Net::ProtocolError, InvalidMetadataError => e
handle_connection_error(e)
end
[response, token]
end
|