Skip to content

Commit bb49ae6

Browse files
authored
Merge pull request #14 from bunq/13-api-context-json
add methods to serialize and de-serialize ApiContext, fix Card [#13]
2 parents 02bcaa5 + 0beeed9 commit bb49ae6

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Bunq.Sdk.Context;
2+
using Xunit;
3+
4+
namespace Bunq.Sdk.Tests.Context
5+
{
6+
/// <summary>
7+
/// Tests:
8+
/// ApiContext
9+
/// </summary>
10+
public class ApiContextTest : BunqSdkTestBase, IClassFixture<ApiContextTest>
11+
{
12+
/// <summary>
13+
/// Path to a temporary context file.
14+
/// </summary>
15+
private const string CONTEXT_FILENAME_TEST = "context-save-restore-test.conf";
16+
17+
private static ApiContext apiContext;
18+
19+
public ApiContextTest()
20+
{
21+
if (apiContext == null) apiContext = GetApiContext();
22+
}
23+
24+
/// <summary>
25+
/// Tests serialization and de-serialization of the API context.
26+
/// </summary>
27+
[Fact]
28+
public void TestApiContextSerializeDeserialize()
29+
{
30+
var apiContextJson = apiContext.ToJson();
31+
var apiContextDeSerialised = ApiContext.FromJson(apiContextJson);
32+
33+
Assert.Equal(apiContextJson, apiContextDeSerialised.ToJson());
34+
}
35+
36+
/// <summary>
37+
/// Tests saving and restoring of the API context.
38+
/// </summary>
39+
[Fact]
40+
public void TestApiContextSaveRestore()
41+
{
42+
var apiContextJson = apiContext.ToJson();
43+
apiContext.Save(CONTEXT_FILENAME_TEST);
44+
var apiContextRestored = ApiContext.Restore(CONTEXT_FILENAME_TEST);
45+
46+
Assert.Equal(apiContextJson, apiContextRestored.ToJson());
47+
}
48+
}
49+
}

BunqSdk/Context/ApiContext.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,22 @@ public void Save(string fileName)
201201
{
202202
try
203203
{
204-
File.WriteAllText(fileName, BunqJsonConvert.SerializeObject(this), ENCODING_BUNQ_CONF);
204+
File.WriteAllText(fileName, ToJson(), ENCODING_BUNQ_CONF);
205205
}
206206
catch (IOException exception)
207207
{
208208
throw new BunqException(ERROR_COULD_NOT_SAVE_API_CONTEXT, exception);
209209
}
210210
}
211211

212+
/// <summary>
213+
/// Serialize the API Context to JSON.
214+
/// </summary>
215+
public string ToJson()
216+
{
217+
return BunqJsonConvert.SerializeObject(this);
218+
}
219+
212220
/// <summary>
213221
/// Restores a context from a default location.
214222
/// </summary>
@@ -224,16 +232,22 @@ public static ApiContext Restore(string fileName)
224232
{
225233
try
226234
{
227-
var apiContextJson = File.ReadAllText(fileName, ENCODING_BUNQ_CONF);
228-
229-
return BunqJsonConvert.DeserializeObject<ApiContext>(apiContextJson);
235+
return FromJson(File.ReadAllText(fileName, ENCODING_BUNQ_CONF));
230236
}
231237
catch (IOException exception)
232238
{
233239
throw new BunqException(ERROR_COULD_NOT_RESTORE_API_CONTEXT, exception);
234240
}
235241
}
236242

243+
/// <summary>
244+
/// De-serializes a context from JSON.
245+
/// </summary>
246+
public static ApiContext FromJson(string json)
247+
{
248+
return BunqJsonConvert.DeserializeObject<ApiContext>(json);
249+
}
250+
237251
/// <summary>
238252
/// Returns the base URI of the environment assigned to the API context.
239253
/// </summary>

BunqSdk/Model/Generated/Card.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public class Card : BunqModel
136136
/// Array of Types, PINs, account IDs assigned to the card.
137137
/// </summary>
138138
[JsonProperty(PropertyName = "pin_code_assignment")]
139-
public CardPinAssignment PinCodeAssignment { get; private set; }
139+
public List<CardPinAssignment> PinCodeAssignment { get; private set; }
140140

141141
public static BunqResponse<Card> Update(ApiContext apiContext, IDictionary<string, object> requestMap,
142142
int userId, int cardId)

0 commit comments

Comments
 (0)