Skip to content

Commit

Permalink
Changes for v1 release. (#38)
Browse files Browse the repository at this point in the history
Change-Id: I10b44934bf3e8fe0ee5cd7a51d29de5cfcb8c006
  • Loading branch information
mcloonan authored Mar 5, 2019
1 parent 8e0de3e commit f90fc2d
Show file tree
Hide file tree
Showing 759 changed files with 47,948 additions and 1,101 deletions.
4 changes: 3 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
0.8.1:
1.0.0:
- Adding support for Google Ads API v1.
- Adding support for versioned path and proto lookup utils.
- Fix to logging interceptor for customer service requests.

0.8.0:
Expand Down
93 changes: 76 additions & 17 deletions lib/google/ads/google_ads/google_ads_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ module Resources
module Services
end
end
module V1
module Common
end
module Enums
end
module Errors
end
module Resources
end
module Services
end
end
end
end
end
Expand All @@ -44,8 +56,6 @@ module Services
require 'google/ads/google_ads/config'
require 'google/ads/google_ads/field_mask_util'
require 'google/ads/google_ads/wrapper_util'
require 'google/ads/google_ads/proto_lookup_util'
require 'google/ads/google_ads/path_lookup_util'
require 'google/ads/google_ads/logging_interceptor'

require 'google/ads/google_ads/errors'
Expand All @@ -59,7 +69,8 @@ module Google
module Ads
module GoogleAds
class GoogleAdsClient
API_VERSION = :V0
DEFAULT_API_VERSION = :V1
KNOWN_API_VERSIONS = [:V0, :V1]

DEFAULT_CONFIG_FILENAME = 'google_ads_config.rb'

Expand Down Expand Up @@ -92,10 +103,8 @@ def initialize(config_path = nil, &block)
end
@config = eval_result
end
@proto_lookup_util =
Google::Ads::GoogleAds::ProtoLookupUtil.new(API_VERSION)
@path_lookup_util =
Google::Ads::GoogleAds::PathLookupUtil.new(@proto_lookup_util)
@proto_lookup_utils = {}
@path_lookup_utils = {}

begin
@logger = create_default_logger
Expand All @@ -113,14 +122,14 @@ def configure(&block)
# :Campaign will return an instantiated CampaignServiceClient.
#
# Raises ArgumentError if no service can be found for the provided type.
def service(name)
def service(name, version = DEFAULT_API_VERSION)
service_path = ENV['GOOGLEADS_SERVICE_PATH']

# We need a local reference to refer to from within the class block
# below.
logger = @logger

class_to_return = @proto_lookup_util.service(name)
class_to_return = proto_lookup_util(version).service(name)
class_to_return = Class.new(class_to_return) do
unless service_path.nil? || service_path.empty?
const_set('SERVICE_ADDRESS', service_path.freeze)
Expand Down Expand Up @@ -153,25 +162,25 @@ def service(name)
# example, passing :Campaign will return an instantiated Campaign.
#
# Raises ArgumentError if no entity can be found for the provided type.
def resource(name)
@proto_lookup_util.resource(name).new
def resource(name, version = DEFAULT_API_VERSION)
proto_lookup_util(version).resource(name).new
end

# Return an operation for the provided entity type. For example, passing
# :Campaign will return an instantiated CampaignOperation.
#
# Raises ArgumentError if no entity can be found for the provided type.
def operation(name)
@proto_lookup_util.operation(name).new
def operation(name, version = DEFAULT_API_VERSION)
proto_lookup_util(version).operation(name).new
end

# Return a reference to the enum class for the provided enum type. For
# example, passing :CampaignStatus will return a reference to the
# CampaignStatusEnum.
#
# Raises ArgumentError if no enum can be found for the provided type.
def enum(name)
@proto_lookup_util.enum(name)
def enum(name, version = DEFAULT_API_VERSION)
proto_lookup_util(version).enum(name)
end

# Returns a reference to the FieldMaskUtil class for ease of access.
Expand All @@ -185,8 +194,8 @@ def wrapper()
end

# Returns a reference to the PathLookupUtil to generate resource names.
def path()
@path_lookup_util
def path(version = DEFAULT_API_VERSION)
path_lookup_util(version)
end

# Set the logger to use. This will only take effect on services fetched
Expand Down Expand Up @@ -244,6 +253,56 @@ def create_default_logger()
logger.level = Logger.const_get(@config.log_level)
return logger
end

def valid_version?(version)
KNOWN_API_VERSIONS.include?(version)
end

# Load up the proto lookup util for the given version, storing a copy
# of it if this is the first time we needed it.
def proto_lookup_util(version)
unless valid_version?(version)
raise sprintf('Unknown version %s', version)
end
if @proto_lookup_utils[version].nil?
path_version = version.downcase
require sprintf('google/ads/google_ads/utils/%s/proto_lookup_util',
path_version)
class_path = sprintf(
'Google::Ads::GoogleAds::Utils::%s::ProtoLookupUtil',
version
)
@proto_lookup_utils[version] = class_for_path(class_path).new
end
@proto_lookup_utils[version]
end

# Load up the path lookup util for the given version, storing a copy
# of it if this is the first time we needed it.
def path_lookup_util(version)
unless valid_version?(version)
raise sprintf('Unknown version %s', version)
end
if @path_lookup_utils[version].nil?
path_version = version.downcase
require sprintf('google/ads/google_ads/utils/%s/path_lookup_util',
path_version)
class_path = sprintf(
'Google::Ads::GoogleAds::Utils::%s::PathLookupUtil',
version
)
@path_lookup_utils[version] = class_for_path(class_path).new(
proto_lookup_util(version))
end
@path_lookup_utils[version]
end

# Converts complete class path into class object.
def class_for_path(path)
path.split('::').inject(Kernel) do |scope, const_name|
scope.const_get(const_name)
end
end
end
end
end
Expand Down
Loading

0 comments on commit f90fc2d

Please sign in to comment.