31
32
33
34
35
36
37
38
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/cnvrg/api.rb', line 31
def self.request(resource, method = 'GET', data = {}, parse_request = true)
begin
n = Netrc.read
rescue => e
end
if n['cnvrg.io'].nil?
puts 'You\'re not logged in'
puts 'Please log in via `cnvrg login`'
return
end
@user, @pass = n[Cnvrg::Helpers.netrc_domain]
begin
conn = Faraday.new
conn.['Auth-Token'] = @pass
conn.['User-Agent'] = "#{Cnvrg::API::USER_AGENT}"
case method
when 'GET'
response = conn.get "#{endpoint_uri}/#{resource}"
if parse_request == true
JSON.parse(response.body)
else
response
end
when 'POST'
conn.options.timeout = 420
conn.options.open_timeout =420
response = conn.post "#{endpoint_uri}/#{resource}", data
if parse_request == true
JSON.parse(response.body)
else
response
end
when 'POST_FILE'
conn = Faraday.new do |fr|
fr.['Auth-Token'] = @pass
fr.['User-Agent'] = "#{Cnvrg::API::USER_AGENT}"
fr.["Content-Type"] = "multipart/form-data"
fr.request :multipart
fr.request :url_encoded
fr.adapter :net_http
end
conn.options.timeout = 420
conn.options.open_timeout =420
file_base = File.basename(data[:relative_path])
temp_path = File.expand_path('~')+"/.cnvrg/tmp_files/#{file_base}"
FileUtils.touch(temp_path)
data[:file] = Faraday::UploadIO.new("#{temp_path}", "plain/text")
response = conn.post "#{endpoint_uri}/#{resource}", data
FileUtils.rm_rf(temp_path)
if parse_request == true
JSON.parse(response.body)
else
response
end
when 'DELETE'
response = conn.delete "#{endpoint_uri}/#{resource}", data
if parse_request == true
JSON.parse(response.body)
else
response
end
else
end
rescue => e
puts e
return nil
rescue SignalException
return false
end
end
|