Permalink
Browse files

DFP v201208 examples

  • Loading branch information...
1 parent c26e636 commit fabc746bc31fd8cc4c5d54639e2291e9dec890ee @dklimkin dklimkin committed Sep 11, 2012
Showing with 12,158 additions and 0 deletions.
  1. +2 −0 dfp_api/dfp_api.yml
  2. +93 −0 dfp_api/examples/v201208/audience_segment_service/get_all_audience_segments.rb
  3. +76 −0 dfp_api/examples/v201208/common/error_handling.rb
  4. +128 −0 dfp_api/examples/v201208/common/oauth2_handling.rb
  5. +131 −0 dfp_api/examples/v201208/common/oauth_handling.rb
  6. +81 −0 dfp_api/examples/v201208/company_service/create_companies.rb
  7. +92 −0 dfp_api/examples/v201208/company_service/get_all_companies.rb
  8. +88 −0 dfp_api/examples/v201208/company_service/get_companies_by_statement.rb
  9. +72 −0 dfp_api/examples/v201208/company_service/get_company.rb
  10. +95 −0 dfp_api/examples/v201208/company_service/update_companies.rb
  11. +94 −0 dfp_api/examples/v201208/content_service/get_all_content.rb
  12. +136 −0 dfp_api/examples/v201208/content_service/get_content_by_category.rb
  13. +115 −0 dfp_api/examples/v201208/creative_service/copy_image_creatives.rb
  14. +139 −0 dfp_api/examples/v201208/creative_service/create_creative_from_template.rb
  15. +106 −0 dfp_api/examples/v201208/creative_service/create_creatives.rb
  16. +104 −0 dfp_api/examples/v201208/creative_service/create_custom_creative.rb
  17. +93 −0 dfp_api/examples/v201208/creative_service/get_all_creatives.rb
  18. +72 −0 dfp_api/examples/v201208/creative_service/get_creative.rb
  19. +87 −0 dfp_api/examples/v201208/creative_service/get_creatives_by_statement.rb
  20. +96 −0 dfp_api/examples/v201208/creative_service/update_creatives.rb
  21. +77 −0 dfp_api/examples/v201208/creative_set_service/associate_creative_set_to_line_item.rb
  22. +83 −0 dfp_api/examples/v201208/creative_set_service/create_creative_set.rb
  23. +94 −0 dfp_api/examples/v201208/creative_set_service/get_all_creative_sets.rb
  24. +90 −0 dfp_api/examples/v201208/creative_set_service/get_creative_sets_by_statement.rb
  25. +85 −0 dfp_api/examples/v201208/creative_set_service/update_creative_sets.rb
  26. +94 −0 dfp_api/examples/v201208/creative_template_service/get_all_creative_templates.rb
  27. +74 −0 dfp_api/examples/v201208/creative_template_service/get_creative_template.rb
  28. +80 −0 dfp_api/examples/v201208/creative_template_service/get_creative_templates_by_statement.rb
  29. +87 −0 dfp_api/examples/v201208/custom_field_service/create_custom_field_options.rb
  30. +86 −0 dfp_api/examples/v201208/custom_field_service/create_custom_fields.rb
  31. +112 −0 dfp_api/examples/v201208/custom_field_service/deactivate_all_line_item_custom_fields.rb
  32. +102 −0 dfp_api/examples/v201208/custom_field_service/get_all_custom_fields.rb
  33. +86 −0 dfp_api/examples/v201208/custom_field_service/get_all_line_item_custom_fields.rb
  34. +135 −0 dfp_api/examples/v201208/custom_field_service/set_line_item_custom_field_value.rb
  35. +83 −0 dfp_api/examples/v201208/custom_field_service/update_custom_fields.rb
  36. +140 −0 dfp_api/examples/v201208/custom_targeting_service/create_custom_targeting_keys_and_values.rb
  37. +121 −0 dfp_api/examples/v201208/custom_targeting_service/delete_custom_targeting_keys.rb
  38. +125 −0 dfp_api/examples/v201208/custom_targeting_service/delete_custom_targeting_values.rb
  39. +148 −0 dfp_api/examples/v201208/custom_targeting_service/get_all_custom_targeting_keys_and_values.rb
  40. +92 −0 dfp_api/examples/v201208/custom_targeting_service/get_custom_targeting_keys_by_statement.rb
  41. +98 −0 dfp_api/examples/v201208/custom_targeting_service/get_custom_targeting_values_by_statement.rb
  42. +94 −0 dfp_api/examples/v201208/custom_targeting_service/update_custom_targeting_keys.rb
  43. +106 −0 dfp_api/examples/v201208/custom_targeting_service/update_custom_targeting_values.rb
  44. +107 −0 dfp_api/examples/v201208/forecast_service/get_forecast.rb
  45. +80 −0 dfp_api/examples/v201208/forecast_service/get_forecast_by_id.rb
  46. +101 −0 dfp_api/examples/v201208/inventory_service/create_ad_units.rb
  47. +101 −0 dfp_api/examples/v201208/inventory_service/create_mobile_ad_unit.rb
  48. +108 −0 dfp_api/examples/v201208/inventory_service/create_video_ad_unit.rb
  49. +119 −0 dfp_api/examples/v201208/inventory_service/deactivate_ad_units.rb
  50. +72 −0 dfp_api/examples/v201208/inventory_service/get_ad_unit.rb
  51. +85 −0 dfp_api/examples/v201208/inventory_service/get_ad_unit_sizes.rb
  52. +96 −0 dfp_api/examples/v201208/inventory_service/get_ad_units_by_statement.rb
  53. +93 −0 dfp_api/examples/v201208/inventory_service/get_all_ad_units.rb
  54. +94 −0 dfp_api/examples/v201208/inventory_service/update_ad_units.rb
  55. +83 −0 dfp_api/examples/v201208/label_service/create_labels.rb
  56. +118 −0 dfp_api/examples/v201208/label_service/deactivate_labels.rb
  57. +95 −0 dfp_api/examples/v201208/label_service/get_all_labels.rb
  58. +76 −0 dfp_api/examples/v201208/label_service/get_label.rb
  59. +81 −0 dfp_api/examples/v201208/label_service/get_labels_by_statement.rb
  60. +102 −0 dfp_api/examples/v201208/label_service/update_labels.rb
  61. +94 −0 dfp_api/examples/v201208/line_item_creative_association_service/create_licas.rb
  62. +127 −0 dfp_api/examples/v201208/line_item_creative_association_service/deactivate_licas.rb
  63. +98 −0 dfp_api/examples/v201208/line_item_creative_association_service/get_all_licas.rb
  64. +77 −0 dfp_api/examples/v201208/line_item_creative_association_service/get_lica.rb
  65. +91 −0 dfp_api/examples/v201208/line_item_creative_association_service/get_licas_by_statement.rb
  66. +91 −0 dfp_api/examples/v201208/line_item_creative_association_service/update_licas.rb
  67. +131 −0 dfp_api/examples/v201208/line_item_service/activate_line_items.rb
  68. +177 −0 dfp_api/examples/v201208/line_item_service/create_line_items.rb
  69. +135 −0 dfp_api/examples/v201208/line_item_service/create_mobile_line_item.rb
  70. +156 −0 dfp_api/examples/v201208/line_item_service/create_video_line_item.rb
  71. +94 −0 dfp_api/examples/v201208/line_item_service/get_all_line_items.rb
  72. +74 −0 dfp_api/examples/v201208/line_item_service/get_line_item.rb
  73. +93 −0 dfp_api/examples/v201208/line_item_service/get_line_items_by_statement.rb
  74. +98 −0 dfp_api/examples/v201208/line_item_service/get_recently_updated_line_items.rb
  75. +129 −0 dfp_api/examples/v201208/line_item_service/target_custom_criteria.rb
  76. +109 −0 dfp_api/examples/v201208/line_item_service/update_line_items.rb
  77. +74 −0 dfp_api/examples/v201208/network_service/get_all_networks.rb
  78. +66 −0 dfp_api/examples/v201208/network_service/get_current_network.rb
  79. +79 −0 dfp_api/examples/v201208/network_service/make_test_network.rb
  80. +122 −0 dfp_api/examples/v201208/order_service/approve_orders.rb
  81. +92 −0 dfp_api/examples/v201208/order_service/create_orders.rb
  82. +93 −0 dfp_api/examples/v201208/order_service/get_all_orders.rb
  83. +74 −0 dfp_api/examples/v201208/order_service/get_order.rb
  84. +90 −0 dfp_api/examples/v201208/order_service/get_orders_by_statement.rb
  85. +95 −0 dfp_api/examples/v201208/order_service/update_orders.rb
  86. +131 −0 dfp_api/examples/v201208/placement_service/create_placements.rb
  87. +118 −0 dfp_api/examples/v201208/placement_service/deactivate_placements.rb
  88. +93 −0 dfp_api/examples/v201208/placement_service/get_all_placements.rb
  89. +74 −0 dfp_api/examples/v201208/placement_service/get_placement.rb
  90. +86 −0 dfp_api/examples/v201208/placement_service/get_placements_by_statement.rb
  91. +97 −0 dfp_api/examples/v201208/placement_service/update_placements.rb
  92. +90 −0 dfp_api/examples/v201208/publisher_query_language_service/get_all_cities.rb
  93. +81 −0 dfp_api/examples/v201208/report_service/download_report.rb
  94. +103 −0 dfp_api/examples/v201208/report_service/run_delivery_report.rb
  95. +96 −0 dfp_api/examples/v201208/report_service/run_inventory_report.rb
  96. +94 −0 dfp_api/examples/v201208/report_service/run_merged_delivery_report.rb
  97. +135 −0 dfp_api/examples/v201208/report_service/run_report_with_custom_fields.rb
  98. +92 −0 dfp_api/examples/v201208/report_service/run_sales_report.rb
  99. +109 −0 dfp_api/examples/v201208/suggested_ad_unit_service/approve_all_suggested_ad_units.rb
  100. +96 −0 dfp_api/examples/v201208/suggested_ad_unit_service/get_all_suggested_ad_units.rb
  101. +75 −0 dfp_api/examples/v201208/suggested_ad_unit_service/get_suggested_ad_unit.rb
  102. +92 −0 dfp_api/examples/v201208/suggested_ad_unit_service/get_suggested_ad_units_by_statement.rb
  103. +83 −0 dfp_api/examples/v201208/team_service/create_teams.rb
  104. +92 −0 dfp_api/examples/v201208/team_service/get_all_teams.rb
  105. +74 −0 dfp_api/examples/v201208/team_service/get_team.rb
  106. +78 −0 dfp_api/examples/v201208/team_service/get_teams_by_statement.rb
  107. +95 −0 dfp_api/examples/v201208/team_service/update_teams.rb
  108. +125 −0 dfp_api/examples/v201208/third_party_slot_service/archive_third_party_slots.rb
  109. +91 −0 dfp_api/examples/v201208/third_party_slot_service/create_third_party_slot.rb
  110. +93 −0 dfp_api/examples/v201208/third_party_slot_service/get_all_third_party_slots.rb
  111. +86 −0 dfp_api/examples/v201208/third_party_slot_service/get_third_party_slot_by_statement.rb
  112. +95 −0 dfp_api/examples/v201208/third_party_slot_service/update_third_party_slots.rb
  113. +89 −0 dfp_api/examples/v201208/user_service/create_users.rb
  114. +97 −0 dfp_api/examples/v201208/user_service/deactivate_users.rb
  115. +73 −0 dfp_api/examples/v201208/user_service/get_all_roles.rb
  116. +93 −0 dfp_api/examples/v201208/user_service/get_all_users.rb
  117. +66 −0 dfp_api/examples/v201208/user_service/get_current_user.rb
  118. +73 −0 dfp_api/examples/v201208/user_service/get_user.rb
  119. +79 −0 dfp_api/examples/v201208/user_service/get_users_by_statement.rb
  120. +89 −0 dfp_api/examples/v201208/user_service/update_users.rb
  121. +86 −0 dfp_api/examples/v201208/user_team_association_service/create_user_team_associations.rb
  122. +111 −0 dfp_api/examples/v201208/user_team_association_service/delete_user_team_associations.rb
  123. +94 −0 dfp_api/examples/v201208/user_team_association_service/get_all_user_team_associations.rb
  124. +76 −0 dfp_api/examples/v201208/user_team_association_service/get_user_team_association.rb
  125. +94 −0 dfp_api/examples/v201208/user_team_association_service/get_user_team_associations_by_statement.rb
  126. +102 −0 dfp_api/examples/v201208/user_team_association_service/update_user_team_associations.rb
