Skip to content

Commit

Permalink
API calls take optional account ID
Browse files Browse the repository at this point in the history
  • Loading branch information
mroloux committed Sep 29, 2019
1 parent 2355d3e commit 283b08f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 6 deletions.
18 changes: 18 additions & 0 deletions SeatsioDotNet.Test/AccountIdAuthenticationTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion SeatsioDotNet.Test/ErrorHandlingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Test4xx()
public void WeirdError()
{
var e = Assert.Throws<SeatsioException>(() =>
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);
Expand Down
1 change: 1 addition & 0 deletions SeatsioDotNet.Test/HoldTokens/RetrieveHoldTokenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
7 changes: 6 additions & 1 deletion SeatsioDotNet.Test/SeatsioClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
1 change: 1 addition & 0 deletions SeatsioDotNet.Test/Subaccounts/RetrieveSubaccountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
1 change: 1 addition & 0 deletions SeatsioDotNet/HoldTokens/HoldToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
17 changes: 13 additions & 4 deletions SeatsioDotNet/SeatsioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions SeatsioDotNet/Subaccounts/Subaccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down

0 comments on commit 283b08f

Please sign in to comment.