forked from ontoportal/ontologies_api_ruby_client
-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.rb
298 lines (254 loc) · 8.77 KB
/
http.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
require 'oj'
require 'multi_json'
require 'digest'
require 'ostruct'
##
# This monkeypatch makes OpenStruct act like Struct objects
class OpenStruct
alias :each :each_pair
def member?(key)
@table.key?(key)
end
def members
@table.keys
end
def length
@table.keys.length
end
alias :size :length
def to_a
@table.to_a
end
def values
@table.values
end
def values_at(*selectors)
@table.values.values_at(*selectors)
end
end
module LinkedData
module Client
module HTTP
class Link < String
attr_accessor :media_type;
end
def self.conn
unless LinkedData::Client.connection_configured?
if Kernel.const_defined?("Rails")
rails = Kernel.const_get("Rails")
store = rails.cache if rails.cache
end
LinkedData::Client.config_connection(cache_store: store)
end
LinkedData::Client.settings.conn
end
def self.get(path, params = {}, options = {})
headers = options[:headers] || {}
raw = options[:raw] || false # return the unparsed body of the request
params = params.delete_if { |k, v| v == nil || v.to_s.empty? }
params[:ncbo_cache_buster] = Time.now.to_f if raw # raw requests don't get cached to ensure body is available
invalidate_cache = params.delete(:invalidate_cache) || false
begin
puts "Getting: #{path} with #{params}" if $DEBUG
begin
response = conn.get do |req|
req.url path
req.params = params.dup
req.options[:timeout] = 60
req.headers.merge(headers)
req.headers[:invalidate_cache] = invalidate_cache
end
rescue Exception => e
params = Faraday::Utils.build_query(params)
path << "?" unless params.empty? || path.include?("?")
raise e, "Problem retrieving:\n#{path}#{params}\n\nError: #{e.message}\n#{e.backtrace.join("\n\t")}"
end
raise Exception, "Problem retrieving:\n#{path}\n#{response.body}" if response.status >= 500
if raw
obj = response.body
elsif response.respond_to?(:parsed_body) && response.parsed_body
obj = response.parsed_body
obj = obj.dup if obj.frozen?
else
obj = recursive_struct(load_json(response.body))
end
rescue StandardError => e
puts "Problem getting #{path}" if $DEBUG
raise e
end
obj
end
def self.get_batch(paths, params = {})
responses = []
if conn.in_parallel?
conn.in_parallel do
paths.each { |p| responses << conn.get(p, params) }
end
else
responses = threaded_request(paths, params)
end
responses
end
def self.post(path, obj, options = {})
file, file_attribute = params_file_handler(obj)
response = conn.post do |req|
req.url path
custom_req(obj, file, file_attribute, req)
end
raise StandardError, response.body if response.status >= 500
if options[:raw] || false # return the unparsed body of the request
response.body
else
recursive_struct(load_json(response.body))
end
end
def self.put(path, obj)
file, file_attribute = params_file_handler(obj)
response = conn.put do |req|
req.url path
custom_req(obj, file, file_attribute, req)
end
raise StandardError, response.body if response.status >= 500
recursive_struct(load_json(response.body))
end
def self.patch(path, obj)
file, file_attribute = params_file_handler(obj)
response = conn.patch do |req|
req.url path
custom_req(obj, file, file_attribute, req)
end
raise StandardError, response.body if response.status >= 500
response
end
def self.delete(id)
puts "Deleting #{id}" if $DEBUG
response = conn.delete id
raise StandardError, response.body if response.status >= 500
response
end
def self.object_from_json(json)
recursive_struct(load_json(json))
end
private
def self.custom_req(obj, file, file_attribute, req)
req.headers['Content-Type'] = 'application/json'
if file
# multipart
boundary = "OntologiesAPIMultipartPost"
req.headers['Content-Type'] = "multipart/mixed; boundary=#{boundary}; type=application/json; start=json"
parts = []
parts << Faraday::Parts::Part.new(boundary, "json\"\r\nContent-Type: \"application/json; charset=UTF-8", MultiJson.dump(obj))
parts << Faraday::Parts::Part.new(boundary, file_attribute, file)
parts << Faraday::Parts::EpiloguePart.new(boundary)
req.body = Faraday::CompositeReadIO.new(parts)
req.headers["Content-Length"] = req.body.length.to_s
else
# normal
req.body = MultiJson.dump(obj)
end
true
end
def self.params_file_handler(params)
return if params.nil?
file, return_attribute = nil, nil
params.dup.each do |attribute, value|
next unless value.is_a?(File) || value.is_a?(Tempfile) || value.is_a?(ActionDispatch::Http::UploadedFile)
filename = value.original_filename
file = Faraday::UploadIO.new(value.path, "text/plain", filename)
return_attribute = attribute
params.delete(attribute)
end
return file, return_attribute
end
def self.threaded_request(paths, params)
threads = []
responses = []
paths.each do |path|
threads << Thread.new do
responses << get(path, params)
end
end
threads.join
responses
end
def self.recursive_struct(json_obj)
# TODO: Convert dates to date objects
if json_obj.is_a?(Hash)
return if json_obj.empty?
value_cls = LinkedData::Client::Base.class_for_type(json_obj["@type"])
links = prep_links(json_obj) # strip links
context = json_obj.delete("@context") # strip context
# Create a struct with the left-over attributes to store data
attributes = json_obj.keys.map { |k| k.to_sym }
attributes_always_present = value_cls.attrs_always_present || [] rescue []
attributes = (attributes + attributes_always_present).uniq
# Add attributes to instance
if value_cls
instance = value_cls.new
attributes.each do |attr|
attr = attr[1..-1] if attr[0].eql?("@")
instance.class.class_eval do
unless method_defined?(attr.to_sym)
define_method attr.to_sym do
instance_variable_get("@#{attr}")
end
end
unless method_defined?("#{attr}=")
define_method "#{attr}=" do |val|
instance_variable_set("@#{attr}", val)
end
end
end
end
# Create objects for each key/value pair, recursively
json_obj.each do |attr, value|
attr = attr[1..-1] if attr[0].eql?("@")
instance.instance_variable_set("@#{attr}", recursive_struct(value))
end
else
# Get the struct class
recursive_obj_hash = { links: nil, context: nil }
json_obj.each do |key, value|
recursive_obj_hash[key] = recursive_struct(value)
end
instance = OpenStruct.new(recursive_obj_hash)
end
# Assign links/context
instance.links = links if links
instance.context = context if context
elsif json_obj.is_a?(Array)
instance = []
json_obj.each do |value|
instance << recursive_struct(value)
end
else
instance = value_cls ? value_cls.new(values: json_obj) : json_obj
end
instance
end
def self.prep_links(obj)
links = obj.delete(LinkedData::Client.settings.links_attr)
return if links.nil?
context = links.delete("@context")
return if context.nil?
links.keys.each do |link_type|
link = Link.new(links[link_type])
link.media_type = context[link_type]
links[link_type] = link
end
links
end
def self.load_json(json)
return if json.nil? || json.empty?
begin
MultiJson.load(json)
rescue Exception => e
raise e, "Problem loading json\n#{json}"
end
end
def self.dump_json(json)
MultiJson.dump(json)
end
end
end
end