Class: SalesforceBulkQuery::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/salesforce_bulk_query/connection.rb

Overview

Connection to the Salesforce API shared in all classes that do some requests

Constant Summary collapse

XML_REQUEST_HEADER =
{'Content-Type' => 'application/xml; charset=utf-8'}
CSV_REQUEST_HEADER =
{'Content-Type' => 'text/csv; charset=UTF-8'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, api_version, logger = nil, filename_prefix = nil) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(client, api_version, logger=nil, filename_prefix=nil)
  @client = client
  @logger = logger
  @filename_prefix = filename_prefix

  @@API_VERSION = api_version
  @@PATH_PREFIX = "/services/async/#{@@API_VERSION}/"
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/salesforce_bulk_query/connection.rb', line 18

def client
  @client
end

Instance Method Details

#get_to_file(path, filename) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/salesforce_bulk_query/connection.rb', line 71

def get_to_file(path, filename)
  path = "#{@@PATH_PREFIX}#{path}"
  uri = URI.parse( @client.options[:instance_url])
  # open a file
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  headers = XML_REQUEST_HEADER.merge(session_header)
  @logger.info "Doing GET to #{path}, headers #{headers}" if @logger

  if @filename_prefix
    filename = "#{@filename_prefix}_#{filename}"
  end

  # do the request
  http.request_get(path, headers) do |res|

    @logger.info "Got response #{res.inspect}, reading response body by chunks and writing to #{filename}" if @logger

    File.open(filename, 'w') do |file|
      # write the body to the file by chunks
      res.read_body do |segment|

        file.write(segment)
      end
    end
  end
end

#get_xml(path, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/salesforce_bulk_query/connection.rb', line 59

def get_xml(path, options={})
  path = "#{@@PATH_PREFIX}#{path}"
  headers = XML_REQUEST_HEADER

  response = nil
  with_retries do
    response = @client.get(path, {}, headers.merge(session_header))
  end

  return options[:skip_parsing] ? response.body : parse_xml(response.body)
end

#parse_xml(xml) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/salesforce_bulk_query/connection.rb', line 27

def parse_xml(xml)
  parsed = nil
  begin
    parsed = XmlSimple.xml_in(xml)
  rescue => e
    @logger.error "Error parsing xml: #{xml}\n#{e}\n#{e.backtrace}"
    raise
  end

  return parsed
end

#post_xml(path, xml, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/salesforce_bulk_query/connection.rb', line 39

def post_xml(path, xml, options={})
  path = "#{@@PATH_PREFIX}#{path}"
  headers = options[:csv_content_type] ? CSV_REQUEST_HEADER : XML_REQUEST_HEADER

  response = nil
  # do the request
  with_retries do
    begin
      response = @client.post(path, xml, headers.merge(session_header))
    rescue JSON::ParserError => e
      if e.message.index('ExceededQuota')
        raise "You've run out of sfdc batch api quota. Original error: #{e}\n #{e.backtrace}"
      end
      raise e
    end
  end

  return parse_xml(response.body)
end

#query_count(sobject, from, to) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/salesforce_bulk_query/connection.rb', line 115

def query_count(sobject, from, to)
  # do it with retries, if it doesn't succeed, return nil, don't fail.
  begin
    with_retries do
      q = @client.query("SELECT COUNT() FROM #{sobject} WHERE CreatedDate >= #{from} AND CreatedDate < #{to}")
      return q.size
    end
  rescue Faraday::Error::TimeoutError => e
    @logger.warn "Timeout getting count: #{soql}. Error: #{e}. Taking it as failed verification" if @logger
    return nil
  end
end

#session_headerObject



23
24
25
# File 'lib/salesforce_bulk_query/connection.rb', line 23

def session_header
  {'X-SFDC-Session' => @client.options[:oauth_token]}
end

#to_logObject



128
129
130
131
132
133
134
135
# File 'lib/salesforce_bulk_query/connection.rb', line 128

def to_log
  return {
    :client => "Restforce asi",
    :filename_prefix => @filename_prefix,
    :api_version => @@API_VERSION,
    :path_prefix => @@PATH_PREFIX
  }
end

#with_retriesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/salesforce_bulk_query/connection.rb', line 99

def with_retries
  i = 0
  begin
    yield
  rescue => e
    i += 1
    if i < 3
      @logger.warn "Retrying, got error: #{e}, #{e.backtrace}" if @logger
      retry
    else
      @logger.error "Failed 3 times, last error: #{e}, #{e.backtrace}" if @logger
      raise
    end
  end
end