Class: SalesforceBulkApi::Connection

Inherits:
Object
  • Object
show all
Includes:
SalesforceBulkApi::Concerns::Throttling
Defined in:
lib/salesforce_bulk_api/connection.rb

Constant Summary collapse

LOGIN_HOST =
'login.salesforce.com'

Instance Method Summary collapse

Methods included from SalesforceBulkApi::Concerns::Throttling

#add_throttle, #set_status_throttle, #set_throttle_limit_in_seconds, #throttles

Constructor Details

#initialize(api_version, client) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
13
14
15
# File 'lib/salesforce_bulk_api/connection.rb', line 9

def initialize(api_version, client)
  @client = client
  @api_version = api_version
  @path_prefix = "/services/async/#{@api_version}/"

  ()
end

Instance Method Details

#countersObject



73
74
75
76
77
78
# File 'lib/salesforce_bulk_api/connection.rb', line 73

def counters
  {
    get: get_counters[:get],
    post: get_counters[:post]
  }
end

#get_request(host, path, headers) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/salesforce_bulk_api/connection.rb', line 54

def get_request(host, path, headers)
  host = host || @instance_host
  path = "#{@path_prefix}#{path}"
  if host != LOGIN_HOST # Not login, need to add session id to header
    headers['X-SFDC-Session'] = @session_id;
  end

  count :get
  throttle(http_method: :get, path: path)
  https(host).get(path, headers).body
end

#https(host) ⇒ Object



66
67
68
69
70
71
# File 'lib/salesforce_bulk_api/connection.rb', line 66

def https(host)
  req = Net::HTTP.new(host, 443)
  req.use_ssl = true
  req.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req
end

#loginObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/salesforce_bulk_api/connection.rb', line 17

def ()
  client_type = @client.class.to_s
  case client_type
  when "Restforce::Data::Client"
    @session_id = @client.options[:oauth_token]
    @server_url = @client.options[:instance_url]
  else
    @session_id = @client.oauth_token
    @server_url = @client.instance_url
  end
  @instance = parse_instance()
  @instance_host = "#{@instance}.salesforce.com"
end

#post_xml(host, path, xml, headers) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/salesforce_bulk_api/connection.rb', line 31

def post_xml(host, path, xml, headers)
  host = host || @instance_host
  if host != LOGIN_HOST # Not login, need to add session id to header
    headers['X-SFDC-Session'] = @session_id
    path = "#{@path_prefix}#{path}"
  end
  i = 0
  begin
    count :post
    throttle(http_method: :post, path: path)
    https(host).post(path, xml, headers).body
  rescue
    i += 1
    if i < 3
      puts "Request fail #{i}: Retrying #{path}"
      retry
    else
      puts "FATAL: Request to #{path} failed three times."
      raise
    end
  end
end