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

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.



14
15
16
17
18
19
20
21
# File 'lib/gds_api/json_client.rb', line 14

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

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

Instance Attribute Details

#loggerObject



12
13
14
# File 'lib/gds_api/json_client.rb', line 12

def logger
  @logger
end

#optionsObject



12
13
14
# File 'lib/gds_api/json_client.rb', line 12

def options
  @options
end

Class Method Details

.default_request_headersObject



23
24
25
26
27
28
29
# File 'lib/gds_api/json_client.rb', line 23

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



31
32
33
# File 'lib/gds_api/json_client.rb', line 31

def self.default_request_with_json_body_headers
  default_request_headers.merge(json_body_headers)
end

.json_body_headersObject



35
36
37
38
39
# File 'lib/gds_api/json_client.rb', line 35

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

Instance Method Details

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



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

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

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



51
52
53
# File 'lib/gds_api/json_client.rb', line 51

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

#get_raw(url) ⇒ Object



47
48
49
# File 'lib/gds_api/json_client.rb', line 47

def get_raw(url)
  get_raw!(url)
end

#get_raw!(url) ⇒ Object



43
44
45
# File 'lib/gds_api/json_client.rb', line 43

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

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



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

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

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



55
56
57
# File 'lib/gds_api/json_client.rb', line 55

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

#post_multipart(url, params) ⇒ Object



71
72
73
74
# File 'lib/gds_api/json_client.rb', line 71

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



59
60
61
# File 'lib/gds_api/json_client.rb', line 59

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

#put_multipart(url, params) ⇒ Object



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

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