Skip to content

Commit 8f45eb2

Browse files
Add better, more flexible APIs for SDK initialization.
This PR hopes to unify some of these initialization settings int o a single 'configuration' class that can be easily used to intialize parse in a much cleaner way, especially as we add even more initialization options. Depends on #77.
1 parent 0bec459 commit 8f45eb2

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

Parse/Internal/Command/ParseCommand.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ public ParseCommand(string relativeUri,
5050
Method = method;
5151
Data = stream;
5252

53+
// TODO (richardross): Inject configuration instead of using shared static here.
5354
Headers = new List<KeyValuePair<string, string>> {
54-
new KeyValuePair<string, string>("X-Parse-Application-Id", ParseClient.ApplicationId),
55+
new KeyValuePair<string, string>("X-Parse-Application-Id", ParseClient.CurrentConfiguration.ApplicationId),
56+
new KeyValuePair<string, string>("X-Parse-Windows-Key", ParseClient.CurrentConfiguration.WindowsKey),
5557
new KeyValuePair<string, string>("X-Parse-Client-Version", ParseClient.VersionString),
5658
new KeyValuePair<string, string>("X-Parse-Installation-Id", ParseClient.InstallationId.ToString())
5759
};
@@ -71,10 +73,12 @@ public ParseCommand(string relativeUri,
7173
if (!string.IsNullOrEmpty(ParseClient.PlatformHooks.OSVersion)) {
7274
Headers.Add(new KeyValuePair<string, string>("X-Parse-OS-Version", ParseClient.PlatformHooks.OSVersion));
7375
}
76+
// TODO (richardross): I hate the idea of having this super tightly coupled static variable in here.
77+
// Lets eventually get rid of it.
7478
if (!string.IsNullOrEmpty(ParseClient.MasterKey)) {
7579
Headers.Add(new KeyValuePair<string, string>("X-Parse-Master-Key", ParseClient.MasterKey));
7680
} else {
77-
Headers.Add(new KeyValuePair<string, string>("X-Parse-Windows-Key", ParseClient.WindowsKey));
81+
Headers.Add(new KeyValuePair<string, string>("X-Parse-Windows-Key", ParseClient.CurrentConfiguration.WindowsKey));
7882
}
7983
if (!string.IsNullOrEmpty(sessionToken)) {
8084
Headers.Add(new KeyValuePair<string, string>("X-Parse-Session-Token", sessionToken));

Parse/Public/ParseClient.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ public static partial class ParseClient {
2828
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'f'Z'",
2929
};
3030

31+
/// <summary>
32+
/// Represents the configuration of the Parse SDK.
33+
/// </summary>
34+
public struct Configuration {
35+
/// <summary>
36+
/// The Parse.com application ID of your app.
37+
/// </summary>
38+
public String ApplicationId { get; set; }
39+
40+
/// <summary>
41+
/// The Parse.com .NET key for your app.
42+
/// </summary>
43+
public String WindowsKey { get; set; }
44+
}
3145

3246
private static readonly object mutex = new object();
3347
private static readonly string[] assemblyNames = {
@@ -60,10 +74,12 @@ private static Type GetParseType(string name) {
6074
private static readonly IParseCommandRunner commandRunner;
6175
internal static IParseCommandRunner ParseCommandRunner { get { return commandRunner; } }
6276

77+
/// <summary>
78+
/// The current configuration that parse has been initialized with.
79+
/// </summary>
80+
public static Configuration CurrentConfiguration { get; internal set; }
6381
internal static Uri HostName { get; set; }
6482
internal static string MasterKey { get; set; }
65-
internal static string ApplicationId { get; set; }
66-
internal static string WindowsKey { get; set; }
6783

6884
internal static Version Version {
6985
get {
@@ -90,10 +106,24 @@ internal static string VersionString {
90106
/// <param name="dotnetKey">The .NET API Key provided in the Parse dashboard.
91107
/// </param>
92108
public static void Initialize(string applicationId, string dotnetKey) {
109+
Initialize(new Configuration {
110+
ApplicationId = applicationId,
111+
WindowsKey = dotnetKey
112+
});
113+
}
114+
115+
/// <summary>
116+
/// Authenticates this client as belonging to your application. This must be
117+
/// called before your application can use the Parse library. The recommended
118+
/// way is to put a call to <c>ParseFramework.Initialize</c> in your
119+
/// Application startup.
120+
/// </summary>
121+
/// <param name="configuration">The configuration to initialze Parse with.
122+
/// </param>
123+
public static void Initialize(Configuration configuration) {
93124
lock (mutex) {
94125
HostName = HostName ?? new Uri("https://api.parse.com/1/");
95-
ApplicationId = applicationId;
96-
WindowsKey = dotnetKey;
126+
CurrentConfiguration = configuration;
97127

98128
ParseObject.RegisterSubclass<ParseUser>();
99129
ParseObject.RegisterSubclass<ParseInstallation>();

0 commit comments

Comments
 (0)