Commits

lfoust committed 96383a3

Added support for apiKeys

Comments (0)

Files changed (6)

trunk/data/V2.0 Upgrade Checklist.xlsx

Binary file modified.

trunk/source/Stacky/Helpers/UrlHelper.cs

         private static Uri BuildUrl(string method, string version, string[] urlParameters, string queryString)
         {
             Require.NotNullOrEmpty(method, "method");
+            Require.NotNullOrEmpty(version, "version");
 
             string urlBase = String.Format(CultureInfo.CurrentCulture, "https://api.stackexchange.com/{0}/{1}/", version, method);
             if (urlParameters != null)

trunk/source/Stacky/StackyClient/StackyClient.cs

     {
         private string version;
 
-        public StackyClient(string version, Site site, IUrlClient client, IProtocol protocol)
-            : this(version, site.ApiSiteParameter, client, protocol)
+        public StackyClient(string version, Site site, IUrlClient client, IProtocol protocol, string apiKey = null)
+            : this(version, site.ApiSiteParameter, client, protocol, apiKey)
         {
         }
 
-        public StackyClient(string version, string siteUrlName, IUrlClient client, IProtocol protocol)
+        public StackyClient(string version, string siteUrlName, IUrlClient client, IProtocol protocol, string apiKey = null)
         {
             Require.NotNullOrEmpty(version, "version");
             Require.NotNullOrEmpty(siteUrlName, "baseUrl");
             SiteUrlName = siteUrlName;
             WebClient = client;
             Protocol = protocol;
+            ApiKey = apiKey;
         }
 
         #region Methods
 
         public HttpResponse GetResponse(string method, string[] urlArguments, Dictionary<string, string> queryStringArguments)
         {
+            EnsureApiKey(queryStringArguments);
             Uri url = UrlHelper.BuildUrl(method, version, urlArguments, queryStringArguments);
             return WebClient.MakeRequest(url);
         }

trunk/source/Stacky/StackyClientAsync/StackyClientAsync.cs

         private string version;
 
 #if SILVERLIGHT
-        public StackyClient(string version, Site site, IUrlClient client, IProtocol protocol)
+        public StackyClient(string version, Site site, IUrlClient client, IProtocol protocol, string apiKey)
 #else
-        public StackyClientAsync(string version, Site site, IUrlClientAsync client, IProtocol protocol)
+        public StackyClientAsync(string version, Site site, IUrlClientAsync client, IProtocol protocol, string apiKey = null)
 #endif
-            : this(version, site.ApiSiteParameter, client, protocol)
+            : this(version, site.ApiSiteParameter, client, protocol, apiKey)
         {
         }
 
 #if SILVERLIGHT
-        public StackyClient(string version, string siteUrlName, IUrlClient client, IProtocol protocol)
+        public StackyClient(string version, string siteUrlName, IUrlClient client, IProtocol protocol, string apiKey)
 #else
-        public StackyClientAsync(string version, string siteUrlName, IUrlClientAsync client, IProtocol protocol)
+        public StackyClientAsync(string version, string siteUrlName, IUrlClientAsync client, IProtocol protocol, string apiKey = null)
 #endif
         {
             Require.NotNullOrEmpty(version, "version");
             WebClient = client;
             SiteUrlName = siteUrlName;
             Protocol = protocol;
+            ApiKey = apiKey;
         }
 
         #region Properties
 
         public virtual void GetResponse(string method, string[] urlArguments, Dictionary<string, string> queryStringArguments, Action<HttpResponse> onSuccess, Action<ApiException> onError)
         {
+            EnsureApiKey(queryStringArguments);
             Uri url = UrlHelper.BuildUrl(method, version, urlArguments, queryStringArguments);
             WebClient.MakeRequest(url, onSuccess, onError);
         }

trunk/source/Stacky/StackyClientBase.cs

     {
         public IProtocol Protocol { get; set; }
         public string SiteUrlName { get; set; }
+        public string ApiKey { get; set; }
 
         public int RemainingRequests { get; internal set; }
         public int MaxRequests { get; internal set; }
                 return UnixDateTime.UnixTimeFromDate(date.Value);
             return null;
         }
+
+        protected void EnsureApiKey(IDictionary<string, string> queryStringArguments)
+        {
+            if (!String.IsNullOrEmpty(ApiKey) && !queryStringArguments.ContainsKey("key"))
+            {
+                queryStringArguments.Add("key", ApiKey);
+            }
+        }
     }
 }

trunk/source/Tests/Stacky.IntegrationTests/IntegrationTest.cs

     public abstract class IntegrationTest
     {
         public static string Version = "2.0";
-        public static string ApiKey = "LU9IfwI8IEScM3yYAjHimA";
+        public static string ApiKey = "vvfp9MI3DHe4I5dLjQzTXQ((";
 
         public IntegrationTest()
         {
-            Client = new StackyClient(Version, Sites.StackOverflow, new UrlClient(), new JsonProtocol());
-            ClientAsync = new StackyClientAsync(Version, Sites.StackOverflow.ApiSiteParameter, new UrlClientAsync(), new JsonProtocol());
+            Client = new StackyClient(Version, Sites.StackOverflow, new UrlClient(), new JsonProtocol(), ApiKey);
+            ClientAsync = new StackyClientAsync(Version, Sites.StackOverflow.ApiSiteParameter, new UrlClientAsync(), new JsonProtocol(), ApiKey);
         }
 
         public StackyClient Client { get; set; }