Class: GdsApi::JsonClient
Constant Summary
collapse
- DEFAULT_TIMEOUT_IN_SECONDS =
4
- DEFAULT_CACHE_SIZE =
100
- DEFAULT_CACHE_TTL =
15 * 60
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#delete_json(url, additional_headers = {}) ⇒ Object
-
#delete_json_with_params!(url, params, additional_headers = {}) ⇒ Object
-
#get_json(url, additional_headers = {}, &create_response) ⇒ Object
-
#get_raw(url) ⇒ Object
-
#get_raw!(url) ⇒ Object
-
#initialize(options = {}) ⇒ JsonClient
constructor
A new instance of JsonClient.
-
#patch_json(url, params, additional_headers = {}) ⇒ Object
-
#post_json(url, params = {}, additional_headers = {}) ⇒ Object
-
#post_multipart(url, params) ⇒ Object
-
#put_json(url, params, additional_headers = {}) ⇒ Object
-
#put_multipart(url, params) ⇒ Object
#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
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.
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
34
35
36
|
# File 'lib/gds_api/json_client.rb', line 34
def cache
@cache
end
|
#logger ⇒ Object
Returns the value of attribute logger.
34
35
36
|
# File 'lib/gds_api/json_client.rb', line 34
def logger
@logger
end
|
#options ⇒ Object
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
54
55
56
57
58
59
60
|
# File 'lib/gds_api/json_client.rb', line 54
def self.
{
'Accept' => 'application/json',
'User-Agent' => "gds-api-adapters/#{GdsApi::VERSION} (#{ENV['GOVUK_APP_NAME']})"
}
end
|
.default_request_with_json_body_headers ⇒ Object
62
63
64
|
# File 'lib/gds_api/json_client.rb', line 62
def self.default_request_with_json_body_headers
self..merge(self.json_body_headers)
end
|
.json_body_headers ⇒ Object
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, = {})
do_json_request(:delete, url, nil, )
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, = {})
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, )
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, = {}, &create_response)
do_json_request(:get, url, nil, , &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, = {})
do_json_request(:patch, url, params, )
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 = {}, = {})
do_json_request(:post, url, params, )
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, = {})
do_json_request(:put, url, params, )
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
|