Class: Apadmi::Grout::NetworkService
- Inherits:
-
Object
- Object
- Apadmi::Grout::NetworkService
- 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
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #do_delete(path) ⇒ Faraday::Response
- #do_file_post(path, file_path, type) ⇒ Object
- #do_form_encoded_post(path, payload) ⇒ Object
- #do_get(path) ⇒ Faraday::Response
- #do_patch(path, payload) ⇒ Faraday::Response
- #do_post(path, payload) ⇒ Faraday::Response
- #do_put(path, payload) ⇒ Faraday::Response
-
#initialize(auth_header, base_url, timeout = 30) ⇒ NetworkService
constructor
A new instance of NetworkService.
Constructor Details
#initialize(auth_header, base_url, timeout = 30) ⇒ NetworkService
Returns a new instance of NetworkService.
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_url ⇒ Object (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
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
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..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
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
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
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
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 |