Skip to content

Commit

Permalink
Objects can be released in batch (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroloux authored Oct 18, 2024
1 parent 623de8f commit 83e02a1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 105 deletions.
29 changes: 23 additions & 6 deletions SeatsioDotNet.Test/Events/ChangeObjectStatusInBatchTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using SeatsioDotNet.EventReports;
using SeatsioDotNet.Events;
using Xunit;

Expand All @@ -17,8 +18,8 @@ public async Task Test()

var result = await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt1.Key, new[] {"A-1"}, "lolzor"),
new StatusChangeRequest(evnt2.Key, new[] {"A-2"}, "lolzor")
new StatusChangeRequest(type: StatusChangeRequest.CHANGE_STATUS_TO, eventKey: evnt1.Key, objects: new[] {"A-1"}, status: "lolzor"),
new StatusChangeRequest(type: StatusChangeRequest.CHANGE_STATUS_TO,eventKey: evnt2.Key, objects: new[] {"A-2"}, status: "lolzor")
});

Assert.Equal("lolzor", result[0].Objects["A-1"].Status);
Expand All @@ -40,7 +41,7 @@ public async Task ChannelKeys()

var result = await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "lolzor", channelKeys: new[] {"channelKey1"}),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "lolzor", channelKeys: new[] {"channelKey1"}),
});

Assert.Equal("lolzor", result[0].Objects["A-1"].Status);
Expand All @@ -58,7 +59,7 @@ public async Task IgnoreChannels()

var result = await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "lolzor", ignoreChannels: true),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "lolzor", ignoreChannels: true),
});

Assert.Equal("lolzor", result[0].Objects["A-1"].Status);
Expand All @@ -74,7 +75,7 @@ await Assert.ThrowsAsync<SeatsioException>(async () =>
{
await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "lolzor", ignoreChannels: true, allowedPreviousStatuses: new[] {"someOtherStatus"}),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "lolzor", ignoreChannels: true, allowedPreviousStatuses: new[] {"someOtherStatus"}),
});
});
}
Expand All @@ -89,8 +90,24 @@ await Assert.ThrowsAsync<SeatsioException>(async () =>
{
await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "lolzor", ignoreChannels: true, rejectedPreviousStatuses: new[] {"free"}),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "lolzor", ignoreChannels: true, rejectedPreviousStatuses: new[] {"free"}),
});
});
}

[Fact]
public async Task Release()
{
var chartKey = CreateTestChart();
var evnt = await Client.Events.CreateAsync(chartKey);
await Client.Events.BookAsync(evnt.Key, new[] {"A-1"});

var result = await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(type: StatusChangeRequest.RELEASE, eventKey: evnt.Key, objects: new[] {"A-1"}),
});

Assert.Equal(EventObjectInfo.Free, result[0].Objects["A-1"].Status);
Assert.Equal(EventObjectInfo.Free, (await Client.Events.RetrieveObjectInfoAsync(evnt.Key, "A-1")).Status);
}
}
10 changes: 5 additions & 5 deletions SeatsioDotNet.Test/Events/ListStatusChangesForObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public async Task Test()
var evnt = await Client.Events.CreateAsync(chartKey);
await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "s1"),
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "s2"),
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "s3"),
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "s4"),
new StatusChangeRequest(evnt.Key, new[] {"A-2"}, "s5")
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "s1"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "s2"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "s3"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "s4"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-2"}, status: "s5")
});
await WaitForStatusChanges(Client, evnt, 5);

Expand Down
46 changes: 23 additions & 23 deletions SeatsioDotNet.Test/Events/ListStatusChangesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public async Task Test()
var evnt = await Client.Events.CreateAsync(chartKey);
await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "s1"),
new StatusChangeRequest(evnt.Key, new[] {"A-2"}, "s2"),
new StatusChangeRequest(evnt.Key, new[] {"A-3"}, "s3"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "s1"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-2"}, status: "s2"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-3"}, status: "s3"),
});
await WaitForStatusChanges(Client, evnt, 3);

