Class: AdwordsApi::BatchJobUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/adwords_api/batch_job_utils.rb

Instance Method Summary collapse

Constructor Details

#initialize(api, version) ⇒ BatchJobUtils

Default constructor.

Args:

  • api: AdwordsApi object

  • version: API version to use



36
37
38
# File 'lib/adwords_api/batch_job_utils.rb', line 36

def initialize(api, version)
  @api, @version = api, version
end

Instance Method Details

#get_job_results(batch_job_url) ⇒ Object

Downloads the results of a batch job from the specified URL.

Args:

  • batch_job_url: The URL provided by BatchJobService to fetch the results from

Returns:

  • the results of the batch job, as a ruby hash, or nil if none yet exist



165
166
167
168
169
170
171
172
173
# File 'lib/adwords_api/batch_job_utils.rb', line 165

def get_job_results(batch_job_url)
  xml_response = AdsCommon::Http.get_response(batch_job_url, @api.config)
  begin
    return sanitize_result(
        get_nori().parse(xml_response.body)[:mutate_response][:rval])
  rescue
    return nil
  end
end

#initialize_url(batch_job_url) ⇒ Object

Initializes an upload URL to get the actual URL to which to upload operations.

Args:

  • batch_job_url: The UploadURL provided by BatchJobService

Returns:

  • The URL that should actually be used to upload operations.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/adwords_api/batch_job_utils.rb', line 85

def initialize_url(batch_job_url)
  # Initialization is only necessary for v201601 or higher.
  return batch_job_url if [:v201506, :v201509].include?(@version)

  headers = DEFAULT_HEADERS
  headers['Content-Length'] = 0
  headers['x-goog-resumable'] = 'start'

  response = AdsCommon::Http.post_response(
      batch_job_url, '', @api.config, headers)

  return response.headers['Location']
end

#put_incremental_operations(operations, batch_job_url, total_content_length = 0, is_last_request = false) ⇒ Object

Puts the provided operations to the provided URL, allowing for incremental followup puts.

Args:

  • soap_operations: An array including SOAP operations provided by generate_soap_operations

  • batch_job_url: The UploadURL provided by BatchJobService

  • total_content_length: The total number of bytes already uploaded incrementally. Set this to 0 the first time you call the method.

  • is_last_request: Whether or not this set of uploads will conclude the full request.

Returns:

  • total content length, including what was just uploaded. Pass this back into this method on subsequent calls.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/adwords_api/batch_job_utils.rb', line 114

def put_incremental_operations(
    operations, batch_job_url, total_content_length = 0,
    is_last_request = false)
  headers = DEFAULT_HEADERS
  soap_operations = generate_soap_operations(operations)
  request_body = soap_operations.join
  is_first_request = (total_content_length == 0)

  if is_first_request
    request_body = (UPLOAD_XML_PREFIX % [@version]) + request_body
  end
  if is_last_request
    request_body += UPLOAD_XML_SUFFIX
  end

  request_body = add_padding(request_body)
  content_length = request_body.size

  headers['Content-Length'] = content_length.to_s

  lower_bound = total_content_length
  upper_bound = total_content_length + content_length - 1
  total_bytes = is_last_request ? upper_bound + 1 : '*'
  content_range = "bytes %d-%d/%s" %
      [lower_bound, upper_bound, total_bytes]
  headers['Content-Range'] = content_range

  log_request(batch_job_url, headers, request_body)

  # The HTTPI library fails to handle the response when uploading
  # incremental requests. We're not interested in the response, so just
  # ignore the error.
  begin
    AdsCommon::Http.put_response(
        batch_job_url, request_body, @api.config, headers)
  rescue ArgumentError
  end

  total_content_length += content_length
  return total_content_length
end

#start_incremental_upload(batch_job_url, uploaded_bytes = 0) ⇒ Object

Provides a helper to manage incremental uploads.

Args:

  • batch_job_url: The UploadURL provided by BatchJobService for new jobs,

or the upload_url from IncrementalUploadHelper for continued jobs.

  • uploaded_bytes: The number of bytes already uploaded for this

incremental batch job. Can be retrieved from the IncrementalUploadHelper using uploaded_bytes.

Returns:

  • an IncrementalUploadHelper that will accept operations and put them,

keeping track of uploaded bytes automatically.



71
72
73
74
# File 'lib/adwords_api/batch_job_utils.rb', line 71

def start_incremental_upload(batch_job_url, uploaded_bytes = 0)
  return AdwordsApi::IncrementalUploadHelper.new(
      self, uploaded_bytes, batch_job_url)
end

#upload_operations(operations, batch_job_url) ⇒ Object

Uploads the given operations for a batch job to the provided URL.

Args:

  • hash_operations: An array of ruby has operations to execute by posting them to the provided URL

  • service_name: The name of the AdwordsApi service as a symbol that would normally make this request

  • batch_job_url: The UploadURL provided by BatchJobService

Raises:

  • InvalidBatchJobOperationError: If there is a problem converting the

given operations to SOAP.



53
54
55
56
# File 'lib/adwords_api/batch_job_utils.rb', line 53

def upload_operations(operations, batch_job_url)
  helper = start_incremental_upload(batch_job_url)
  helper.upload(operations, true)
end