Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Return http delete, batch and patch responses #13

Merged
merged 5 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ PLATFORMS
ruby
x86_64-darwin-16
x86_64-darwin-17
x86_64-linux

DEPENDENCIES
ontologies_api_client!
Expand Down
39 changes: 26 additions & 13 deletions lib/ontologies_api_client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def members
def length
@table.keys.length
end

alias :size :length

def to_a
Expand All @@ -37,7 +38,9 @@ def values_at(*selectors)
module LinkedData
module Client
module HTTP
class Link < String; attr_accessor :media_type; end
class Link < String
attr_accessor :media_type;
end

def self.conn
unless LinkedData::Client.connection_configured?
Expand All @@ -53,7 +56,7 @@ def self.conn
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 = 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

Expand Down Expand Up @@ -83,7 +86,7 @@ def self.get(path, params = {}, options = {})
else
obj = recursive_struct(load_json(response.body))
end
rescue Exception => e
rescue StandardError => e
puts "Problem getting #{path}" if $DEBUG
raise e
end
Expand All @@ -94,12 +97,12 @@ def self.get_batch(paths, params = {})
responses = []
if conn.in_parallel?
conn.in_parallel do
paths.each {|p| responses << conn.get(p, params) }
paths.each { |p| responses << conn.get(p, params) }
end
else
responses = threaded_request(paths, params)
end
return responses
responses
end

def self.post(path, obj, options = {})
Expand All @@ -108,11 +111,12 @@ def self.post(path, obj, options = {})
req.url path
custom_req(obj, file, file_attribute, req)
end
raise Exception, response.body if response.status >= 500
raise StandardError, response.body if response.status >= 500

if options[:raw] || false # return the unparsed body of the request
return response.body
response.body
else
return recursive_struct(load_json(response.body))
recursive_struct(load_json(response.body))
end
end

Expand All @@ -122,7 +126,8 @@ def self.put(path, obj)
req.url path
custom_req(obj, file, file_attribute, req)
end
raise Exception, response.body if response.status >= 500
raise StandardError, response.body if response.status >= 500

recursive_struct(load_json(response.body))
end

Expand All @@ -132,13 +137,17 @@ def self.patch(path, obj)
req.url path
custom_req(obj, file, file_attribute, req)
end
raise Exception, response.body if response.status >= 500
raise StandardError, response.body if response.status >= 500

response
end

def self.delete(id)
puts "Deleting #{id}" if $DEBUG
response = conn.delete id
raise Exception, response.body if response.status >= 500
raise StandardError, response.body if response.status >= 500

response
end

def self.object_from_json(json)
Expand Down Expand Up @@ -170,9 +179,11 @@ def self.custom_req(obj, file, file_attribute, req)

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
Expand Down Expand Up @@ -203,7 +214,7 @@ def self.recursive_struct(json_obj)
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 = json_obj.keys.map { |k| k.to_sym }
attributes_always_present = value_cls.attrs_always_present || [] rescue []
attributes = (attributes + attributes_always_present).uniq

Expand Down Expand Up @@ -233,7 +244,7 @@ def self.recursive_struct(json_obj)
end
else
# Get the struct class
recursive_obj_hash = {links: nil, context: nil}
recursive_obj_hash = { links: nil, context: nil }
json_obj.each do |key, value|
recursive_obj_hash[key] = recursive_struct(value)
end
Expand All @@ -260,6 +271,7 @@ def self.prep_links(obj)

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]
Expand All @@ -270,6 +282,7 @@ def self.prep_links(obj)

def self.load_json(json)
return if json.nil? || json.empty?

begin
MultiJson.load(json)
rescue Exception => e
Expand Down