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