Skip to content

Commit c3c229a

Browse files
committed
Removed: Wait() method for MCM
1 parent 081cdde commit c3c229a

File tree

4 files changed

+64
-89
lines changed

4 files changed

+64
-89
lines changed

src/Algolia.Search.Test/EndToEnd/Index/MultiClusterManagementTest.cs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* THE SOFTWARE.
2222
*/
2323

24+
using Algolia.Search.Exceptions;
2425
using Algolia.Search.Models.Mcm;
2526
using Algolia.Search.Models.Search;
2627
using NUnit.Framework;
@@ -45,7 +46,7 @@ public async Task McmTest()
4546
string userId = TestHelper.GetMcmUserId();
4647
AssignUserIdResponse assignResponse =
4748
await BaseTest.McmClient.AssignUserIdAsync(userId, listClusters.ElementAt(0).ClusterName);
48-
assignResponse.Wait();
49+
WaitUserId(userId);
4950

5051
SearchResponse<UserIdResponse> searchResponse =
5152
await BaseTest.McmClient.SearchUserIDsAsync(new SearchUserIdsRequest
@@ -58,8 +59,7 @@ await BaseTest.McmClient.SearchUserIDsAsync(new SearchUserIdsRequest
5859
TopUserIdResponse topUserIds = await BaseTest.McmClient.GetTopUserIdAsync();
5960
Assert.True(topUserIds.TopUsers.Any());
6061

61-
RemoveUserIdResponse removeResponse = await BaseTest.McmClient.RemoveUserIdAsync(userId);
62-
removeResponse.Wait();
62+
RemoveUserId(userId);
6363

6464
ListUserIdsResponse listUserIdsTwo = await BaseTest.McmClient.ListUserIdsAsync();
6565
var yesterday = DateTime.UtcNow.AddDays(-1).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
@@ -70,5 +70,55 @@ await BaseTest.McmClient.SearchUserIDsAsync(new SearchUserIdsRequest
7070
userIdsToRemove.Select(x => BaseTest.McmClient.RemoveUserIdAsync(x.UserID)).ToArray();
7171
await Task.WhenAll(delete);
7272
}
73+
74+
private void WaitUserId(string userId)
75+
{
76+
while (true)
77+
{
78+
try
79+
{
80+
BaseTest.McmClient.GetUserId(userId);
81+
}
82+
catch (AlgoliaApiException ex)
83+
{
84+
// Loop until we have found the userID
85+
if (ex.HttpErrorCode == 404)
86+
{
87+
Task.Delay(1000);
88+
continue;
89+
}
90+
91+
throw;
92+
}
93+
94+
break;
95+
}
96+
}
97+
98+
private void RemoveUserId(String userId)
99+
{
100+
RemoveUserIdResponse deleteResponse;
101+
102+
while (true)
103+
{
104+
try
105+
{
106+
deleteResponse = BaseTest.McmClient.RemoveUserId(userId);
107+
}
108+
catch (AlgoliaApiException ex)
109+
{
110+
// Loop until we don't have Error 400: "Another mapping operation is already running for this userID"
111+
if (ex.Message.Contains("Another mapping operation is already running for this userID"))
112+
{
113+
Task.Delay(1000);
114+
continue;
115+
}
116+
117+
throw;
118+
}
119+
120+
break;
121+
}
122+
}
73123
}
74-
}
124+
}

