Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResponseFactory: Add a response factory to the public API #1453

Merged
merged 7 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions Microsoft.Azure.Cosmos/src/CosmosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ internal CosmosClient(
/// </summary>
public virtual CosmosClientOptions ClientOptions => this.ClientContext.ClientOptions;
j82w marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The response factory used to initialize CosmosClient response types
j82w marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public virtual CosmosResponseFactory ResponseFactory => this.ClientContext.ResponseFactory;
j82w marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets the endpoint Uri for the Azure Cosmos DB service.
/// </summary>
Expand Down Expand Up @@ -474,7 +479,7 @@ virtual Task<DatabaseResponse> CreateDatabaseIfNotExistsAsync(

if (readResponse.StatusCode != HttpStatusCode.NotFound)
{
return await this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(database, Task.FromResult(readResponse));
return this.ClientContext.ResponseFactory.CreateDatabaseResponse(database, readResponse);
}

ResponseMessage createResponse = await this.CreateDatabaseStreamAsync(databaseProperties, throughputProperties, requestOptions, cancellationToken);
Expand All @@ -483,7 +488,7 @@ virtual Task<DatabaseResponse> CreateDatabaseIfNotExistsAsync(
createResponse.DiagnosticsContext.AddDiagnosticsInternal(readResponse.DiagnosticsContext);
if (createResponse.StatusCode != HttpStatusCode.Conflict)
{
return await this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(this.GetDatabase(databaseProperties.Id), Task.FromResult(createResponse));
return this.ClientContext.ResponseFactory.CreateDatabaseResponse(this.GetDatabase(databaseProperties.Id), createResponse);
}

// This second Read is to handle the race condition when 2 or more threads have Read the database and only one succeeds with Create
Expand All @@ -492,7 +497,7 @@ virtual Task<DatabaseResponse> CreateDatabaseIfNotExistsAsync(
requestOptions: requestOptions,
cancellationToken: cancellationToken);
readResponseAfterConflict.DiagnosticsContext.AddDiagnosticsInternal(readResponse.DiagnosticsContext);
return await this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(this.GetDatabase(databaseProperties.Id), Task.FromResult(readResponseAfterConflict));
return this.ClientContext.ResponseFactory.CreateDatabaseResponse(this.GetDatabase(databaseProperties.Id), readResponseAfterConflict);
});
}

Expand Down Expand Up @@ -840,13 +845,13 @@ internal virtual Task<ResponseMessage> CreateDatabaseStreamAsync(
cancellationToken));
}

