Skip to content

Commit

Permalink
add $API_CLIENT_INVALIDATE_CACHE option to disable cache
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Dec 16, 2023
1 parent 169afb7 commit 38e4299
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/ontologies_api_client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'multi_json'
require 'digest'
require 'ostruct'

require 'benchmark'
##
# This monkeypatch makes OpenStruct act like Struct objects
class OpenStruct
Expand Down Expand Up @@ -58,18 +58,20 @@ def self.get(path, params = {}, options = {})
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

invalidate_cache = params.delete(:invalidate_cache) || $API_CLIENT_INVALIDATE_CACHE || false
begin
puts "Getting: #{path} with #{params}" if $DEBUG_API_CLIENT
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
response = nil
time = Benchmark.realtime do
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
end
puts "Getting: #{path} with #{params} (#{time}s)" if $DEBUG_API_CLIENT
rescue Exception => e
params = Faraday::Utils.build_query(params)
path << "?" unless params.empty? || path.include?("?")
Expand Down

0 comments on commit 38e4299

Please sign in to comment.