-
Notifications
You must be signed in to change notification settings - Fork 29
/
QboLocal.cs
34 lines (31 loc) · 1.41 KB
/
QboLocal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using Intuit.Ipp.OAuth2PlatformClient;
using System.Text.Json;
namespace QBO.Shared
{
public class QboLocal
{
public static QboAuthTokens? Tokens { get; set; } = null;
public static OAuth2Client? Client { get; set; } = null;
public static void Initialize(string path = ".\\Tokens.jsonc")
{
// Loading the tokens and client once (on sign-in/start up)
// and saving them in static properties saves us from
// deserializing again when we want to read or write the data.
Tokens = JsonSerializer.Deserialize<QboAuthTokens>(File.ReadAllText(path), new JsonSerializerOptions() {
ReadCommentHandling = JsonCommentHandling.Skip
}) ?? new();
// In the case that the data failed to deserialize, the ClientId
// and ClientSecret will be null, we need to make sure that's
// handled correctly.
if (!string.IsNullOrEmpty(Tokens.ClientId) && !string.IsNullOrEmpty(Tokens.ClientSecret)) {
Client = new(Tokens.ClientId, Tokens.ClientSecret, Tokens.RedirectUrl, Tokens.Environment);
}
else {
throw new InvalidDataException(
"The ClientId or ClientSecret was null or empty.\n" +
"Make sure that 'Tokens.jsonc' is setup with your credentials."
);
}
}
}
}