From 8f45eb2613014a402cef31adc4809c35399d4a23 Mon Sep 17 00:00:00 2001 From: Richard Ross Date: Mon, 16 Nov 2015 15:21:51 -0800 Subject: [PATCH] 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. --- Parse/Internal/Command/ParseCommand.cs | 8 ++++-- Parse/Public/ParseClient.cs | 38 +++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Parse/Internal/Command/ParseCommand.cs b/Parse/Internal/Command/ParseCommand.cs index 40989a7e..d108e61e 100644 --- a/Parse/Internal/Command/ParseCommand.cs +++ b/Parse/Internal/Command/ParseCommand.cs @@ -50,8 +50,10 @@ public ParseCommand(string relativeUri, Method = method; Data = stream; + // TODO (richardross): Inject configuration instead of using shared static here. Headers = new List> { - new KeyValuePair("X-Parse-Application-Id", ParseClient.ApplicationId), + new KeyValuePair("X-Parse-Application-Id", ParseClient.CurrentConfiguration.ApplicationId), + new KeyValuePair("X-Parse-Windows-Key", ParseClient.CurrentConfiguration.WindowsKey), new KeyValuePair("X-Parse-Client-Version", ParseClient.VersionString), new KeyValuePair("X-Parse-Installation-Id", ParseClient.InstallationId.ToString()) }; @@ -71,10 +73,12 @@ public ParseCommand(string relativeUri, if (!string.IsNullOrEmpty(ParseClient.PlatformHooks.OSVersion)) { Headers.Add(new KeyValuePair("X-Parse-OS-Version", ParseClient.PlatformHooks.OSVersion)); } + // TODO (richardross): I hate the idea of having this super tightly coupled static variable in here. + // Lets eventually get rid of it. if (!string.IsNullOrEmpty(ParseClient.MasterKey)) { Headers.Add(new KeyValuePair("X-Parse-Master-Key", ParseClient.MasterKey)); } else { - Headers.Add(new KeyValuePair("X-Parse-Windows-Key", ParseClient.WindowsKey)); + Headers.Add(new KeyValuePair("X-Parse-Windows-Key", ParseClient.CurrentConfiguration.WindowsKey)); } if (!string.IsNullOrEmpty(sessionToken)) { Headers.Add(new KeyValuePair("X-Parse-Session-Token", sessionToken)); diff --git a/Parse/Public/ParseClient.cs b/Parse/Public/ParseClient.cs index db3457fe..bbcfdb61 100644 --- a/Parse/Public/ParseClient.cs +++ b/Parse/Public/ParseClient.cs @@ -28,6 +28,20 @@ public static partial class ParseClient { "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'f'Z'", }; + /// + /// Represents the configuration of the Parse SDK. + /// + public struct Configuration { + /// + /// The Parse.com application ID of your app. + /// + public String ApplicationId { get; set; } + + /// + /// The Parse.com .NET key for your app. + /// + public String WindowsKey { get; set; } + } private static readonly object mutex = new object(); private static readonly string[] assemblyNames = { @@ -60,10 +74,12 @@ private static Type GetParseType(string name) { private static readonly IParseCommandRunner commandRunner; internal static IParseCommandRunner ParseCommandRunner { get { return commandRunner; } } + /// + /// The current configuration that parse has been initialized with. + /// + public static Configuration CurrentConfiguration { get; internal set; } internal static Uri HostName { get; set; } internal static string MasterKey { get; set; } - internal static string ApplicationId { get; set; } - internal static string WindowsKey { get; set; } internal static Version Version { get { @@ -90,10 +106,24 @@ internal static string VersionString { /// The .NET API Key provided in the Parse dashboard. /// public static void Initialize(string applicationId, string dotnetKey) { + Initialize(new Configuration { + ApplicationId = applicationId, + WindowsKey = dotnetKey + }); + } + + /// + /// Authenticates this client as belonging to your application. This must be + /// called before your application can use the Parse library. The recommended + /// way is to put a call to ParseFramework.Initialize in your + /// Application startup. + /// + /// The configuration to initialze Parse with. + /// + public static void Initialize(Configuration configuration) { lock (mutex) { HostName = HostName ?? new Uri("https://api.parse.com/1/"); - ApplicationId = applicationId; - WindowsKey = dotnetKey; + CurrentConfiguration = configuration; ParseObject.RegisterSubclass(); ParseObject.RegisterSubclass();