internal Task<DatabaseResponse> CreateDatabaseAsync(
internal async Task<DatabaseResponse> CreateDatabaseAsync(
DatabaseProperties databaseProperties,
ThroughputProperties throughputProperties,
RequestOptions requestOptions = null,
CancellationToken cancellationToken = default(CancellationToken))
{
Task<ResponseMessage> response = this.ClientContext.ProcessResourceOperationStreamAsync(
ResponseMessage response = await this.ClientContext.ProcessResourceOperationStreamAsync(
resourceUri: this.DatabaseRootUri,
resourceType: ResourceType.Database,
operationType: OperationType.Create,
Expand All @@ -858,22 +863,22 @@ internal Task<DatabaseResponse> CreateDatabaseAsync(
diagnosticsContext: null,
cancellationToken: cancellationToken);

return this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(this.GetDatabase(databaseProperties.Id), response);
return this.ClientContext.ResponseFactory.CreateDatabaseResponse(this.GetDatabase(databaseProperties.Id), response);
}

internal Task<DatabaseResponse> CreateDatabaseAsync(
internal async Task<DatabaseResponse> CreateDatabaseAsync(
DatabaseProperties databaseProperties,
int? throughput = null,
RequestOptions requestOptions = null,
CancellationToken cancellationToken = default(CancellationToken))
{
Task<ResponseMessage> response = this.CreateDatabaseStreamInternalAsync(
ResponseMessage response = await this.CreateDatabaseStreamInternalAsync(
streamPayload: this.ClientContext.SerializerCore.ToStream<DatabaseProperties>(databaseProperties),
throughput: throughput,
requestOptions: requestOptions,
cancellationToken: cancellationToken);

return this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(this.GetDatabase(databaseProperties.Id), response);
return this.ClientContext.ResponseFactory.CreateDatabaseResponse(this.GetDatabase(databaseProperties.Id), response);
}

private Task<ResponseMessage> CreateDatabaseStreamInternalAsync(
Expand Down
6 changes: 3 additions & 3 deletions Microsoft.Azure.Cosmos/src/Linq/CosmosLinqQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ internal sealed class CosmosLinqQuery<T> : IDocumentQuery<T>, IOrderedQueryable<

private readonly ContainerInternal container;
private readonly CosmosQueryClientCore queryClient;
private readonly CosmosResponseFactory responseFactory;
private readonly CosmosResponseFactoryInternal responseFactory;
private readonly QueryRequestOptions cosmosQueryRequestOptions;
private readonly bool allowSynchronousQueryExecution = false;
private readonly string continuationToken;
private readonly CosmosSerializationOptions serializationOptions;

public CosmosLinqQuery(
ContainerInternal container,
CosmosResponseFactory responseFactory,
CosmosResponseFactoryInternal responseFactory,
CosmosQueryClientCore queryClient,
string continuationToken,
QueryRequestOptions cosmosQueryRequestOptions,
Expand Down Expand Up @@ -66,7 +66,7 @@ public CosmosLinqQuery(

public CosmosLinqQuery(
ContainerInternal container,
CosmosResponseFactory responseFactory,
CosmosResponseFactoryInternal responseFactory,
CosmosQueryClientCore queryClient,
string continuationToken,
QueryRequestOptions cosmosQueryRequestOptions,
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/Linq/CosmosLinqQueryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class CosmosLinqQueryProvider : IQueryProvider
{
private readonly ContainerInternal container;
private readonly CosmosQueryClientCore queryClient;
private readonly CosmosResponseFactory responseFactory;
private readonly CosmosResponseFactoryInternal responseFactory;
private readonly QueryRequestOptions cosmosQueryRequestOptions;
private readonly bool allowSynchronousQueryExecution;
private readonly Action<IQueryable> onExecuteScalarQueryCallback;
Expand All @@ -27,7 +27,7 @@ internal sealed class CosmosLinqQueryProvider : IQueryProvider

public CosmosLinqQueryProvider(
ContainerInternal container,
CosmosResponseFactory responseFactory,
CosmosResponseFactoryInternal responseFactory,
CosmosQueryClientCore queryClient,
string continuationToken,
QueryRequestOptions cosmosQueryRequestOptions,
Expand Down
8 changes: 4 additions & 4 deletions Microsoft.Azure.Cosmos/src/Resource/ClientContextCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class ClientContextCore : CosmosClientContext
private readonly CosmosClient client;
private readonly DocumentClient documentClient;
private readonly CosmosSerializerCore serializerCore;
private readonly CosmosResponseFactory responseFactory;
private readonly CosmosResponseFactoryInternal responseFactory;
private readonly RequestInvokerHandler requestHandler;
private readonly CosmosClientOptions clientOptions;
private readonly string userAgent;
Expand All @@ -34,7 +34,7 @@ private ClientContextCore(
CosmosClient client,
CosmosClientOptions clientOptions,
CosmosSerializerCore serializerCore,
CosmosResponseFactory cosmosResponseFactory,
CosmosResponseFactoryInternal cosmosResponseFactory,
RequestInvokerHandler requestHandler,
DocumentClient documentClient,
string userAgent,
Expand Down Expand Up @@ -115,7 +115,7 @@ internal static CosmosClientContext Create(
clientOptions.Serializer,
clientOptions.SerializerOptions);

CosmosResponseFactory responseFactory = new CosmosResponseFactory(serializerCore);
CosmosResponseFactoryInternal responseFactory = new CosmosResponseFactoryCore(serializerCore);

return new ClientContextCore(
client: cosmosClient,
Expand All @@ -138,7 +138,7 @@ internal static CosmosClientContext Create(

internal override CosmosSerializerCore SerializerCore => this.ThrowIfDisposed(this.serializerCore);

internal override CosmosResponseFactory ResponseFactory => this.ThrowIfDisposed(this.responseFactory);
internal override CosmosResponseFactoryInternal ResponseFactory => this.ThrowIfDisposed(this.responseFactory);

internal override RequestInvokerHandler RequestHandler => this.ThrowIfDisposed(this.requestHandler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public override async Task<ItemResponse<T>> ReadCurrentAsync<T>(
uriPathSegment: Paths.DocumentsPathSegment,
id: cosmosConflict.SourceResourceId);

Task<ResponseMessage> response = this.clientContext.ProcessResourceOperationStreamAsync(
ResponseMessage response = await this.clientContext.ProcessResourceOperationStreamAsync(
resourceUri: itemLink,
resourceType: ResourceType.Document,
operationType: OperationType.Read,
Expand All @@ -173,7 +173,7 @@ public override async Task<ItemResponse<T>> ReadCurrentAsync<T>(
diagnosticsContext: null,
cancellationToken: cancellationToken);

return await this.clientContext.ResponseFactory.CreateItemResponseAsync<T>(response);
return this.clientContext.ResponseFactory.CreateItemResponse<T>(response);
}

public override T ReadConflictContent<T>(ConflictProperties cosmosConflict)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public override async Task<ItemResponse<T>> CreateItemAsync<T>(
CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);
using (diagnosticsContext.GetOverallScope())
{
Task<ResponseMessage> response = this.ExtractPartitionKeyAndProcessItemStreamAsync(
ResponseMessage response = await this.ExtractPartitionKeyAndProcessItemStreamAsync(
partitionKey: partitionKey,
itemId: null,
item: item,
Expand All @@ -81,7 +81,7 @@ public override async Task<ItemResponse<T>> CreateItemAsync<T>(
diagnosticsContext: diagnosticsContext,
cancellationToken: cancellationToken);

return await this.ClientContext.ResponseFactory.CreateItemResponseAsync<T>(response);
return this.ClientContext.ResponseFactory.CreateItemResponse<T>(response);
}
}

Expand Down Expand Up @@ -114,7 +114,7 @@ public override async Task<ItemResponse<T>> ReadItemAsync<T>(
CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);
using (diagnosticsContext.GetOverallScope())
{
Task<ResponseMessage> response = this.ProcessItemStreamAsync(
ResponseMessage response = await this.ProcessItemStreamAsync(
partitionKey: partitionKey,
itemId: id,
streamPayload: null,
Expand All @@ -123,7 +123,7 @@ public override async Task<ItemResponse<T>> ReadItemAsync<T>(
diagnosticsContext: diagnosticsContext,
cancellationToken: cancellationToken);

return await this.ClientContext.ResponseFactory.CreateItemResponseAsync<T>(response);
return this.ClientContext.ResponseFactory.CreateItemResponse<T>(response);
}
}

Expand Down Expand Up @@ -161,7 +161,7 @@ public override async Task<ItemResponse<T>> UpsertItemAsync<T>(
CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);
using (diagnosticsContext.GetOverallScope())
{
Task<ResponseMessage> response = this.ExtractPartitionKeyAndProcessItemStreamAsync(
ResponseMessage response = await this.ExtractPartitionKeyAndProcessItemStreamAsync(
partitionKey: partitionKey,
itemId: null,
item: item,
Expand All @@ -170,7 +170,7 @@ public override async Task<ItemResponse<T>> UpsertItemAsync<T>(
diagnosticsContext: diagnosticsContext,
cancellationToken: cancellationToken);

return await this.ClientContext.ResponseFactory.CreateItemResponseAsync<T>(response);
return this.ClientContext.ResponseFactory.CreateItemResponse<T>(response);
}
}

Expand Down Expand Up @@ -215,7 +215,7 @@ public override async Task<ItemResponse<T>> ReplaceItemAsync<T>(
CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);
using (diagnosticsContext.GetOverallScope())
{
Task<ResponseMessage> response = this.ExtractPartitionKeyAndProcessItemStreamAsync(
ResponseMessage response = await this.ExtractPartitionKeyAndProcessItemStreamAsync(
partitionKey: partitionKey,
itemId: id,
item: item,
Expand All @@ -224,7 +224,7 @@ public override async Task<ItemResponse<T>> ReplaceItemAsync<T>(
diagnosticsContext: diagnosticsContext,
cancellationToken: cancellationToken);

return await this.ClientContext.ResponseFactory.CreateItemResponseAsync<T>(response);
return this.ClientContext.ResponseFactory.CreateItemResponse<T>(response);
}
}

Expand Down Expand Up @@ -257,7 +257,7 @@ public override async Task<ItemResponse<T>> DeleteItemAsync<T>(
CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);
using (diagnosticsContext.GetOverallScope())
{
Task<ResponseMessage> response = this.ProcessItemStreamAsync(
ResponseMessage response = await this.ProcessItemStreamAsync(
partitionKey: partitionKey,
itemId: id,
streamPayload: null,
Expand All @@ -266,7 +266,7 @@ public override async Task<ItemResponse<T>> DeleteItemAsync<T>(
diagnosticsContext: diagnosticsContext,
cancellationToken: cancellationToken);

return await this.ClientContext.ResponseFactory.CreateItemResponseAsync<T>(response);
return this.ClientContext.ResponseFactory.CreateItemResponse<T>(response);
}
}

Expand Down
18 changes: 9 additions & 9 deletions Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ protected ContainerCore(

public override Scripts.Scripts Scripts { get; }

public override Task<ContainerResponse> ReadContainerAsync(
public override async Task<ContainerResponse> ReadContainerAsync(
ContainerRequestOptions requestOptions = null,
CancellationToken cancellationToken = default(CancellationToken))
{
Task<ResponseMessage> response = this.ReadContainerStreamAsync(
ResponseMessage response = await this.ReadContainerStreamAsync(
requestOptions: requestOptions,
cancellationToken: cancellationToken);

return this.ClientContext.ResponseFactory.CreateContainerResponseAsync(this, response);
return this.ClientContext.ResponseFactory.CreateContainerResponse(this, response);
}

public override Task<ContainerResponse> ReplaceContainerAsync(
public override async Task<ContainerResponse> ReplaceContainerAsync(
ContainerProperties containerProperties,
ContainerRequestOptions requestOptions = null,
CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -82,23 +82,23 @@ public override Task<ContainerResponse> ReplaceContainerAsync(
}

this.ClientContext.ValidateResource(containerProperties.Id);
Task<ResponseMessage> response = this.ReplaceStreamInternalAsync(
ResponseMessage response = await this.ReplaceStreamInternalAsync(
streamPayload: this.ClientContext.SerializerCore.ToStream(containerProperties),
requestOptions: requestOptions,
cancellationToken: cancellationToken);

return this.ClientContext.ResponseFactory.CreateContainerResponseAsync(this, response);
return this.ClientContext.ResponseFactory.CreateContainerResponse(this, response);
}

public override Task<ContainerResponse> DeleteContainerAsync(
public override async Task<ContainerResponse> DeleteContainerAsync(
ContainerRequestOptions requestOptions = null,
CancellationToken cancellationToken = default(CancellationToken))
{
Task<ResponseMessage> response = this.DeleteContainerStreamAsync(
ResponseMessage response = await this.DeleteContainerStreamAsync(
requestOptions: requestOptions,
cancellationToken: cancellationToken);

return this.ClientContext.ResponseFactory.CreateContainerResponseAsync(this, response);
return this.ClientContext.ResponseFactory.CreateContainerResponse(this, response);
}

public override async Task<int?> ReadThroughputAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal abstract class CosmosClientContext : IDisposable

internal abstract CosmosSerializerCore SerializerCore { get; }

internal abstract CosmosResponseFactory ResponseFactory { get; }
internal abstract CosmosResponseFactoryInternal ResponseFactory { get; }

internal abstract RequestInvokerHandler RequestHandler { get; }

Expand Down
Loading