From 283b08f8002e078f83c4e0078064df1f9d321cc2 Mon Sep 17 00:00:00 2001 From: mroloux Date: Sun, 29 Sep 2019 22:46:36 +0200 Subject: [PATCH] API calls take optional account ID --- .../AccountIdAuthenticationTest.cs | 18 ++++++++++++++++++ SeatsioDotNet.Test/ErrorHandlingTest.cs | 2 +- .../HoldTokens/RetrieveHoldTokenTest.cs | 1 + SeatsioDotNet.Test/SeatsioClientTest.cs | 7 ++++++- .../Subaccounts/RetrieveSubaccountTest.cs | 1 + SeatsioDotNet/HoldTokens/HoldToken.cs | 1 + SeatsioDotNet/SeatsioClient.cs | 17 +++++++++++++---- SeatsioDotNet/Subaccounts/Subaccount.cs | 1 + 8 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 SeatsioDotNet.Test/AccountIdAuthenticationTest.cs diff --git a/SeatsioDotNet.Test/AccountIdAuthenticationTest.cs b/SeatsioDotNet.Test/AccountIdAuthenticationTest.cs new file mode 100644 index 0000000..56c234f --- /dev/null +++ b/SeatsioDotNet.Test/AccountIdAuthenticationTest.cs @@ -0,0 +1,18 @@ +using Xunit; + +namespace SeatsioDotNet.Test +{ + public class AccountIdAuthenticationTest : SeatsioClientTest + { + [Fact] + public void Test() + { + var subaccount = Client.Subaccounts.Create(); + + var subaccountClient = CreateSeatsioClient(User.SecretKey, subaccount.AccountId); + var holdToken = subaccountClient.HoldTokens.Create(); + + Assert.Equal(subaccount.AccountId, holdToken.AccountId); + } + } +} \ No newline at end of file diff --git a/SeatsioDotNet.Test/ErrorHandlingTest.cs b/SeatsioDotNet.Test/ErrorHandlingTest.cs index a4d931b..27ec8ce 100644 --- a/SeatsioDotNet.Test/ErrorHandlingTest.cs +++ b/SeatsioDotNet.Test/ErrorHandlingTest.cs @@ -19,7 +19,7 @@ public void Test4xx() public void WeirdError() { var e = Assert.Throws(() => - new SeatsioClient("", "unknownProtocol://").Events.RetrieveObjectStatus("unexistingEvent", "unexistingObject")); + new SeatsioClient("", null, "unknownProtocol://").Events.RetrieveObjectStatus("unexistingEvent", "unexistingObject")); Assert.Equal("GET resulted in a 0 response.", e.Message); Assert.Null(e.Errors); diff --git a/SeatsioDotNet.Test/HoldTokens/RetrieveHoldTokenTest.cs b/SeatsioDotNet.Test/HoldTokens/RetrieveHoldTokenTest.cs index 352d7f3..50b0673 100644 --- a/SeatsioDotNet.Test/HoldTokens/RetrieveHoldTokenTest.cs +++ b/SeatsioDotNet.Test/HoldTokens/RetrieveHoldTokenTest.cs @@ -14,6 +14,7 @@ public void Test() Assert.Equal(holdToken.Token, retrievedHoldToken.Token); Assert.Equal(holdToken.ExpiresAt, retrievedHoldToken.ExpiresAt); Assert.InRange(holdToken.ExpiresInSeconds, 14 * 60, 15 * 60); + Assert.True(holdToken.AccountId > 0); } } } \ No newline at end of file diff --git a/SeatsioDotNet.Test/SeatsioClientTest.cs b/SeatsioDotNet.Test/SeatsioClientTest.cs index b6efa89..d29065a 100644 --- a/SeatsioDotNet.Test/SeatsioClientTest.cs +++ b/SeatsioDotNet.Test/SeatsioClientTest.cs @@ -64,7 +64,12 @@ protected string CreateTestChartFromJson(String json) protected SeatsioClient CreateSeatsioClient(string secretKey) { - return new SeatsioClient(secretKey, BaseUrl); + return new SeatsioClient(secretKey, null, BaseUrl); + } + + protected SeatsioClient CreateSeatsioClient(string secretKey, long accountId) + { + return new SeatsioClient(secretKey, accountId, BaseUrl); } } } \ No newline at end of file diff --git a/SeatsioDotNet.Test/Subaccounts/RetrieveSubaccountTest.cs b/SeatsioDotNet.Test/Subaccounts/RetrieveSubaccountTest.cs index d96197b..fb37706 100644 --- a/SeatsioDotNet.Test/Subaccounts/RetrieveSubaccountTest.cs +++ b/SeatsioDotNet.Test/Subaccounts/RetrieveSubaccountTest.cs @@ -17,6 +17,7 @@ public void Test() Assert.Equal(subaccount.PublicKey, retrievedSubaccount.PublicKey); Assert.Equal("joske", retrievedSubaccount.Name); Assert.True(retrievedSubaccount.Active); + Assert.True(retrievedSubaccount.AccountId > 0); } } } \ No newline at end of file diff --git a/SeatsioDotNet/HoldTokens/HoldToken.cs b/SeatsioDotNet/HoldTokens/HoldToken.cs index 3c27fbf..d2ee45b 100644 --- a/SeatsioDotNet/HoldTokens/HoldToken.cs +++ b/SeatsioDotNet/HoldTokens/HoldToken.cs @@ -9,5 +9,6 @@ public class HoldToken public string Token { get; set; } public DateTimeOffset ExpiresAt { get; set; } public int ExpiresInSeconds { get; set; } + public long AccountId { get; set; } } } \ No newline at end of file diff --git a/SeatsioDotNet/SeatsioClient.cs b/SeatsioDotNet/SeatsioClient.cs index a624a90..15ac1e5 100644 --- a/SeatsioDotNet/SeatsioClient.cs +++ b/SeatsioDotNet/SeatsioClient.cs @@ -14,9 +14,9 @@ public class SeatsioClient public ChartReports.ChartReports ChartReports { get; } public UsageReports.UsageReports UsageReports { get; } - public SeatsioClient(string secretKey, string baseUrl) + public SeatsioClient(string secretKey, long? accountId, string baseUrl) { - var restClient = CreateRestClient(secretKey, baseUrl); + var restClient = CreateRestClient(secretKey, accountId, baseUrl); Charts = new Charts.Charts(restClient); Events = new Events.Events(restClient); Accounts = new Accounts.Accounts(restClient); @@ -27,14 +27,23 @@ public SeatsioClient(string secretKey, string baseUrl) UsageReports = new UsageReports.UsageReports(restClient); } - public SeatsioClient(string secretKey): this(secretKey, "https://api.seatsio.net") + public SeatsioClient(string secretKey, long? accountId) : this(secretKey, accountId, "https://api.seatsio.net") { } - private static RestClient CreateRestClient(string secretKey, string baseUrl) + public SeatsioClient(string secretKey) : this(secretKey, null) + { + } + + private static RestClient CreateRestClient(string secretKey, long? accountId, string baseUrl) { var client = new RestClient(baseUrl); client.Authenticator = new HttpBasicAuthenticator(secretKey, null); + if (accountId != null) + { + client.AddDefaultHeader("X-Account-ID", accountId.ToString()); + } + return client; } } diff --git a/SeatsioDotNet/Subaccounts/Subaccount.cs b/SeatsioDotNet/Subaccounts/Subaccount.cs index bf772f7..aa5e91d 100644 --- a/SeatsioDotNet/Subaccounts/Subaccount.cs +++ b/SeatsioDotNet/Subaccounts/Subaccount.cs @@ -3,6 +3,7 @@ public class Subaccount { public long Id { get; set; } + public long AccountId { get; set; } public string SecretKey { get; set; } public string DesignerKey { get; set; } public string PublicKey { get; set; }