Class: Lutaml::Hal::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/hal/client.rb

Overview

HAL Client for making HTTP requests to HAL APIs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lutaml/hal/client.rb', line 16

def initialize(options = {})
  @api_url = options[:api_url] || raise(ArgumentError, 'api_url is required')
  @connection = options[:connection] || create_connection
  @params_default = options[:params_default] || {}
  @debug = options[:debug] || !ENV['DEBUG_API'].nil?
  @cache = options[:cache] || {}
  @cache_enabled = options[:cache_enabled] || false
  @rate_limiter = options[:rate_limiter] || RateLimiter.new(options[:rate_limiting] || {})

  @api_url = strip_api_url(@api_url)
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



14
15
16
# File 'lib/lutaml/hal/client.rb', line 14

def api_url
  @api_url
end

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/lutaml/hal/client.rb', line 14

def connection
  @connection
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



14
15
16
# File 'lib/lutaml/hal/client.rb', line 14

def last_response
  @last_response
end

#rate_limiterObject (readonly)

Returns the value of attribute rate_limiter.



14
15
16
# File 'lib/lutaml/hal/client.rb', line 14

def rate_limiter
  @rate_limiter
end

Instance Method Details

#get(url, params = {}) ⇒ Object

Make a GET request to the API



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lutaml/hal/client.rb', line 41

def get(url, params = {})
  cache_key = "#{url}:#{params.to_json}"

  return @cache[cache_key] if @cache_enabled && @cache.key?(cache_key)

  response = @rate_limiter.with_rate_limiting do
    @last_response = @connection.get(url, params)
    handle_response(@last_response, url)
  end

  @cache[cache_key] = response if @cache_enabled
  response
rescue Faraday::ConnectionFailed => e
  raise Lutaml::Hal::ConnectionError, "Connection failed: #{e.message}"
rescue Faraday::TimeoutError => e
  raise Lutaml::Hal::TimeoutError, "Request timed out: #{e.message}"
rescue Faraday::ParsingError => e
  raise Lutaml::Hal::ParsingError, "Response parsing error: #{e.message}"
rescue Faraday::Adapter::Test::Stubs::NotFound => e
  raise Lutaml::Hal::LinkResolutionError, "Resource not found: #{e.message}"
end

#get_by_url(url, params = {}) ⇒ Object

Get a resource by its full URL



34
35
36
37
38
# File 'lib/lutaml/hal/client.rb', line 34

def get_by_url(url, params = {})
  # Strip API endpoint if it's included
  path = strip_api_url(url)
  get(path, params)
end

#get_with_headers(url, headers = {}) ⇒ Object

Make a GET request with custom headers



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lutaml/hal/client.rb', line 64

def get_with_headers(url, headers = {})
  cache_key = "#{url}:#{headers.to_json}"

  return @cache[cache_key] if @cache_enabled && @cache.key?(cache_key)

  response = @rate_limiter.with_rate_limiting do
    @last_response = @connection.get(url) do |req|
      headers.each { |key, value| req.headers[key] = value }
    end
    handle_response(@last_response, url)
  end

  @cache[cache_key] = response if @cache_enabled
  response
rescue Faraday::ConnectionFailed => e
  raise Lutaml::Hal::ConnectionError, "Connection failed: #{e.message}"
rescue Faraday::TimeoutError => e
  raise Lutaml::Hal::TimeoutError, "Request timed out: #{e.message}"
rescue Faraday::ParsingError => e
  raise Lutaml::Hal::ParsingError, "Response parsing error: #{e.message}"
rescue Faraday::Adapter::Test::Stubs::NotFound => e
  raise Lutaml::Hal::LinkResolutionError, "Resource not found: #{e.message}"
end

#strip_api_url(url) ⇒ Object

Strip any trailing slash from the API URL



29
30
31
# File 'lib/lutaml/hal/client.rb', line 29

def strip_api_url(url)
  url.sub(%r{/\Z}, '')
end