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

Define onlyIfUnchanged for ETag support #2

Merged
merged 1 commit into from
Apr 23, 2020
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
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 @@ -1433,7 +1433,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 @@ -1444,20 +1448,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 @@ -1469,57 +1478,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 @@ -261,31 +261,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