Module: MarketingCloudSDK::HTTPRequest

Included in:
Targeting
Defined in:
lib/marketingcloudsdk/http_request.rb

Instance Method Summary collapse

Instance Method Details

#generate_uri(url, params = nil) ⇒ Object



92
93
94
95
96
# File 'lib/marketingcloudsdk/http_request.rb', line 92

def generate_uri(url, params=nil)
  uri = URI.parse(url)
  uri.query = URI.encode_www_form(params) if params
  uri
end

#request(method, url, options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/marketingcloudsdk/http_request.rb', line 98

def request(method, url, options={})
  uri = generate_uri url, options['params']
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  data = options['data']
  _request = method.new uri.request_uri
  _request.body = data.to_json if data
  _request.content_type = options['content_type'] if options['content_type']
  _request.add_field('User-Agent', 'FuelSDK-Ruby-v' + MarketingCloudSDK::VERSION)

  # Add Authorization header if we have an access token
  if options['access_token']
    _request.add_field('Authorization', 'Bearer ' + options['access_token'])
  end

  response = http.request(_request)
  HTTPResponse.new(response, self, :url => url, :options => options)
end