Class: GdsApi::JsonClient

Inherits:
Object
  • Object
show all
Includes:
ExceptionHandling
Defined in:
lib/gds_api/json_client.rb

Constant Summary collapse

DEFAULT_TIMEOUT_IN_SECONDS =
4
DEFAULT_CACHE_SIZE =
100
DEFAULT_CACHE_TTL =

15 minutes

15 * 60

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExceptionHandling

#build_specific_http_error, #error_class_for_code

Constructor Details

#initialize(options = {}) ⇒ JsonClient

Returns a new instance of JsonClient.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gds_api/json_client.rb', line 36

def initialize(options = {})
  if options[:disable_timeout] || options[:timeout].to_i < 0
    raise "It is no longer possible to disable the timeout."
  end

  @logger = options[:logger] || NullLogger.instance

  if options[:disable_cache] || (options[:cache_size] == 0)
    @cache = NullCache.new
  else
    cache_size = options[:cache_size] || DEFAULT_CACHE_SIZE
    cache_ttl = options[:cache_ttl] || DEFAULT_CACHE_TTL
    @cache = JsonClient.cache(cache_size, cache_ttl)
  end
  @options = options
end

Class Attribute Details

.cache(size = DEFAULT_CACHE_SIZE, ttl = DEFAULT_CACHE_TTL) ⇒ Object

Cache TTL will be overridden for a given request/response by the Expires header if it is included in the response.

LRUCache doesn’t respect a cache size of 0, and instead effectively creates a cache with a size of 1.



19
20
21
# File 'lib/gds_api/json_client.rb', line 19

def self.cache(size = DEFAULT_CACHE_SIZE, ttl = DEFAULT_CACHE_TTL)
  @cache ||= LRUCache.new(max_size: size, ttl: ttl)
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



34
35
36
# File 'lib/gds_api/json_client.rb', line 34

def cache
  @cache
end

#loggerObject

Returns the value of attribute logger.



34
35
36
# File 'lib/gds_api/json_client.rb', line 34

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



34
35
36
# File 'lib/gds_api/json_client.rb', line 34

def options
  @options
end

Class Method Details

.default_request_headersObject



53
54
55
56
57
58
59
# File 'lib/gds_api/json_client.rb', line 53

def self.default_request_headers
  {
    'Accept' => 'application/json',
    # GOVUK_APP_NAME is set for all apps by Puppet
    'User-Agent' => "gds-api-adapters/#{GdsApi::VERSION} (#{ENV['GOVUK_APP_NAME']})"
  }
end

.default_request_with_json_body_headersObject



61
62
63
# File 'lib/gds_api/json_client.rb', line 61

def self.default_request_with_json_body_headers
  self.default_request_headers.merge(self.json_body_headers)
end

.json_body_headersObject



65
66
67
68
69
# File 'lib/gds_api/json_client.rb', line 65

def self.json_body_headers
  {
    'Content-Type' => 'application/json',
  }
end

Instance Method Details

#delete_json(url, additional_headers = {}) ⇒ Object



99
100
101
# File 'lib/gds_api/json_client.rb', line 99

def delete_json(url, additional_headers = {})
  do_json_request(:delete, url, nil, additional_headers)
end

#delete_json_with_params!(url, params, additional_headers = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gds_api/json_client.rb', line 103

def delete_json_with_params!(url, params, additional_headers = {})
  warn <<-doc
    DEPRECATION NOTICE: Delete requests should not include parameters.

    Do not use this method as the ability to do this will be removed.

    Called from: #{caller[2]}
  doc

  do_json_request(:delete, url, params, additional_headers)
end

#get_json(url, additional_headers = {}, &create_response) ⇒ Object



83
84
85
# File 'lib/gds_api/json_client.rb', line 83

def get_json(url, additional_headers = {}, &create_response)
  do_json_request(:get, url, nil, additional_headers, &create_response)
end

#get_raw(url) ⇒ Object



79
80
81
# File 'lib/gds_api/json_client.rb', line 79

def get_raw(url)
  get_raw!(url)
end

#get_raw!(url) ⇒ Object



75
76
77
# File 'lib/gds_api/json_client.rb', line 75

def get_raw!(url)
  do_raw_request(:get, url)
end

#patch_json(url, params, additional_headers = {}) ⇒ Object



95
96
97
# File 'lib/gds_api/json_client.rb', line 95

def patch_json(url, params, additional_headers = {})
  do_json_request(:patch, url, params, additional_headers)
end

#post_json(url, params = {}, additional_headers = {}) ⇒ Object



87
88
89
# File 'lib/gds_api/json_client.rb', line 87

def post_json(url, params = {}, additional_headers = {})
  do_json_request(:post, url, params, additional_headers)
end

#post_multipart(url, params) ⇒ Object



115
116
117
118
# File 'lib/gds_api/json_client.rb', line 115

def post_multipart(url, params)
  r = do_raw_request(:post, url, params.merge(multipart: true))
  Response.new(r)
end

#put_json(url, params, additional_headers = {}) ⇒ Object



91
92
93
# File 'lib/gds_api/json_client.rb', line 91

def put_json(url, params, additional_headers = {})
  do_json_request(:put, url, params, additional_headers)
end

#put_multipart(url, params) ⇒ Object



120
121
122
123
# File 'lib/gds_api/json_client.rb', line 120

def put_multipart(url, params)
  r = do_raw_request(:put, url, params.merge(multipart: true))
  Response.new(r)
end