Skip to content

Commit 1582e9b

Browse files
committed
PutItemOperationRequest
1 parent c5d7cfc commit 1582e9b

File tree

4 files changed

+342
-2
lines changed

4 files changed

+342
-2
lines changed

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Table.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,47 @@ internal Document PutItemHelper(Document doc, PutItemOperationConfig config)
12371237
return ret;
12381238
}
12391239

1240+
internal async Task<Document> PutItemHelperAsync(PutItemDocumentOperationRequest request, CancellationToken cancellationToken)
1241+
{
1242+
var req = MapPutItemDocumentOperationRequestToPutItemRequest(request);
1243+
this.UpdateRequestUserAgentDetails(req, isAsync: true);
1244+
var resp = await DDBClient.PutItemAsync(req, cancellationToken).ConfigureAwait(false);
1245+
request.Document.CommitChanges();
1246+
Document ret = null;
1247+
if (request.ReturnValues == ReturnValues.AllOldAttributes)
1248+
{
1249+
ret = this.FromAttributeMap(resp.Attributes);
1250+
}
1251+
return ret;
1252+
}
1253+
internal Document PutItemHelper(PutItemDocumentOperationRequest request)
1254+
{
1255+
var req = MapPutItemDocumentOperationRequestToPutItemRequest(request);
1256+
1257+
#if NETSTANDARD
1258+
// Cast the IAmazonDynamoDB to the concrete client instead, so we can access the internal sync-over-async methods
1259+
var client = DDBClient as AmazonDynamoDBClient;
1260+
if (client == null)
1261+
{
1262+
throw new InvalidOperationException("Calling the synchronous PutItem from .NET or .NET Core requires initializing the Table " +
1263+
"with an actual AmazonDynamoDBClient. You can use a mocked or substitute IAmazonDynamoDB when creating a Table via PutItemAsync instead.");
1264+
}
1265+
#else
1266+
var client = DDBClient;
1267+
#endif
1268+
1269+
var resp = client.PutItem(req);
1270+
1271+
request.Document.CommitChanges();
1272+
Document ret = null;
1273+
if (request.ReturnValues == ReturnValues.AllOldAttributes)
1274+
{
1275+
ret = this.FromAttributeMap(resp.Attributes);
1276+
}
1277+
return ret;
1278+
}
1279+
1280+
12401281
internal async Task<Document> PutItemHelperAsync(Document doc, PutItemOperationConfig config, CancellationToken cancellationToken)
12411282
{
12421283
var currentConfig = config ?? new PutItemOperationConfig();
@@ -1282,6 +1323,31 @@ internal async Task<Document> PutItemHelperAsync(Document doc, PutItemOperationC
12821323
return ret;
12831324
}
12841325

1326+
private PutItemRequest MapPutItemDocumentOperationRequestToPutItemRequest(PutItemDocumentOperationRequest request)
1327+
{
1328+
if(request==null)
1329+
throw new ArgumentNullException(nameof(request));
1330+
1331+
if (request.Document == null)
1332+
throw new InvalidOperationException("The Document property of the PutItemDocumentOperationRequest cannot be null.");
1333+
1334+
PutItemRequest req = new PutItemRequest
1335+
{
1336+
TableName = TableName,
1337+
Item = this.ToAttributeMap(request.Document)
1338+
};
1339+
1340+
if (request.ReturnValues == ReturnValues.AllOldAttributes)
1341+
req.ReturnValues = EnumMapper.Convert(request.ReturnValues);
1342+
1343+
if (request.ConditionalExpression is { IsSet: true })
1344+
{
1345+
request.ConditionalExpression.ApplyExpression(req, this);
1346+
}
1347+
1348+
return req;
1349+
}
1350+
12851351
#endregion
12861352

12871353

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/_async/Table.Async.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public partial interface ITable
3636

3737
/// <summary>
3838
/// Initiates the asynchronous execution of the PutItem operation.
39-
/// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.PutItem"/>
39+
/// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.PutItem(Document)"/>
4040
/// </summary>
4141
/// <param name="doc">Document to save.</param>
4242
/// <param name="cancellationToken">Token which can be used to cancel the task.</param>
@@ -45,14 +45,25 @@ public partial interface ITable
4545

4646
/// <summary>
4747
/// Initiates the asynchronous execution of the PutItem operation.
48-
/// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.PutItem"/>
48+
/// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.PutItem(Document, PutItemOperationConfig)"/>
4949
/// </summary>
5050
/// <param name="doc">Document to save.</param>
5151
/// <param name="config">Configuration to use.</param>
5252
/// <param name="cancellationToken">Token which can be used to cancel the task.</param>
5353
/// <returns>A Task that can be used to poll or wait for results, or both.</returns>
5454
Task<Document> PutItemAsync(Document doc, PutItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken));
5555

