Class: Apadmi::Grout::NetworkService

Inherits:
Object
  • Object
show all
Defined in:
lib/apadmi/grout/utils/network_service.rb

Overview

Utility class for running REST network calls

Constant Summary collapse

PATCH_CONTENT_TYPE =
"application/json-patch+json"
FORM_CONTENT_TYPE =
"application/x-www-form-urlencoded"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_header, base_url, timeout = 30) ⇒ NetworkService

Returns a new instance of NetworkService.

Parameters:

  • auth_header (String)
  • base_url (String)


29
30
31
32
33
# File 'lib/apadmi/grout/utils/network_service.rb', line 29

def initialize(auth_header, base_url, timeout = 30)
  @base_url = base_url
  @auth_header = auth_header
  @timeout = timeout
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/apadmi/grout/utils/network_service.rb', line 9

def base_url
  @base_url
end

Class Method Details

.init_for_basic_auth(username, password, base_url, timeout = 30) ⇒ Object

Params are encoded for use in Basic Auth datatracker.ietf.org/doc/html/rfc7617

Parameters:

  • username (String)
  • password (String)
  • base_url (String)


21
22
23
24
25
# File 'lib/apadmi/grout/utils/network_service.rb', line 21

def self.init_for_basic_auth(username, password, base_url, timeout = 30)
  auth_str = "#{username}:#{password}"
  auth_header = "Basic #{Base64.strict_encode64(auth_str)}"
  NetworkService.new(auth_header, base_url, timeout)
end

Instance Method Details

#do_delete(path) ⇒ Faraday::Response

Parameters:

  • path (String)

Returns:

  • (Faraday::Response)


100
101
102
103
104
105
# File 'lib/apadmi/grout/utils/network_service.rb', line 100

def do_delete(path)
  conn = setup_con
  response = conn.delete("#{@base_url}#{path}", CONTENT_TYPE => APPLICATION_JSON)
  throw_if_error(response)
  response
end

#do_file_post(path, file_path, type) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/apadmi/grout/utils/network_service.rb', line 74

def do_file_post(path, file_path, type)
  conn = Faraday.new do |f|
    f.request :multipart
    f.adapter :net_http
  end
  conn.options.timeout = @timeout
  conn.headers["Authorization"] = @auth_header

  payload = { file: Faraday::UploadIO.new(file_path, type) }
  response = conn.post("#{@base_url}#{path}", payload)
  throw_if_error(response)
  response
end

#do_form_encoded_post(path, payload) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/apadmi/grout/utils/network_service.rb', line 64

def do_form_encoded_post(path, payload)
  conn = setup_con
  response = conn.post("#{@base_url}#{path}") do |req|
    req.headers[CONTENT_TYPE] = FORM_CONTENT_TYPE
    req.body = URI.encode_www_form(payload)
  end
  throw_if_error(response)
  response
end

#do_get(path) ⇒ Faraday::Response

Parameters:

  • path (String)

Returns:

  • (Faraday::Response)


37
38
39
40
41
42
# File 'lib/apadmi/grout/utils/network_service.rb', line 37

def do_get(path)
  conn = setup_con
  response = conn.get("#{@base_url}#{path}", CONTENT_TYPE => APPLICATION_JSON)
  throw_if_error(response)
  response
end

#do_patch(path, payload) ⇒ Faraday::Response

Parameters:

  • path (String)
  • payload (String)

Returns:

  • (Faraday::Response)


91
92
93
94
95
96
# File 'lib/apadmi/grout/utils/network_service.rb', line 91

def do_patch(path, payload)
  conn = setup_con
  response = conn.patch("#{@base_url}#{path}", payload, CONTENT_TYPE => PATCH_CONTENT_TYPE)
  throw_if_error(response)
  response
end

#do_post(path, payload) ⇒ Faraday::Response

Parameters:

  • path (String)
  • payload (String)

Returns:

  • (Faraday::Response)


57
58
59
60
61
62
# File 'lib/apadmi/grout/utils/network_service.rb', line 57

def do_post(path, payload)
  conn = setup_con
  response = conn.post("#{@base_url}#{path}", payload, CONTENT_TYPE => APPLICATION_JSON)
  throw_if_error(response)
  response
end

#do_put(path, payload) ⇒ Faraday::Response

Parameters:

  • path (String)
  • payload (String)

Returns:

  • (Faraday::Response)


47
48
49
50
51
52
# File 'lib/apadmi/grout/utils/network_service.rb', line 47

def do_put(path, payload)
  conn = setup_con
  response = conn.put("#{@base_url}#{path}", payload, CONTENT_TYPE => APPLICATION_JSON)
  throw_if_error(response)
  response
end