Skip to content

Commit

Permalink
Merge pull request #587 from googleads/fix-dependency-injection
Browse files Browse the repository at this point in the history
Move the dependency injection classes under Google.Ads.GoogleAds.Exte…
  • Loading branch information
Raibaz authored Dec 5, 2024
2 parents 8ce3190 + cb0c734 commit 6352a6c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 29 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
namespace Google.Ads.GoogleAds.Extensions.DependencyInjection
{
/// <summary>
/// Google Ads API options used when injecting the configuration in a DI container.
/// </summary>
public class GoogleAdsApiOptions
{
/// <summary>
/// The GoogleAdsApi prefix for configuration options.
/// </summary>
public const string GoogleAdsApi = "GoogleAdsApi";

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Google.Ads.GoogleAds.Config;
using Google.Ads.GoogleAds.Extensions.DependencyInjection;
using Google.Ads.GoogleAds.Lib;
Expand All @@ -6,6 +7,9 @@

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension functions to add the Google Ads configuration to a dependency injection container.
/// </summary>
public static class GoogleAdsClientServiceCollectionExtensions
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using Google.Ads.Gax.Config;
using Microsoft.Extensions.Options;

namespace Google.Ads.GoogleAds.Extensions.DependencyInjection
{
public class GoogleAdsConfig : Config.GoogleAdsConfig
/// <summary>
/// Configuration options to be injected in a DI container.
/// </summary>
public class GoogleAdsConfig : GoogleAds.Config.GoogleAdsConfig
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using Google.Ads.GoogleAds.Lib;
using Google.Ads.GoogleAds.V14.Services;
using System;
using System.IO;
using Google.Ads.GoogleAds.Lib;
using Google.Ads.GoogleAds.V18.Services;
using Grpc.Auth;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;


namespace Google.Ads.GoogleAds.Extensions.DependencyInjection.Tests
{
Expand All @@ -14,13 +19,13 @@ public class TestGoogleAdsClientServiceCollectionExtensions
{
private IGoogleAdsClient googleAdsClient;

[SetUp]
public void Setup()
[Test]
public void TestGetGoogleAdsClientFromDIContainer()
{
// read the configuration from appsettings.json
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

IConfigurationRoot Configuration = builder.Build();
IServiceCollection services = new ServiceCollection();
Expand All @@ -31,29 +36,15 @@ public void Setup()

// get the configured Google Ads Client from the DI container
googleAdsClient = serviceProvider.GetService<IGoogleAdsClient>();
}

[Test]
public async Task TestAddGoogleAdsClientAsync()
{
string customerId = "987654543";
long adId = 123456789012;
string query =
"SELECT ad_group_ad.ad.id " +
"FROM ad_group_ad " +
$"WHERE ad_group_ad.ad.id={adId} LIMIT 1";

GoogleAdsServiceClient googleAdsServiceClient = googleAdsClient.GetService(Services.V14.GoogleAdsService);

Task t = googleAdsServiceClient.SearchStreamAsync(customerId, query,
delegate (SearchGoogleAdsStreamResponse resp)
{
foreach (GoogleAdsRow googleAdsRow in resp.Results)
{
Assert.AreEqual(googleAdsRow.AdGroupAd.Ad.Id, adId);
}
});
await t;
GoogleAdsServiceClient googleAdsService = googleAdsClient.GetService(
Services.V18.GoogleAdsService);

Assert.AreEqual("abcdefghijkl1234567890", googleAdsService.ServiceContext.Client.Config.DeveloperToken);
Assert.AreEqual("TEST_OAUTH2_CLIENT_ID", googleAdsService.ServiceContext.Client.Config.OAuth2ClientId);
Assert.AreEqual("TEST_OAUTH2_CLIENT_SECRET", googleAdsService.ServiceContext.Client.Config.OAuth2ClientSecret);
Assert.AreEqual("TEST_OAUTH2_REFRESH_TOKEN", googleAdsService.ServiceContext.Client.Config.OAuth2RefreshToken);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@
<LastGenOutput>TestResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Condition="!Exists('..\..\Google.Ads.GoogleAds\src\Google.Ads.GoogleAds.csproj')" Include="Google.Ads.GoogleAds" Version="21.1.1" />
<ProjectReference Condition="Exists('..\..\Google.Ads.GoogleAds\src\Google.Ads.GoogleAds.csproj')" Include="..\..\Google.Ads.GoogleAds\src\Google.Ads.GoogleAds.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

0 comments on commit 6352a6c

Please sign in to comment.