Expand Down Expand Up @@ -88,10 +88,10 @@ public async Task Filter()
var evnt = await Client.Events.CreateAsync(chartKey);
await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-2"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"B-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-3"}, "booked")
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-2"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"B-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-3"}, status: "booked")
});
await WaitForStatusChanges(Client, evnt, 4);

Expand All @@ -107,10 +107,10 @@ public async Task SortAsc()
var evnt = await Client.Events.CreateAsync(chartKey);
await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-2"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"B-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-3"}, "booked")
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-2"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"B-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-3"}, status: "booked")
});
await WaitForStatusChanges(Client, evnt, 4);

Expand All @@ -126,10 +126,10 @@ public async Task SortAscPageBefore()
var evnt = await Client.Events.CreateAsync(chartKey);
await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-2"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"B-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-3"}, "booked")
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-2"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"B-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-3"}, status: "booked")
});
await WaitForStatusChanges(Client, evnt, 4);

Expand All @@ -147,10 +147,10 @@ public async Task SortAscPageAfter()
var evnt = await Client.Events.CreateAsync(chartKey);
await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-2"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"B-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-3"}, "booked")
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-2"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"B-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-3"}, status: "booked")
});
await WaitForStatusChanges(Client, evnt, 4);

Expand All @@ -168,10 +168,10 @@ public async Task SortDesc()
var evnt = await Client.Events.CreateAsync(chartKey);
await Client.Events.ChangeObjectStatusAsync(new[]
{
new StatusChangeRequest(evnt.Key, new[] {"A-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-2"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"B-1"}, "booked"),
new StatusChangeRequest(evnt.Key, new[] {"A-3"}, "booked")
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-2"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"B-1"}, status: "booked"),
new StatusChangeRequest(eventKey: evnt.Key, objects: new[] {"A-3"}, status: "booked")
});
await WaitForStatusChanges(Client, evnt, 4);

