Module: Continuum::NetworkUtil
- Defined in:
- src/lib/continuum/network_util.rb
Overview
This module encapsulates all network requests Continuum itself makes to the Internet.
Class Method Summary collapse
-
.get_from_amp(url_endpoint_path, query_params, include_token) ⇒ Hash
Performs a GET request to the specified AMP API endpoint.
-
.post_to_amp(url_endpoint_path, body_params, include_token) ⇒ Hash
Performs a POST request to the specified AMP API endpoint.
-
.post_to_level_access_platform(url_endpoint_path, body_params) ⇒ Hash
Performs a POST request to the specified Level Access Platform API endpoint.
-
.start_request(endpoint_uri, query_params = {}, include_token = false) ⇒ Object
Starts an HTTP connection to the given endpoint.
Class Method Details
.get_from_amp(url_endpoint_path, query_params, include_token) ⇒ Hash
Performs a GET request to the specified AMP API endpoint.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'src/lib/continuum/network_util.rb', line 26 def self.get_from_amp(url_endpoint_path, query_params, include_token) uri = URI.join(Continuum.configuration.amp_instance_url, url_endpoint_path) start_request(uri, query_params, include_token) do |http| request = Net::HTTP::Get.new(uri.request_uri) request['Content-Type'] = 'application/json; charset=UTF-8' response = http.request(request) if response.is_a?(Net::HTTPSuccess) return DataUtil.parse_json(response.body) end raise HttpErrorException.new(%{ Unexpectedly encountered a non-200 status code (#{response.code} #{response.}) while attempting to GET data from #{uri.request_uri} Response: #{response.body} }) end end |
.post_to_amp(url_endpoint_path, body_params, include_token) ⇒ Hash
Performs a POST request to the specified AMP API endpoint.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'src/lib/continuum/network_util.rb', line 54 def self.post_to_amp(url_endpoint_path, body_params, include_token) uri = URI.join(Continuum.configuration.amp_instance_url, url_endpoint_path) start_request(uri, {}, include_token) do |http| request = Net::HTTP::Post.new(uri.request_uri) request.body = body_params.to_json if body_params request['Content-Type'] = 'application/json; charset=UTF-8' response = http.request(request) if response.is_a?(Net::HTTPSuccess) return DataUtil.parse_json(response.body) end raise HttpErrorException.new(%{ Unexpectedly encountered a non-200 status code (#{response.code} #{response.}) while attempting to POST data to #{uri.request_uri}, with: #{request.body} Response: #{response.body} }) end end |
.post_to_level_access_platform(url_endpoint_path, body_params) ⇒ Hash
Performs a POST request to the specified Level Access Platform API endpoint.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'src/lib/continuum/network_util.rb', line 82 def self.post_to_level_access_platform(url_endpoint_path, body_params) uri = URI.join(Continuum.configuration.level_access_platform_configuration.org_instance_url, url_endpoint_path) start_request(uri, {}, false) do |http| request = Net::HTTP::Post.new(uri.request_uri) request.body = body_params.to_json if body_params request['Content-Type'] = 'application/json; charset=UTF-8' request['x-auth-token'] = Continuum.configuration.level_access_platform_configuration.api_key response = http.request(request) if response.is_a?(Net::HTTPSuccess) return DataUtil.parse_json(response.body) end raise HttpErrorException.new(%{ Unexpectedly encountered a non-200 status code (#{response.code} #{response.}) while attempting to POST data to #{uri.request_uri}, with: #{request.body} Response: #{response.body} }) end end |
.start_request(endpoint_uri, query_params = {}, include_token = false) ⇒ Object
Starts an HTTP connection to the given endpoint
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'src/lib/continuum/network_util.rb', line 108 def self.start_request(endpoint_uri, query_params = {}, include_token = false) query_params = include_token ? { :apiToken => Continuum.configuration.amp_api_token }.merge(query_params) : query_params endpoint_uri.query = URI.encode_www_form(query_params) unless query_params.empty? proxy_configuration = Continuum.configuration.proxy_configuration args = [endpoint_uri.host, endpoint_uri.port] if proxy_configuration args.push( proxy_configuration.host, proxy_configuration.port, proxy_configuration.username, proxy_configuration.password ) end Net::HTTP.start( *args, :use_ssl => endpoint_uri.scheme == 'https', :open_timeout => @timeout, :read_timeout => @timeout ) do |http| yield http end end |