-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add users batch * add users batch * remove comments
- Loading branch information
Showing
6 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Stream.Models; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace Stream | ||
{ | ||
public interface IUsersBatch | ||
{ | ||
Task<IEnumerable<User>> UpsertUsersAsync(IEnumerable<User> users, bool overrideExisting = false); | ||
Task<IEnumerable<User>> GetUsersAsync(IEnumerable<string> userIds); | ||
Task<IEnumerable<string>> DeleteUsersAsync(IEnumerable<string> userIds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Stream.Utils; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Stream.Models | ||
{ | ||
public class AddUserBatchResponse | ||
{ | ||
public IEnumerable<User> CreatedUsers { get; set; } | ||
} | ||
|
||
public class GetUserBatchResponse | ||
{ | ||
public IEnumerable<User> Users { get; set; } | ||
} | ||
|
||
public class DeleteUsersBatchResponse | ||
{ | ||
public IEnumerable<string> DeletedUserIds { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Stream.Models; | ||
using Stream.Utils; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace Stream | ||
{ | ||
public class UsersBatch : IUsersBatch | ||
{ | ||
private readonly StreamClient _client; | ||
|
||
internal UsersBatch(StreamClient client) | ||
{ | ||
_client = client; | ||
} | ||
|
||
public async Task<IEnumerable<User>> UpsertUsersAsync(IEnumerable<User> users, bool overrideExisting = false) | ||
{ | ||
var body = new Dictionary<string, object> | ||
{ | ||
{ "users", users }, | ||
{ "override", overrideExisting }, | ||
}; | ||
var request = _client.BuildAppRequest("users/", HttpMethod.Post); | ||
request.SetJsonBody(StreamJsonConverter.SerializeObject(body)); | ||
|
||
var response = await _client.MakeRequestAsync(request); | ||
|
||
if (response.StatusCode == HttpStatusCode.Created) | ||
{ | ||
var addUserBatchResponse = StreamJsonConverter.DeserializeObject<AddUserBatchResponse>(response.Content); | ||
return addUserBatchResponse.CreatedUsers; | ||
} | ||
|
||
throw StreamException.FromResponse(response); | ||
} | ||
|
||
public async Task<IEnumerable<User>> GetUsersAsync(IEnumerable<string> userIds) | ||
{ | ||
var request = _client.BuildAppRequest("users/", HttpMethod.Get); | ||
request.AddQueryParameter("ids", string.Join(",", userIds)); | ||
|
||
var response = await _client.MakeRequestAsync(request); | ||
|
||
if (response.StatusCode == HttpStatusCode.OK) | ||
{ | ||
var getUserBatchResponse = StreamJsonConverter.DeserializeObject<GetUserBatchResponse>(response.Content); | ||
return getUserBatchResponse.Users; | ||
} | ||
|
||
throw StreamException.FromResponse(response); | ||
} | ||
|
||
public async Task<IEnumerable<string>> DeleteUsersAsync(IEnumerable<string> userIds) | ||
{ | ||
var request = _client.BuildAppRequest("users/", HttpMethod.Delete); | ||
request.AddQueryParameter("ids", string.Join(",", userIds)); | ||
|
||
var response = await _client.MakeRequestAsync(request); | ||
|
||
if (response.StatusCode == HttpStatusCode.OK) | ||
{ | ||
var deleteUserBatchResponse = StreamJsonConverter.DeserializeObject<DeleteUsersBatchResponse>(response.Content); | ||
return deleteUserBatchResponse.DeletedUserIds; | ||
} | ||
|
||
throw StreamException.FromResponse(response); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using NUnit.Framework; | ||
using Stream; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace StreamNetTests | ||
{ | ||
[TestFixture] | ||
public class UsersBatchTests : TestBase | ||
{ | ||
[Test] | ||
public async Task TestAddGetUsersAsync() | ||
{ | ||
var userIds = new List<string> { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }; | ||
|
||
var users = new List<User> | ||
{ | ||
new User { Id = userIds[0], Data = new Dictionary<string, object> { { "field", "value1" } } }, | ||
new User { Id = userIds[1], Data = new Dictionary<string, object> { { "field", "value2" } } }, | ||
}; | ||
|
||
var response = await Client.UsersBatch.UpsertUsersAsync(users); | ||
|
||
Assert.NotNull(response); | ||
Assert.AreEqual(users.Count, response.Count()); | ||
|
||
var usersReturned = await Client.UsersBatch.GetUsersAsync(userIds); | ||
|
||
Assert.NotNull(usersReturned); | ||
Assert.AreEqual(userIds.Count, usersReturned.Count()); | ||
} | ||
|
||
[Test] | ||
public async Task TestDeleteUsersAsync() | ||
{ | ||
var userIds = new List<string> { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }; | ||
|
||
var deletedUserIds = await Client.UsersBatch.DeleteUsersAsync(userIds); | ||
|
||
Assert.NotNull(deletedUserIds); | ||
Assert.AreEqual(userIds.Count, deletedUserIds.Count()); | ||
Assert.IsTrue(userIds.All(id => deletedUserIds.Contains(id))); | ||
} | ||
|
||
[Test] | ||
public async Task AddGetDeleteGetUsersAsync() | ||
{ | ||
var userIds = new List<string> { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }; | ||
|
||
// Add users | ||
var users = new List<User> | ||
{ | ||
new User { Id = userIds[0], Data = new Dictionary<string, object> { { "field", "value1" } } }, | ||
new User { Id = userIds[1], Data = new Dictionary<string, object> { { "field", "value2" } } }, | ||
}; | ||
|
||
var addResponse = await Client.UsersBatch.UpsertUsersAsync(users); | ||
Assert.NotNull(addResponse); | ||
Assert.AreEqual(users.Count, addResponse.Count()); | ||
|
||
// Get users to confirm they were added | ||
var getUsersResponse = await Client.UsersBatch.GetUsersAsync(userIds); | ||
Assert.NotNull(getUsersResponse); | ||
Assert.AreEqual(users.Count, getUsersResponse.Count()); | ||
|
||
// Delete users | ||
var deleteResponse = await Client.UsersBatch.DeleteUsersAsync(userIds); | ||
Assert.NotNull(deleteResponse); | ||
Assert.AreEqual(userIds.Count(), deleteResponse.Count()); | ||
Assert.IsTrue(userIds.All(id => deleteResponse.Contains(id))); | ||
|
||
// Attempt to get deleted users to confirm they were deleted | ||
var getDeletedUsersResponse = await Client.UsersBatch.GetUsersAsync(userIds); | ||
Assert.IsEmpty(getDeletedUsersResponse); | ||
} | ||
} | ||
} |