Expand Down
75 changes: 17 additions & 58 deletions SeatsioDotNet/Events/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,18 @@ public async Task<BestAvailableResult> BookAsync(string eventKey, BestAvailable

public async Task<ChangeObjectStatusResult> PutUpForResaleAsync(string eventKey, IEnumerable<string> objects)
{
return await ChangeObjectStatusAsync(eventKey, objects, EventObjectInfo.Resale, null);
return await ChangeObjectStatusAsync(eventKey, objects, EventObjectInfo.Resale);
}

public async Task<ChangeObjectStatusResult> PutUpForResaleAsync(string[] eventKeys, IEnumerable<string> objects)
{
return await ChangeObjectStatusAsync(eventKeys, objects, EventObjectInfo.Resale, null);
return await ChangeObjectStatusAsync(eventKeys, objects, EventObjectInfo.Resale);
}

public async Task<ChangeObjectStatusResult> ReleaseAsync(string eventKey, IEnumerable<string> objects, string holdToken = null,
string orderId = null, bool? keepExtraData = null, bool? ignoreChannels = null, string[] channelKeys = null, CancellationToken cancellationToken = default)
{
var requestBody = ReleaseObjectsRequest(new[] {eventKey}, objects.Select(o => new ObjectProperties(o)), holdToken, orderId, keepExtraData,
var requestBody = ChangeObjectStatusRequest(StatusChangeRequest.RELEASE, new[] {eventKey}, objects.Select(o => new ObjectProperties(o)), null, holdToken, orderId, keepExtraData,
ignoreChannels, channelKeys);
return await DoChangeObjectStatusAsync(requestBody, cancellationToken);
}
Expand Down Expand Up @@ -361,7 +361,7 @@ public async Task<ChangeObjectStatusResult> ChangeObjectStatusAsync(IEnumerable<
string[] allowedPreviousStatuses = null,
string[] rejectedPreviousStatuses = null, CancellationToken cancellationToken = default)
{
var requestBody = ChangeObjectStatusRequest(events, objects, status, holdToken, orderId, keepExtraData,
var requestBody = ChangeObjectStatusRequest(StatusChangeRequest.CHANGE_STATUS_TO, events, objects, status, holdToken, orderId, keepExtraData,
ignoreChannels, channelKeys, allowedPreviousStatuses, rejectedPreviousStatuses);
return await DoChangeObjectStatusAsync(requestBody, cancellationToken);
}
Expand All @@ -376,7 +376,7 @@ private async Task<ChangeObjectStatusResult> DoChangeObjectStatusAsync(Dictionar

public async Task<List<ChangeObjectStatusResult>> ChangeObjectStatusAsync(StatusChangeRequest[] requests, CancellationToken cancellationToken = default)
{
var serializedRequests = requests.Select(r => ChangeObjectStatusRequest(r.EventKey, r.Objects, r.Status,
var serializedRequests = requests.Select(r => ChangeObjectStatusRequest(r.Type, r.EventKey, r.Objects, r.Status,
r.HoldToken, r.OrderId, r.KeepExtraData, r.IgnoreChannels, r.ChannelKeys,
r.AllowedPreviousStatuses, r.RejectedPreviousStatuses));
var restRequest = new RestRequest("/events/actions/change-object-status", Method.Post)
Expand All @@ -385,49 +385,43 @@ public async Task<List<ChangeObjectStatusResult>> ChangeObjectStatusAsync(Status
return AssertOk(await _restClient.ExecuteAsync<ChangeObjectStatusInBatchResult>(restRequest, cancellationToken)).Results;
}

private Dictionary<string, object> ChangeObjectStatusRequest(string evnt, IEnumerable<ObjectProperties> objects,
private Dictionary<string, object> ChangeObjectStatusRequest(string type, string evnt, IEnumerable<ObjectProperties> objects,
string status, string holdToken, string orderId, bool? keepExtraData, bool? ignoreChannels = null,
string[] channelKeys = null, string[] allowedPreviousStatuses = null,
string[] rejectedPreviousStatuses = null)
string[] channelKeys = null, string[] allowedPreviousStatuses = null, string[] rejectedPreviousStatuses = null)
{
var request = ChangeObjectStatusRequest(objects, status, holdToken, orderId, keepExtraData, ignoreChannels,
var request = ChangeObjectStatusRequest(type, objects, status, holdToken, orderId, keepExtraData, ignoreChannels,
channelKeys, allowedPreviousStatuses, rejectedPreviousStatuses);
request.Add("event", evnt);
return request;
}

private Dictionary<string, object> ReleaseObjectsRequest(IEnumerable<string> events,
IEnumerable<ObjectProperties> objects, string holdToken, string orderId, bool? keepExtraData,
bool? ignoreChannels = null, string[] channelKeys = null)
{
var request = ReleaseObjectsRequest(objects, holdToken, orderId, keepExtraData, ignoreChannels, channelKeys);
request.Add("events", events);
return request;
}

private Dictionary<string, object> ChangeObjectStatusRequest(IEnumerable<string> events,
private Dictionary<string, object> ChangeObjectStatusRequest(string type, IEnumerable<string> events,
IEnumerable<ObjectProperties> objects, string status, string holdToken, string orderId, bool? keepExtraData,
bool? ignoreChannels = null, string[] channelKeys = null,
string[] allowedPreviousStatuses = null, string[] rejectedPreviousStatuses = null)
{
var request = ChangeObjectStatusRequest(objects, status, holdToken, orderId, keepExtraData, ignoreChannels,
var request = ChangeObjectStatusRequest(type, objects, status, holdToken, orderId, keepExtraData, ignoreChannels,
channelKeys, allowedPreviousStatuses, rejectedPreviousStatuses);
request.Add("events", events);
return request;
}

private Dictionary<string, object> ChangeObjectStatusRequest(IEnumerable<ObjectProperties> objects,
private Dictionary<string, object> ChangeObjectStatusRequest(string type, IEnumerable<ObjectProperties> objects,
string status, string holdToken, string orderId, bool? keepExtraData, bool? ignoreChannels = null,
string[] channelKeys = null, string[] allowedPreviousStatuses = null,
string[] rejectedPreviousStatuses = null)
{
var requestBody = new Dictionary<string, object>()
{
{"type", "CHANGE_STATUS_TO"},
{"status", status},
{"type", type},
{"objects", objects.Select(o => o.AsDictionary())},
};

if (type != StatusChangeRequest.RELEASE)
{
requestBody.Add("status", status);
}

if (holdToken != null)
{
requestBody.Add("holdToken", holdToken);
Expand Down Expand Up @@ -466,41 +460,6 @@ private Dictionary<string, object> ChangeObjectStatusRequest(IEnumerable<ObjectP
return requestBody;
}

private Dictionary<string, object> ReleaseObjectsRequest(IEnumerable<ObjectProperties> objects, string holdToken, string orderId, bool? keepExtraData, bool? ignoreChannels = null, string[] channelKeys = null)
{
var requestBody = new Dictionary<string, object>()
{
{"type", "RELEASE"},
{"objects", objects.Select(o => o.AsDictionary())},
};

if (holdToken != null)
{
requestBody.Add("holdToken", holdToken);
}

if (orderId != null)
{
requestBody.Add("orderId", orderId);
}

if (keepExtraData != null)
{
requestBody.Add("keepExtraData", keepExtraData);
}

if (ignoreChannels != null)
{
requestBody.Add("ignoreChannels", ignoreChannels);
}

if (channelKeys != null)
{
requestBody.Add("channelKeys", channelKeys);
}
return requestBody;
}

public async Task<BestAvailableResult> ChangeObjectStatusAsync(string eventKey, BestAvailable bestAvailable, string status,
string holdToken = null, string orderId = null, bool? keepExtraData = null, bool? ignoreChannels = null,
string[] channelKeys = null, CancellationToken cancellationToken = default)
Expand Down
19 changes: 6 additions & 13 deletions SeatsioDotNet/Events/StatusChangeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ namespace SeatsioDotNet.Events;

public class StatusChangeRequest
{
public const string CHANGE_STATUS_TO = "CHANGE_STATUS_TO";
public const string RELEASE = "RELEASE";

public string Type { get; }
public string EventKey { get; }
public IEnumerable<ObjectProperties> Objects { get; }
public string Status { get; }
Expand All @@ -16,20 +20,9 @@ public class StatusChangeRequest
public string[] AllowedPreviousStatuses { get; }
public string[] RejectedPreviousStatuses { get; }

public StatusChangeRequest(string eventKey, IEnumerable<ObjectProperties> objects, string status, string holdToken = null, string orderId = null, bool? keepExtraData = null, bool? ignoreChannels = null, string[] channelKeys = null)
{
EventKey = eventKey;
Objects = objects;
Status = status;
HoldToken = holdToken;
OrderId = orderId;
KeepExtraData = keepExtraData;
IgnoreChannels = ignoreChannels;
ChannelKeys = channelKeys;
}

public StatusChangeRequest(string eventKey, IEnumerable<string> objects, string status, string holdToken = null, string orderId = null, bool? keepExtraData = null, bool? ignoreChannels = null, string[] channelKeys = null, string[] allowedPreviousStatuses = null, string[] rejectedPreviousStatuses = null)
public StatusChangeRequest(string type = CHANGE_STATUS_TO, string eventKey = null, IEnumerable<string> objects = null, string status = null, string holdToken = null, string orderId = null, bool? keepExtraData = null, bool? ignoreChannels = null, string[] channelKeys = null, string[] allowedPreviousStatuses = null, string[] rejectedPreviousStatuses = null)
{
Type = type;
EventKey = eventKey;
Objects = objects.Select(o => new ObjectProperties(o));
Status = status;
Expand Down

0 comments on commit 83e02a1

Please sign in to comment.