Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 43 additions & 9 deletions src/Custom/Batch/CreateBatchOperation.Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ public static async Task<CreateBatchOperation> RehydrateAsync(BatchClient client

CreateBatchOperationToken token = CreateBatchOperationToken.FromToken(rehydrationToken);

ClientResult result = await client.GetBatchAsync(token.BatchId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
PipelineResponse response = result.GetRawResponse();

using JsonDocument doc = JsonDocument.Parse(response.Content);
string status = doc.RootElement.GetProperty("status"u8).GetString()!;

return client.CreateCreateBatchOperation(token.BatchId, status, response);
return await RehydrateAsync(client, token.BatchId, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -87,13 +81,53 @@ public static CreateBatchOperation Rehydrate(BatchClient client, ContinuationTok

CreateBatchOperationToken token = CreateBatchOperationToken.FromToken(rehydrationToken);

ClientResult result = client.GetBatch(token.BatchId, cancellationToken.ToRequestOptions());
return Rehydrate(client, token.BatchId, cancellationToken);
}

/// <summary>
/// Recreates a <see cref="CreateBatchOperation"/> from a batch ID.
/// </summary>
/// <param name="client"> The <see cref="BatchClient"/> used to obtain the
/// operation status from the service. </param>
/// <param name="batchId"> The ID of the batch operation to rehydrate. </param>
/// <param name="cancellationToken"> A token that can be used to cancel the
/// request. </param>
/// <returns> The rehydrated operation. </returns>
/// <exception cref="ArgumentNullException"> <paramref name="client"/> is null. </exception>
public static async Task<CreateBatchOperation> RehydrateAsync(BatchClient client, string batchId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(client, nameof(client));

ClientResult result = await client.GetBatchAsync(batchId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
PipelineResponse response = result.GetRawResponse();

using JsonDocument doc = JsonDocument.Parse(response.Content);
string status = doc.RootElement.GetProperty("status"u8).GetString()!;

return client.CreateCreateBatchOperation(batchId, status, response);
}

/// <summary>
/// Recreates a <see cref="CreateBatchOperation"/> from a batch ID.
/// </summary>
/// <param name="client"> The <see cref="BatchClient"/> used to obtain the
/// operation status from the service. </param>
/// <param name="batchId"> The ID of the batch operation to rehydrate. </param>
/// <param name="cancellationToken"> A token that can be used to cancel the
/// request. </param>
/// <returns> The rehydrated operation. </returns>
/// <exception cref="ArgumentNullException"> <paramref name="client"/> is null. </exception>
public static CreateBatchOperation Rehydrate(BatchClient client, string batchId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(client, nameof(client));

ClientResult result = client.GetBatch(batchId, cancellationToken.ToRequestOptions());
PipelineResponse response = result.GetRawResponse();

using JsonDocument doc = JsonDocument.Parse(response.Content);
string status = doc.RootElement.GetProperty("status"u8).GetString()!;

return client.CreateCreateBatchOperation(token.BatchId, status, response);
return client.CreateCreateBatchOperation(batchId, status, response);
}

/// <inheritdoc/>
Expand Down
28 changes: 19 additions & 9 deletions tests/Batch/BatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ public async Task CreateGetAndCancelBatchProtocol()
Assert.That(status, Is.EqualTo("validating"));
}

[Test]
public async Task CanRehydrateBatchOperation()
[TestCase(true)]
[TestCase(false)]
public async Task CanRehydrateBatchOperation(bool useBatchId)
{
using MemoryStream testFileStream = new();
using StreamWriter streamWriter = new(testFileStream);
Expand Down Expand Up @@ -183,13 +184,22 @@ public async Task CanRehydrateBatchOperation()
? await client.CreateBatchAsync(content, waitUntilCompleted: false)
: client.CreateBatch(content, waitUntilCompleted: false);

// Simulate rehydration of the operation
BinaryData rehydrationBytes = batchOperation.RehydrationToken.ToBytes();
ContinuationToken rehydrationToken = ContinuationToken.FromBytes(rehydrationBytes);

CreateBatchOperation rehydratedOperation = IsAsync ?
await CreateBatchOperation.RehydrateAsync(client, rehydrationToken) :
CreateBatchOperation.Rehydrate(client, rehydrationToken);
CreateBatchOperation rehydratedOperation;
if (useBatchId)
{
rehydratedOperation = IsAsync ?
await CreateBatchOperation.RehydrateAsync(client, batchOperation.BatchId) :
CreateBatchOperation.Rehydrate(client, batchOperation.BatchId);
}
else {
// Simulate rehydration of the operation
BinaryData rehydrationBytes = batchOperation.RehydrationToken.ToBytes();
ContinuationToken rehydrationToken = ContinuationToken.FromBytes(rehydrationBytes);

rehydratedOperation = IsAsync ?
await CreateBatchOperation.RehydrateAsync(client, rehydrationToken) :
CreateBatchOperation.Rehydrate(client, rehydrationToken);
}

static bool Validate(CreateBatchOperation operation)
{
Expand Down