src/Algolia.Search/Clients/SearchClient.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -466,26 +466,10 @@ public async Task<RemoveUserIdResponse> RemoveUserIdAsync(string userId, Request
466466
var userIdHeader = new Dictionary<string, string>() { { "X-Algolia-USER-ID", userId } };
467467
requestOptions = requestOptions.AddHeaders(userIdHeader);
468468

469-
try
470-
{
471-
RemoveUserIdResponse response = await _transport.ExecuteRequestAsync<RemoveUserIdResponse>(
472-
HttpMethod.Delete,
473-
$"/1/clusters/mapping", CallType.Write, requestOptions, ct)
474-
.ConfigureAwait(false);
475-
476-
response.UserId = userId;
477-
response.RemoveUserId = u => RemoveUserId(u);
478-
return response;
479-
}
480-
catch (AlgoliaApiException ex)
481-
{
482-
if (!ex.Message.Contains("Another mapping operation is already running for this userID"))
483-
{
484-
throw;
485-
}
486-
487-
return new RemoveUserIdResponse { UserId = userId, RemoveUserId = u => RemoveUserId(u) };
488-
}
469+
return await _transport.ExecuteRequestAsync<RemoveUserIdResponse>(
470+
HttpMethod.Delete,
471+
$"/1/clusters/mapping", CallType.Write, requestOptions, ct)
472+
.ConfigureAwait(false);
489473
}
490474

491475
/// <inheritdoc />
@@ -664,4 +648,4 @@ public async Task<TResult> CustomRequestAsync<TResult, TData>(TData data, string
664648
.ConfigureAwait(false);
665649
}
666650
}
667-
}
651+
}

src/Algolia.Search/Models/Mcm/AssignUserIdResponse.cs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace Algolia.Search.Models.Mcm
3131
/// <summary>
3232
/// Waitable response of assignUserid method
3333
/// </summary>
34-
public class AssignUserIdResponse : IAlgoliaWaitableResponse
34+
public class AssignUserIdResponse
3535
{
3636
internal Func<string, UserIdResponse> GetUserId { get; set; }
3737

@@ -41,36 +41,8 @@ public class AssignUserIdResponse : IAlgoliaWaitableResponse
4141
public string UserId { get; set; }
4242

4343
/// <summary>
44-
/// Date of creation of the userId
44+
/// /// Date of creation of the userId
4545
/// </summary>
4646
public DateTime CreatedAt { get; set; }
47-
48-
/// <summary>
49-
/// Wait until the userID is created on the API
50-
/// Loop until httpErrorCode != 404
51-
/// </summary>
52-
public void Wait()
53-
{
54-
while (true)
55-
{
56-
try
57-
{
58-
GetUserId(UserId);
59-
}
60-
catch (AlgoliaApiException ex)
61-
{
62-
// Loop until we have found the userID
63-
if (ex.HttpErrorCode == 404)
64-
{
65-
Task.Delay(1000);
66-
continue;
67-
}
68-
69-
throw;
70-
}
71-
72-
break;
73-
}
74-
}
7547
}
76-
}
48+
}

src/Algolia.Search/Models/Mcm/RemoveUserIdResponse.cs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace Algolia.Search.Models.Mcm
3131
/// <summary>
3232
/// Waitable response for removeUserId
3333
/// </summary>
34-
public class RemoveUserIdResponse : IAlgoliaWaitableResponse
34+
public class RemoveUserIdResponse
3535
{
3636
internal Func<string, RemoveUserIdResponse> RemoveUserId { get; set; }
3737

@@ -44,36 +44,5 @@ public class RemoveUserIdResponse : IAlgoliaWaitableResponse
4444
/// Date of deletion
4545
/// </summary>
4646
public DateTime DeletedAt { get; set; }
47-
48-
/// <summary>
49-
/// As the delete operation is asynchronous
50-
/// Wait until the userId is deleted on the API
51-
/// </summary>
52-
public void Wait()
53-
{
54-
RemoveUserIdResponse deleteResponse;
55-
56-
while (true)
57-
{
58-
try
59-
{
60-
deleteResponse = RemoveUserId(UserId);
61-
}
62-
catch (AlgoliaApiException ex)
63-
{
64-
// Loop until we don't have Error 400: "Another mapping operation is already running for this userID"
65-
if (ex.Message.Contains("Another mapping operation is already running for this userID"))
66-
{
67-
Task.Delay(1000);
68-
continue;
69-
}
70-
71-
throw;
72-
}
73-
74-
DeletedAt = deleteResponse.DeletedAt;
75-
break;
76-
}
77-
}
7847
}
79-
}
48+
}

0 commit comments

Comments
 (0)