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


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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
  disable_cache = options[:disable_cache] || ENV.fetch("GDS_API_DISABLE_CACHE", false)

  if disable_cache || options[:cache_size]&.zero?
    @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


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

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


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

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

.json_body_headersObject


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

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

Instance Method Details

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


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

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

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


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

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


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

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

#get_raw(url) ⇒ Object


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

def get_raw(url)
  get_raw!(url)
end

#get_raw!(url) ⇒ Object


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

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

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


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

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

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


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

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

#post_multipart(url, params) ⇒ Object


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

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


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

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

#put_multipart(url, params) ⇒ Object


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

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