Skip to content

Commit

Permalink
Client builders for REST APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Jun 14, 2019
1 parent e2d1e2f commit 5c5afff
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 24 deletions.
8 changes: 0 additions & 8 deletions NuGet.Config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ namespace Google.Cloud.BigQuery.V2
/// </remarks>
public abstract partial class BigQueryClient : IDisposable
{
private static readonly ScopedCredentialProvider _credentialProvider = new ScopedCredentialProvider(
new[] {
internal static ScopedCredentialProvider ScopedCredentialProvider { get; } =
new ScopedCredentialProvider(new[] {
BigqueryService.Scope.Bigquery,
BigqueryService.Scope.BigqueryInsertdata,
BigqueryService.Scope.DevstorageFullControl,
Expand Down Expand Up @@ -95,7 +95,7 @@ public abstract partial class BigQueryClient : IDisposable
public static async Task<BigQueryClient> CreateAsync(string projectId, GoogleCredential credential = null)
{
GaxPreconditions.CheckNotNull(projectId, nameof(projectId));
var scopedCredentials = await _credentialProvider.GetCredentialsAsync(credential).ConfigureAwait(false);
var scopedCredentials = await ScopedCredentialProvider.GetCredentialsAsync(credential).ConfigureAwait(false);
return CreateImpl(projectId, scopedCredentials);
}

Expand All @@ -112,7 +112,7 @@ public static async Task<BigQueryClient> CreateAsync(string projectId, GoogleCre
public static BigQueryClient Create(string projectId, GoogleCredential credential = null)
{
GaxPreconditions.CheckNotNull(projectId, nameof(projectId));
var scopedCredentials = _credentialProvider.GetCredentials(credential);
var scopedCredentials = ScopedCredentialProvider.GetCredentials(credential);
return CreateImpl(projectId, scopedCredentials);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2019 Google LLC
//
// 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
//
// https://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.

using Google.Api.Gax;
using Google.Api.Gax.Rest;
using Google.Apis.Bigquery.v2;
using Google.Apis.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Google.Cloud.BigQuery.V2
{
/// <summary>
/// A builder for <see cref="BigQueryClient"/>, allowing simple client customization.
/// </summary>
public sealed class BigQueryClientBuilder : ClientBuilderBase<BigQueryClient>
{
/// <summary>
/// The project ID, which must be set before building the client.
/// </summary>
public string ProjectId { get; set; }

/// <summary>
/// The default location to use for location-specific operations.
/// </summary>
public string DefaultLocation { get; set; }

/// <inheritdoc />
public override BigQueryClient Build()
{
Validate();
var initializer = CreateServiceInitializer();
initializer.Serializer = new NewtonsoftJsonSerializer(BigQueryClient.CreateJsonSerializersSettings());
var service = new BigqueryService(initializer);
return new BigQueryClientImpl(ProjectId, service, DefaultLocation);
}

/// <inheritdoc />
public override async Task<BigQueryClient> BuildAsync(CancellationToken cancellationToken = default)
{
Validate();
var initializer = await CreateServiceInitializerAsync(cancellationToken).ConfigureAwait(false);
initializer.Serializer = new NewtonsoftJsonSerializer(BigQueryClient.CreateJsonSerializersSettings());
var service = new BigqueryService(initializer);
return new BigQueryClientImpl(ProjectId, service, DefaultLocation);
}

/// <inheritdoc />
protected override string GetDefaultApplicationName() => BigQueryClientImpl.ApplicationName;

/// <inheritdoc />
protected override ScopedCredentialProvider GetScopedCredentialProvider() => BigQueryClient.ScopedCredentialProvider;

/// <inheritdoc />
protected override void Validate()
{
base.Validate();
GaxPreconditions.CheckState(ProjectId != null, "The project ID must be set before building a client.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ namespace Google.Cloud.Storage.V1
/// </remarks>
public abstract partial class StorageClient : IDisposable
{
private static readonly ScopedCredentialProvider _credentialProvider = new ScopedCredentialProvider(
new[] { StorageService.Scope.DevstorageFullControl });
internal static ScopedCredentialProvider ScopedCredentialProvider { get; } =
new ScopedCredentialProvider(new[] { StorageService.Scope.DevstorageFullControl });

/// <summary>
/// The underlying storage service object used by this client.
Expand Down Expand Up @@ -79,7 +79,7 @@ public abstract partial class StorageClient : IDisposable
/// <returns>The task representing the created <see cref="StorageClient"/>.</returns>
public static async Task<StorageClient> CreateAsync(GoogleCredential credential = null, EncryptionKey encryptionKey = null)
{
var scopedCredentials = await _credentialProvider.GetCredentialsAsync(credential).ConfigureAwait(false);
var scopedCredentials = await ScopedCredentialProvider.GetCredentialsAsync(credential).ConfigureAwait(false);
return CreateImpl(scopedCredentials, encryptionKey);
}

Expand All @@ -95,7 +95,7 @@ public static async Task<StorageClient> CreateAsync(GoogleCredential credential
/// <returns>The created <see cref="StorageClient"/>.</returns>
public static StorageClient Create(GoogleCredential credential = null, EncryptionKey encryptionKey = null)
{
var scopedCredentials = _credentialProvider.GetCredentials(credential);
var scopedCredentials = ScopedCredentialProvider.GetCredentials(credential);
return CreateImpl(scopedCredentials, encryptionKey);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2019 Google LLC
//
// 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
//
// https://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.

using Google.Api.Gax;
using Google.Api.Gax.Rest;
using Google.Apis.Http;
using Google.Apis.Storage.v1;
using System.Threading;
using System.Threading.Tasks;

namespace Google.Cloud.Storage.V1
{
/// <summary>
/// A builder for <see cref="StorageClient"/>, allowing simple client customization.
/// </summary>
public sealed class StorageClientBuilder : ClientBuilderBase<StorageClient>
{
/// <summary>
/// If set to true, no credentials are created when the client is built.
/// </summary>
public bool UnauthenticatedAccess { get; set; }

/// <summary>
/// The customer-supplied encryption key used by default for all relevant object-based operations.
/// </summary>
public EncryptionKey EncryptionKey { get; set; }

/// <inheritdoc />
public override StorageClient Build()
{
Validate();
var initializer = CreateServiceInitializer();
var service = new StorageService(initializer);
return new StorageClientImpl(service, EncryptionKey);
}

/// <inheritdoc />
public override async Task<StorageClient> BuildAsync(CancellationToken cancellationToken = default)
{
Validate();
var initializer = await CreateServiceInitializerAsync(cancellationToken).ConfigureAwait(false);
var service = new StorageService(initializer);
return new StorageClientImpl(service, EncryptionKey);
}

/// <inheritdoc />
protected override IConfigurableHttpClientInitializer GetHttpClientInitializer() =>
UnauthenticatedAccess ? null : base.GetHttpClientInitializer();

/// <inheritdoc />
protected override Task<IConfigurableHttpClientInitializer> GetHttpClientInitializerAsync(CancellationToken cancellationToken) =>
UnauthenticatedAccess ? Task.FromResult<IConfigurableHttpClientInitializer>(null): base.GetHttpClientInitializerAsync(cancellationToken);

/// <inheritdoc />
protected override string GetDefaultApplicationName() => StorageClientImpl.ApplicationName;

/// <inheritdoc />
protected override ScopedCredentialProvider GetScopedCredentialProvider() => StorageClient.ScopedCredentialProvider;

/// <inheritdoc />
protected override void Validate()
{
base.Validate();
GaxPreconditions.CheckState(!UnauthenticatedAccess || (ApiKey == null && Credential == null && CredentialsPath == null && JsonCredentials == null),
"When requesting unauthenticated access, don't specify any other credentials.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ namespace Google.Cloud.Translation.V2
/// </remarks>
public abstract class AdvancedTranslationClient : IDisposable
{
private static readonly ScopedCredentialProvider _credentialProvider =
new ScopedCredentialProvider(new[] { TranslateService.Scope.CloudPlatform });

/// <summary>
/// The underlying translation service object used by this client.
/// </summary>
Expand Down Expand Up @@ -324,7 +321,7 @@ public virtual Task<IList<Language>> ListLanguagesAsync(string target = null, st
/// <returns>The task representing the created <see cref="AdvancedTranslationClient"/>.</returns>
public static async Task<AdvancedTranslationClient> CreateAsync(GoogleCredential credential = null, string model = null)
{
var scopedCredentials = await _credentialProvider.GetCredentialsAsync(credential).ConfigureAwait(false);
var scopedCredentials = await TranslationClient.ScopedCredentialProvider.GetCredentialsAsync(credential).ConfigureAwait(false);
return CreateImpl(scopedCredentials, null, model);
}

Expand Down Expand Up @@ -356,7 +353,7 @@ public static AdvancedTranslationClient CreateFromApiKey(string apiKey, string m
/// <returns>The created <see cref="AdvancedTranslationClient"/>.</returns>
public static AdvancedTranslationClient Create(GoogleCredential credential = null, string model = null)
{
var scopedCredentials = _credentialProvider.GetCredentials(credential);
var scopedCredentials = TranslationClient.ScopedCredentialProvider.GetCredentials(credential);
return CreateImpl(scopedCredentials, null, model);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2019 Google LLC
//
// 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
//
// https://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.

using Google.Api.Gax.Rest;
using Google.Apis.Translate.v2;
using System.Threading;
using System.Threading.Tasks;

namespace Google.Cloud.Translation.V2
{
/// <summary>
/// A builder for <see cref="AdvancedTranslationClient"/>, allowing simple client customization.
/// </summary>
public sealed class AdvancedTranslationClientBuilder : ClientBuilderBase<AdvancedTranslationClient>
{
/// <summary>
/// The translation model to use.
/// </summary>
public string Model { get; set; }

/// <inheritdoc />
public override AdvancedTranslationClient Build()
{
Validate();
var initializer = CreateServiceInitializer();
var service = new TranslateService(initializer);
return new AdvancedTranslationClientImpl(service, Model);
}

/// <inheritdoc />
public override async Task<AdvancedTranslationClient> BuildAsync(CancellationToken cancellationToken = default)
{
Validate();
var initializer = await CreateServiceInitializerAsync(cancellationToken).ConfigureAwait(false);
var service = new TranslateService(initializer);
return new AdvancedTranslationClientImpl(service, Model);
}

/// <inheritdoc />
protected override string GetDefaultApplicationName() => AdvancedTranslationClientImpl.ApplicationName;

/// <inheritdoc />
protected override ScopedCredentialProvider GetScopedCredentialProvider() => TranslationClient.ScopedCredentialProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace Google.Cloud.Translation.V2
/// </remarks>
public abstract class TranslationClient : IDisposable
{
private static readonly ScopedCredentialProvider _credentialProvider =
internal static ScopedCredentialProvider ScopedCredentialProvider { get; } =
new ScopedCredentialProvider(new[] { TranslateService.Scope.CloudPlatform });

/// <summary>
Expand Down Expand Up @@ -322,7 +322,7 @@ public virtual Task<IList<Language>> ListLanguagesAsync(string target = null, Tr
/// <returns>The task representing the created <see cref="TranslationClient"/>.</returns>
public static async Task<TranslationClient> CreateAsync(GoogleCredential credential = null, TranslationModel model = TranslationModel.ServiceDefault)
{
var scopedCredentials = await _credentialProvider.GetCredentialsAsync(credential).ConfigureAwait(false);
var scopedCredentials = await ScopedCredentialProvider.GetCredentialsAsync(credential).ConfigureAwait(false);
return CreateImpl(scopedCredentials, null, model);
}

Expand Down Expand Up @@ -354,7 +354,7 @@ public static TranslationClient CreateFromApiKey(string apiKey, TranslationModel
/// <returns>The created <see cref="TranslationClient"/>.</returns>
public static TranslationClient Create(GoogleCredential credential = null, TranslationModel model = TranslationModel.ServiceDefault)
{
var scopedCredentials = _credentialProvider.GetCredentials(credential);
var scopedCredentials = ScopedCredentialProvider.GetCredentials(credential);
return CreateImpl(scopedCredentials, null, model);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2019 Google LLC
//
// 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
//
// https://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.

using Google.Api.Gax.Rest;
using Google.Apis.Translate.v2;
using System.Threading;
using System.Threading.Tasks;

namespace Google.Cloud.Translation.V2
{
/// <summary>
/// A builder for <see cref="TranslationClient"/>, allowing simple client customization.
/// </summary>
public sealed class TranslationClientBuilder : ClientBuilderBase<TranslationClient>
{
/// <summary>
/// The translation model to use, defaulting to <see cref="TranslationModel.ServiceDefault"/>.
/// </summary>
public TranslationModel TranslationModel { get; set; } = TranslationModel.ServiceDefault;

/// <inheritdoc />
public override TranslationClient Build()
{
Validate();
var initializer = CreateServiceInitializer();
var service = new TranslateService(initializer);
return new TranslationClientImpl(service, TranslationModel);
}

/// <inheritdoc />
public override async Task<TranslationClient> BuildAsync(CancellationToken cancellationToken = default)
{
Validate();
var initializer = await CreateServiceInitializerAsync(cancellationToken).ConfigureAwait(false);
var service = new TranslateService(initializer);
return new TranslationClientImpl(service, TranslationModel);
}

/// <inheritdoc />
protected override string GetDefaultApplicationName() => TranslationClientImpl.ApplicationName;

/// <inheritdoc />
protected override ScopedCredentialProvider GetScopedCredentialProvider() => TranslationClient.ScopedCredentialProvider;
}
}

0 comments on commit 5c5afff

Please sign in to comment.