/adx_buyer_api/examples/v201109/perform_mutate_job.rb

https://code.google.com/ · Ruby · 151 lines · 94 code · 19 blank · 38 comment · 9 complexity · bff9245c7ad4444d990587c5a4cbc74c MD5 · raw file

  1. #!/usr/bin/ruby
  2. #
  3. # Author:: api.dklimkin@gmail.com (Danial Klimkin)
  4. #
  5. # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
  6. #
  7. # License:: Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  16. # implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. # This code sample illustrates how to perform asynchronous requests using the
  21. # MutateJobService.
  22. #
  23. # Tags: MutateJobService.mutate, MutateJobService.get,
  24. # MutateJobService.getResult
  25. require 'rubygems'
  26. require 'adwords_api'
  27. API_VERSION = :v201109
  28. RETRY_INTERVAL = 30
  29. RETRIES_COUNT = 30
  30. PLACEMENT_NUMBER = 100
  31. def perform_mutate_job()
  32. # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
  33. # when called without parameters.
  34. adwords = AdwordsApi::Api.new
  35. # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
  36. # the configuration file or provide your own logger:
  37. # adwords.logger = Logger.new('adwords_xml.log')
  38. mutate_job_srv = adwords.service(:MutateJobService, API_VERSION)
  39. ad_group_id = 'INSERT_AD_GROUP_ID_HERE'.to_i
  40. # Create AdGroupCriterionOperations to add placements.
  41. operations = (1..PLACEMENT_NUMBER).map do |index|
  42. url = (rand(10) == 0) ? 'invalid-url' : 'http://mars.google.com/'
  43. {:xsi_type => 'AdGroupCriterionOperation',
  44. :operator => 'ADD',
  45. :operand => {
  46. :xsi_type => 'BiddableAdGroupCriterion',
  47. :ad_group_id => ad_group_id,
  48. :criterion => {
  49. :xsi_type => 'Placement',
  50. :url => url
  51. }}}
  52. end
  53. # You can specify up to 3 job IDs that must successfully complete before
  54. # this job can be processed.
  55. policy = {:prerequisite_job_ids => []}
  56. # Call mutate to create a new job.
  57. response = mutate_job_srv.mutate(operations, policy)
  58. raise StandardError, 'Failed to submit a job; aborting.' unless response
  59. job_id = response[:id]
  60. puts "Job ID %d was successfully created." % job_id
  61. # Creating selector to retrive hob status and wait for it to complete.
  62. selector = {
  63. :xsi_type => 'BulkMutateJobSelector',
  64. :job_ids => [job_id]
  65. }
  66. status = nil
  67. # Poll for job status until it's finished.
  68. puts 'Retrieving job status...'
  69. RETRIES_COUNT.times do |retry_attempt|
  70. job_status_response = mutate_job_srv.get(selector)
  71. if job_status_response
  72. status = job_status_response.first[:status]
  73. case status
  74. when 'FAILED'
  75. raise StandardError, "Job failed with reason: '%s'" %
  76. job_status_response.first[:failure_reason]
  77. when 'COMPLETED'
  78. puts "[%d] Job finished with status '%s'" % [retry_attempt, status]
  79. break
  80. else
  81. puts "[%d] Current status is '%s', waiting %d seconds to retry..." %
  82. [retry_attempt, status, RETRY_INTERVAL]
  83. sleep(RETRY_INTERVAL)
  84. end
  85. else
  86. raise StandardError, 'Error retrieving job status; aborting.'
  87. end
  88. end
  89. if status == 'COMPLETED'
  90. # Get the job result. Here we re-use the same selector.
  91. result_response = mutate_job_srv.get_result(selector)
  92. if result_response and result_response[:simple_mutate_result]
  93. results = result_response[:simple_mutate_result][:results]
  94. if results
  95. results.each_with_index do |result, index|
  96. result_message = result.include?(:place_holder) ?
  97. 'FAILED' : 'SUCCEEDED'
  98. puts "Operation [%d] - %s" % [index, result_message]
  99. end
  100. end
  101. errors = result_response[:simple_mutate_result][:errors]
  102. if errors
  103. errors.each do |error|
  104. puts "Error, reason: '%s', trigger: '%s', field path: '%s'" %
  105. [error[:reason], error[:trigger], error[:field_path]]
  106. end
  107. end
  108. else
  109. raise StandardError, 'Error retrieving job results; aborting.'
  110. end
  111. else
  112. puts "Job failed to complete after %d retries" % RETRY_COUNT
  113. end
  114. end
  115. if __FILE__ == $0
  116. begin
  117. perform_mutate_job()
  118. # HTTP errors.
  119. rescue AdsCommon::Errors::HttpError => e
  120. puts "HTTP Error: %s" % e
  121. # API errors.
  122. rescue AdwordsApi::Errors::ApiException => e
  123. puts "Message: %s" % e.message
  124. puts 'Errors:'
  125. e.errors.each_with_index do |error, index|
  126. puts "\tError [%d]:" % (index + 1)
  127. error.each do |field, value|
  128. puts "\t\t%s: %s" % [field, value]
  129. end
  130. end
  131. end
  132. end