Class: BaseInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid/base_interface.rb

Overview

Initialize the HTTP client

Direct Known Subclasses

SendGrid::API, TwilioEmail::API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth:, host:, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {}) ⇒ BaseInterface

  • Args :

    • auth -> authorization header value

    • host -> the base URL for the API

    • request_headers -> any headers that you want to be globally applied

    • version -> the version of the API you wish to access,

      currently only "v3" is supported
      
    • impersonate_subuser -> the subuser to impersonate, will be passed

      in the "On-Behalf-Of" header
      
    • http_options -> http options that you want to be globally applied to each request



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sendgrid/base_interface.rb', line 19

def initialize(auth:, host:, request_headers: nil, version: nil, impersonate_subuser: nil, http_options: {})
  @auth = auth
  @host = host
  @version = version || 'v3'
  @impersonate_subuser = impersonate_subuser
  @user_agent = "sendgrid/#{SendGrid::VERSION};ruby"
  @request_headers = JSON.parse('
      {
        "Authorization": "' + @auth + '",
        "Accept": "application/json",
        "User-Agent": "' + @user_agent + '"
      }
  ')
  @request_headers['On-Behalf-Of'] = @impersonate_subuser if @impersonate_subuser

  @request_headers = @request_headers.merge(request_headers) if request_headers
  @http_options = http_options
  @client = SendGrid::Client.new(host: "#{@host}/#{@version}",
                                 request_headers: @request_headers,
                                 http_options: @http_options)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/sendgrid/base_interface.rb', line 6

def client
  @client
end

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/sendgrid/base_interface.rb', line 7

def host
  @host
end

#http_optionsObject (readonly)

Returns the value of attribute http_options.



7
8
9
# File 'lib/sendgrid/base_interface.rb', line 7

def http_options
  @http_options
end

#impersonate_subuserObject (readonly)

Returns the value of attribute impersonate_subuser.



7
8
9
# File 'lib/sendgrid/base_interface.rb', line 7

def impersonate_subuser
  @impersonate_subuser
end

#request_headersObject (readonly)

Returns the value of attribute request_headers.



7
8
9
# File 'lib/sendgrid/base_interface.rb', line 7

def request_headers
  @request_headers
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/sendgrid/base_interface.rb', line 7

def version
  @version
end

Instance Method Details

#sendgrid_data_residency(region:) ⇒ Object

Client libraries contain setters for specifying region/edge. This supports global and eu regions only. This set will likely expand in the future. Global is the default residency (or region) Global region means the message will be sent through api.sendgrid.com EU region means the message will be sent through api.eu.sendgrid.com Parameters:

  • region(String) : specify the region. Currently supports “global” and “eu”

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
# File 'lib/sendgrid/base_interface.rb', line 48

def sendgrid_data_residency(region:)
  region_host_dict = { "eu" => 'https://api.eu.sendgrid.com', "global" => 'https://api.sendgrid.com' }
  raise ArgumentError, "region can only be \"eu\" or \"global\"" if region.nil? || !region_host_dict.key?(region)

  @host = region_host_dict[region]
  @client = SendGrid::Client.new(host: "#{@host}/#{@version}",
                                 request_headers: @request_headers,
                                 http_options: @http_options)
end