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

Class Method Details

.get_from_amp(url_endpoint_path, query_params, include_token) ⇒ Hash

Performs a GET request to the specified AMP API endpoint.

Parameters:

  • url_endpoint_path (String)

    The AMP API endpoint to request data from

  • query_params (Hash)

    A map of params (keys and values) to include in the query of the request

  • include_token (Boolean)

    Defines whether or not to include the user's API token in this request

Returns:

  • (Hash)

    The JSON data in the body of the response from AMP

Raises:



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.message})
      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.

Parameters:

  • url_endpoint_path (String)

    The AMP API endpoint to request data from

  • body_params (Hash)

    A map of params (keys and values) to include in the body of the request as JSON

  • include_token (Boolean)

    Defines whether or not to include the user's API token in this request

Returns:

  • (Hash)

    The JSON data in the body of the response from AMP

Raises:



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.message})
      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.

Parameters:

  • url_endpoint_path (String)

    The Level Access Platform API endpoint to request data from

  • body_params (Hash)

    A map of params (keys and values) to include in the body of the request as JSON

Returns:

  • (Hash)

    The JSON data in the body of the response from Level Access Platform

Raises:

  • (HttpErrorException)

    If an error is encountered while attempting to connect to Level Access Platform



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.message})
      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