Permalink
Browse files

DFP API v201206 support

  • Loading branch information...
1 parent be15ef1 commit a87fb13d9c22f01c46a9d0831ddd3275b5b80509 @dklimkin dklimkin committed Jul 24, 2012
Showing 362 changed files with 14,396 additions and 197 deletions.
View
@@ -1,3 +1,6 @@
+0.3.3:
+ - Added support and examples for v201206.
+
0.3.2:
- Require google-ads-common 0.7.3 or later from now on.
- Support for OAuth2.0 authentication method.
View
@@ -112,7 +112,7 @@ Once the library object is created you can request services with a 'service'
method:
user_service = dfp.service(:UserService, <API_VERSION>)
-where <API_VERSION> is required version of DFP API as symbol, e.g. :v201204.
+where <API_VERSION> is required version of DFP API as symbol, e.g. :v201206.
Then you should be able to execute service methods:
@@ -146,7 +146,7 @@ Examples can be run by executing by running:
from the "examples/<version>/<service_name>" directory i.e.
- $ cd examples/v201204/user_service
+ $ cd examples/v201206/user_service
$ ruby get_all_users.rb
Some examples require modification to be functional, like create_order example
@@ -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 = :v201206
+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 = :v201206
+
+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 = :v201206
+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 a87fb13

Please sign in to comment.