56+
/// <summary>
57+
/// Initiates the asynchronous execution of the PutItem operation.
58+
/// <seealso cref="Table.PutItem(Amazon.DynamoDBv2.DocumentModel.Document,Amazon.DynamoDBv2.DocumentModel.PutItemOperationConfig)"/>
59+
/// </summary>
60+
/// <param name="request">The PutItemDocumentOperationRequest object containing all parameters for the PutItem operation.</param>
61+
/// <param name="cancellationToken">Token which can be used to cancel the task.</param>
62+
/// <returns>A Task that can be used to poll or wait for results, or both.</returns>
63+
Task<Document> PutItemAsync(PutItemDocumentOperationRequest request, CancellationToken cancellationToken = default(CancellationToken));
64+
65+
66+
5667
#endregion
5768

5869
#region GetItemAsync
@@ -310,6 +321,16 @@ public partial class Table : ITable
310321
}
311322
}
312323

324+
/// <inheritdoc/>
325+
public async Task<Document> PutItemAsync(PutItemDocumentOperationRequest request, CancellationToken cancellationToken = default(CancellationToken))
326+
{
327+
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(Table), nameof(PutItemAsync));
328+
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
329+
{
330+
return await PutItemHelperAsync(request, cancellationToken).ConfigureAwait(false);
331+
}
332+
}
333+
313334
#endregion
314335

315336
#region GetItemAsync

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/_bcl/Table.Sync.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ public partial interface ITable
3939
/// <returns>True if put is successful or false if the condition in the config was not met.</returns>
4040
bool TryPutItem(Document doc, PutItemOperationConfig config = null);
4141

42+
/// <summary>
43+
/// Puts a document into DynamoDB, using optional configs.
44+
/// </summary>
45+
/// <param name="request">The PutItemDocumentOperationRequest object containing all parameters for the PutItem operation.</param>
46+
/// <returns>Null or updated attributes, depending on request config.</returns>
47+
Document PutItem(PutItemDocumentOperationRequest request);
48+
49+
/// <summary>
50+
/// Puts a document into DynamoDB, using optional configs.
51+
/// </summary>
52+
/// <param name="request">The PutItemDocumentOperationRequest object containing all parameters for the PutItem operation.</param>
53+
/// <returns>True if put is successful or false if the condition was not met.</returns>
54+
bool TryPutItem(PutItemDocumentOperationRequest request);
55+
4256
#endregion
4357

4458
#region GetItem
@@ -295,6 +309,35 @@ public bool TryPutItem(Document doc, PutItemOperationConfig config = null)
295309
}
296310
}
297311

312+
/// <inheritdoc/>
313+
public Document PutItem(PutItemDocumentOperationRequest request)
314+
{
315+
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(Table), nameof(PutItem));
316+
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
317+
{
318+
return PutItemHelper(request);
319+
}
320+
}
321+
322+
/// <inheritdoc/>
323+
public bool TryPutItem(PutItemDocumentOperationRequest request)
324+
{
325+
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(Table), nameof(TryPutItem));
326+
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
327+
{
328+
try
329+
{
330+
PutItemHelper(request);
331+
return true;
332+
}
333+
catch (ConditionalCheckFailedException)
334+
{
335+
return false;
336+
}
337+
}
338+
}
339+
340+
298341
#endregion
299342

300343

0 commit comments

Comments
 (0)