Skip to content

Commit

Permalink
Define onlyIfUnchanged for ETag support (#2)
Browse files Browse the repository at this point in the history
This was suggested to match what AppConfiguration does. It simplifies it, with no practical reason for IfNoneMatch. We could always add that later as well.
  • Loading branch information
heaths committed Apr 23, 2020
1 parent 5e9c901 commit 5ac4930
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 132 deletions.
64 changes: 42 additions & 22 deletions sdk/search/Azure.Search.Documents/src/SearchServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,11 @@ await SynonymMapsClient.CreateAsync(
/// Creates a new synonym map or updates an existing synonym map.
/// </summary>
/// <param name="synonymMap">Required. The <see cref="SynonymMap"/> to create or update.</param>
/// <param name="options">Optional <see cref="SearchConditionalOptions"/> to customize the operation's behavior.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SynonymMap.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="options">Optional <see cref="SearchRequestOptions"/> to customize the operation's behavior.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing the <see cref="SynonymMap"/> that was created.
Expand All @@ -1454,20 +1458,25 @@ await SynonymMapsClient.CreateAsync(
[ForwardsClientCalls]
public virtual Response<SynonymMap> CreateOrUpdateSynonymMap(
SynonymMap synonymMap,
SearchConditionalOptions options = null,
bool onlyIfUnchanged = false,
SearchRequestOptions options = null,
CancellationToken cancellationToken = default) =>
SynonymMapsClient.CreateOrUpdate(
synonymMap?.Name,
synonymMap,
options?.ClientRequestId,
options?.IfMatch?.ToString(),
options?.IfNoneMatch?.ToString(),
onlyIfUnchanged ? synonymMap?.ETag?.ToString() : null,
null,
cancellationToken);

/// <summary>
/// Creates a new synonym map or updates an existing synonym map.
/// </summary>
/// <param name="synonymMap">Required. The <see cref="SynonymMap"/> to create or update.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SynonymMap.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="options">Optional <see cref="SearchConditionalOptions"/> to customize the operation's behavior.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
Expand All @@ -1479,57 +1488,68 @@ public virtual Response<SynonymMap> CreateOrUpdateSynonymMap(
[ForwardsClientCalls]
public virtual async Task<Response<SynonymMap>> CreateOrUpdateSynonymMapAsync(
SynonymMap synonymMap,
bool onlyIfUnchanged = false,
SearchConditionalOptions options = null,
CancellationToken cancellationToken = default) =>
await SynonymMapsClient.CreateOrUpdateAsync(
synonymMap?.Name,
synonymMap,
options?.ClientRequestId,
options?.IfMatch?.ToString(),
options?.IfNoneMatch?.ToString(),
onlyIfUnchanged ? synonymMap?.ETag?.ToString() : null,
null,
cancellationToken)
.ConfigureAwait(false);

/// <summary>
/// Deletes a synonym map.
/// </summary>
/// <param name="synonymMapName">The name of the synonym map to delete.</param>
/// <param name="options">Optional <see cref="SearchConditionalOptions"/> to customize the operation's behavior.</param>
/// <param name="synonymMap">The <see cref="SynonymMap"/> to delete.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SynonymMap.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="options">Optional <see cref="SearchRequestOptions"/> to customize the operation's behavior.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response"/> from the server.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMapName"/> is null.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMap"/> or <see cref="SynonymMap.Name"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
[ForwardsClientCalls]
public virtual Response DeleteSynonymMap(
string synonymMapName,
SearchConditionalOptions options = null,
SynonymMap synonymMap,
bool onlyIfUnchanged = false,
SearchRequestOptions options = null,
CancellationToken cancellationToken = default) =>
SynonymMapsClient.Delete(
synonymMapName,
synonymMap?.Name,
options?.ClientRequestId,
options?.IfMatch?.ToString(),
options?.IfNoneMatch?.ToString(),
onlyIfUnchanged ? synonymMap?.ETag?.ToString() : null,
null,
cancellationToken);

/// <summary>
/// Deletes a synonym map.
/// </summary>
/// <param name="synonymMapName">The name of the synonym map to delete.</param>
/// <param name="options">Optional <see cref="SearchConditionalOptions"/> to customize the operation's behavior.</param>
/// <param name="synonymMap">The <see cref="SynonymMap"/> to delete.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SynonymMap.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="options">Optional <see cref="SearchRequestOptions"/> to customize the operation's behavior.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response"/> from the server.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMapName"/> is null.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMap"/> or <see cref="SynonymMap.Name"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
[ForwardsClientCalls]
public virtual async Task<Response> DeleteSynonymMapAsync(
string synonymMapName,
SearchConditionalOptions options = null,
SynonymMap synonymMap,
bool onlyIfUnchanged = false,
SearchRequestOptions options = null,
CancellationToken cancellationToken = default) =>
await SynonymMapsClient.DeleteAsync(
synonymMapName,
synonymMap?.Name,
options?.ClientRequestId,
options?.IfMatch?.ToString(),
options?.IfNoneMatch?.ToString(),
onlyIfUnchanged ? synonymMap?.ETag?.ToString() : null,
null,
cancellationToken)
.ConfigureAwait(false);

Expand Down
34 changes: 22 additions & 12 deletions sdk/search/Azure.Search.Documents/tests/SearchServiceClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,31 +346,41 @@ public async Task CrudSynonymMaps()

SearchServiceClient client = resources.GetServiceClient();

SynonymMap map = await client.CreateSynonymMapAsync(new SynonymMap(synonymMapName, "msft=>Microsoft"));
Assert.AreEqual(synonymMapName, map.Name);
Assert.AreEqual("solr", map.Format);
Assert.AreEqual("msft=>Microsoft", map.Synonyms);
SynonymMap createdMap = await client.CreateSynonymMapAsync(new SynonymMap(synonymMapName, "msft=>Microsoft"));
Assert.AreEqual(synonymMapName, createdMap.Name);
Assert.AreEqual("solr", createdMap.Format);
Assert.AreEqual("msft=>Microsoft", createdMap.Synonyms);

map = await client.CreateOrUpdateSynonymMapAsync(new SynonymMap(synonymMapName, "ms,msft=>Microsoft"), new SearchConditionalOptions { IfMatch = map.ETag });
Assert.AreEqual(synonymMapName, map.Name);
Assert.AreEqual("solr", map.Format);
Assert.AreEqual("ms,msft=>Microsoft", map.Synonyms);
SynonymMap updatedMap = await client.CreateOrUpdateSynonymMapAsync(
new SynonymMap(synonymMapName, "ms,msft=>Microsoft")
{
ETag = createdMap.ETag,
},
onlyIfUnchanged: true);
Assert.AreEqual(synonymMapName, updatedMap.Name);
Assert.AreEqual("solr", updatedMap.Format);
Assert.AreEqual("ms,msft=>Microsoft", updatedMap.Synonyms);

RequestFailedException ex = await CatchAsync<RequestFailedException>(async () =>
await client.CreateOrUpdateSynonymMapAsync(new SynonymMap(synonymMapName, "ms,msft=>Microsoft"), new SearchConditionalOptions { IfNoneMatch = map.ETag }));
await client.CreateOrUpdateSynonymMapAsync(
new SynonymMap(synonymMapName, "ms,msft=>Microsoft")
{
ETag = createdMap.ETag,
},
onlyIfUnchanged: true));
Assert.AreEqual((int)HttpStatusCode.PreconditionFailed, ex.Status);

Response<IReadOnlyList<SynonymMap>> mapsResponse = await client.GetSynonymMapsAsync(new[] { nameof(SynonymMap.Name) });
foreach (SynonymMap namedMap in mapsResponse.Value)
{
if (string.Equals(map.Name, namedMap.Name, StringComparison.OrdinalIgnoreCase))
if (string.Equals(updatedMap.Name, namedMap.Name, StringComparison.OrdinalIgnoreCase))
{
SynonymMap fetchedMap = await client.GetSynonymMapAsync(namedMap.Name);
Assert.AreEqual(map.Synonyms, fetchedMap.Synonyms);
Assert.AreEqual(updatedMap.Synonyms, fetchedMap.Synonyms);
}
}

await client.DeleteSynonymMapAsync(map.Name, new SearchConditionalOptions { IfMatch = map.ETag });
await client.DeleteSynonymMapAsync(updatedMap, onlyIfUnchanged: true);
}

/// <summary>
Expand Down
Loading

0 comments on commit 5ac4930

Please sign in to comment.