View
@@ -1,6 +1,8 @@
---
# This is an example configuration file for the Ruby DFP API client library.
# Please fill in the required fields, and copy it over to your home directory.
+# Detailed descriptions of these properties can be found at:
+# https://developers.google.com/doubleclick-publishers/docs/soap_xml
:authentication:
# Authentication method, methods currently supported:
# OAuth2, OAuth, ClientLogin.
@@ -0,0 +1,93 @@
+#!/usr/bin/env ruby
+# Encoding: utf-8
+#
+# Author:: api.dklimkin@gmail.com (Danial Klimkin)
+#
+# Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
+#
+# License:: Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# This code example gets all audience segments.
+#
+# Tags: AudienceSegmentService.getAudienceSegmentsByStatement
+
+require 'dfp_api'
+
+API_VERSION = :v201208
+PAGE_SIZE = 500
+
+def get_all_audience_segments()
+ # Get DfpApi instance and load configuration from ~/dfp_api.yml.
+ dfp = DfpApi::Api.new
+
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
+ # the configuration file or provide your own logger:
+ # dfp.logger = Logger.new('dfp_xml.log')
+
+ # Get the AudienceSegmentService.
+ audience_segment_service = dfp.service(:AudienceSegmentService, API_VERSION)
+
+ # Define initial values.
+ offset = 0
+ page = {}
+
+ begin
+ # Create statement for one page with current offset.
+ statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
+
+ # Get audience segments by statement.
+ page =
+ audience_segment_service.get_audience_segments_by_statement(statement)
+
+ if page[:results]
+ # Increase query offset by page size.
+ offset += PAGE_SIZE
+
+ # Get the start index for printout.
+ start_index = page[:start_index]
+
+ # Print details about each audience segment in results page.
+ page[:results].each_with_index do |segment, index|
+ puts "%d) Audience segment ID: %d, name: %s" %
+ [index + start_index, segment[:id], segment[:name]]
+ end
+ end
+ end while offset < page[:total_result_set_size]
+
+ # Print a footer
+ if page.include?(:total_result_set_size)
+ puts "Total number of audience segments: %d" % page[:total_result_set_size]
+ end
+end
+
+if __FILE__ == $0
+ begin
+ get_all_audience_segments()
+
+ # HTTP errors.
+ rescue AdsCommon::Errors::HttpError => e
+ puts "HTTP Error: %s" % e
+
+ # API errors.
+ rescue DfpApi::Errors::ApiException => e
+ puts "Message: %s" % e.message
+ puts 'Errors:'
+ e.errors.each_with_index do |error, index|
+ puts "\tError [%d]:" % (index + 1)
+ error.each do |field, value|
+ puts "\t\t%s: %s" % [field, value]
+ end
+ end
+ end
+end
@@ -0,0 +1,76 @@
+#!/usr/bin/env ruby
+# Encoding: utf-8
+#
+# Author:: api.dklimkin@gmail.com (Danial Klimkin)
+#
+# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
+#
+# License:: Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# This example runs incorrect query and demonstrates how to handle errors.
+#
+# Tags: UserService.updateUser
+
+require 'dfp_api'
+
+API_VERSION = :v201208
+
+def produce_api_error()
+ # Get DfpApi instance and load configuration from ~/dfp_api.yml.
+ dfp = DfpApi::Api.new
+
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
+ # the configuration file or provide your own logger:
+ # dfp.logger = Logger.new('dfp_xml.log')
+
+ # Get the UserService.
+ user_service = dfp.service(:UserService, API_VERSION)
+
+ # Omitting "id" field here to produce an error.
+ user = {:preferred_locale => 'en_UK', :name => 'foo_bar'}
+
+ # Execute request and get the response, this should raise an exception.
+ user = user_service.update_user(user)
+
+ # Output retrieved data.
+ puts "User ID: %d, name: %s, email: %s" %
+ [user[:id], user[:name], user[:email]]
+end
+
+if __FILE__ == $0
+ begin
+ # This function should produce an exception for demo.
+ produce_api_error()
+
+ # One of two kinds of exception might occur, general HTTP error like 403 or
+ # 404 and DFP API error defined in WSDL and described in documentation.
+
+ # Handling HTTP errors.
+ rescue AdsCommon::Errors::HttpError => e
+ puts "HTTP Error: %s" % e
+
+ # Handling API errors.
+ rescue DfpApi::Errors::ApiException => e
+ # Standard DFP API error includes message and array of errors occured.
+ puts "Message: %s" % e.message
+ puts 'Errors:'
+ # Print out each of the errors.
+ e.errors.each_with_index do |error, index|
+ puts "\tError [%d]:" % (index + 1)
+ error.each do |field, value|
+ puts "\t\t%s: %s" % [field, value]
+ end
+ end
+ end
+end
@@ -0,0 +1,128 @@
+#!/usr/bin/env ruby
+# Encoding: utf-8
+#
+# Author:: api.dklimkin@gmail.com (Danial Klimkin)
+#
+# Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
+#
+# License:: Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# This example shows how to use OAuth2.0 authorization method. It is designed to
+# be run from console and requires user input.
+#
+# Tags: UserService.getUsersByStatement
+
+require 'dfp_api'
+
+API_VERSION = :v201208
+PAGE_SIZE = 500
+MAX_RETRIES = 3
+
+def oauth2_handling()
+ # Get DfpApi instance and load configuration from ~/dfp_api.yml.
+ dfp = DfpApi::Api.new
+
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
+ # the configuration file or provide your own logger:
+ # dfp.logger = Logger.new('dfp_xml.log')
+
+ # Forcing authorization. A callback URL and other parameters could be
+ # specified as parameter for OAuth method.
+ token = dfp.authorize({:oauth_callback => 'oob'}) do |oauth_url|
+ # For command-line we ask user to go to URL and type in code.
+ verification_code = get_verification_code(oauth_url)
+ # Return verification code from the block.
+ (verification_code.empty?) ? nil : verification_code
+ end
+
+ # Get the UserService.
+ user_service = dfp.service(:UserService, API_VERSION)
+
+ # Define initial values.
+ offset = 0
+ page = Hash.new
+ retry_count = 0
+
+ begin
+ # Create statement for one page with current offset.
+ statement = {:query => "LIMIT %d OFFSET %d" % [PAGE_SIZE, offset]}
+
+ begin
+ # Get users by statement.
+ page = user_service.get_users_by_statement(statement)
+
+ # The second way to do OAuth authentication is to make a request and catch
+ # the OAuthVerificationRequired exception. Add the verification code to the
+ # credentials once acquired.
+ rescue AdsCommon::Errors::OAuth2VerificationRequired => e
+ if retry_count < MAX_RETRIES
+ verification_code = get_verification_code(e.oauth_url)
+ dfp.credential_handler.set_credential(
+ :oauth2_verification_code, verification_code)
+ retry_count += 1
+ retry
+ else
+ raise AdsCommon::Errors::AuthError, 'Failed to authenticate.'
+ end
+ end
+
+ if page[:results]
+ # Increase query offset by page size.
+ offset += PAGE_SIZE
+
+ # Get the start index for printout.
+ start_index = page[:start_index]
+
+ # Print details about each user in results page.
+ page[:results].each_with_index do |user, index|
+ puts "%d) User ID: %d, name: %s, email: %s" %
+ [index + start_index, user[:id], user[:name], user[:email]]
+ end
+ end
+ end while offset < page[:total_result_set_size]
+
+ # Print a footer
+ if page.include?(:total_result_set_size)
+ puts "Total number of users: %d" % page[:total_result_set_size]
+ end
+end
+
+# Misc util to get the verification code from the console.
+def get_verification_code(url)
+ puts "Hit Auth error, please navigate to URL:\n\t%s" % url
+ print 'Log in and type the verification code: '
+ verification_code = gets.chomp
+ return verification_code
+end
+
+if __FILE__ == $0
+ begin
+ oauth2_handling()
+
+ # HTTP errors.
+ rescue AdsCommon::Errors::HttpError => e
+ puts "HTTP Error: %s" % e
+
+ # API errors.
+ rescue DfpApi::Errors::ApiException => e
+ puts "Message: %s" % e.message
+ puts 'Errors:'
+ e.errors.each_with_index do |error, index|
+ puts "\tError [%d]:" % (index + 1)
+ error.each do |field, value|
+ puts "\t\t%s: %s" % [field, value]
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit fabc746

Please sign in to comment.