1. Tatham Oddie
  2. Timeline

Commits

Tatham Oddie  committed 8c9b244

Introduce HealthGraph API Key via config

  • Participants
  • Parent commits 20e92d5
  • Branches default

Comments (0)

Files changed (4)

File .hgignore

View file
  • Ignore whitespace
 obj
 _ReSharper*
 .suo
-.user
+.user
+Web/Config/*

File Web/Controllers/TimelineController.cs

View file
  • Ignore whitespace
 using System.Linq;
 using System.Web.Mvc;
 using Web.Models;
+using Web.Services;
 using Web.Services.HealthGraph;
 
 namespace Web.Controllers
 {
     public class TimelineController : Controller
     {
+        SettingsProvider settings;
+
+        public TimelineController()
+        {
+            settings = new SettingsProvider(() => HttpContext);
+        }
+
         public ActionResult Index()
         {
             var activities = new List<ActivityViewModel>(new[]
                 }
             });
 
-            var runs = new HealthGraphClient("")
+            var runs = new HealthGraphClient(settings.HealthGraphApiKey())
                 .GetFitnessActivities()
                 .Reverse()
                 .Where(f => f.type == "Running")

File Web/Services/SettingsProvider.cs

View file
  • Ignore whitespace
+using System;
+using System.Configuration;
+using System.IO;
+using System.Web;
+
+namespace Web.Services
+{
+    public class SettingsProvider
+    {
+        readonly Func<HttpContextBase> contextAccessor;
+
+        public SettingsProvider(Func<HttpContextBase> contextAccessor)
+        {
+            this.contextAccessor = contextAccessor;
+        }
+
+        public string ReadSetting(string name)
+        {
+            var context = contextAccessor();
+            var overridePath = context.Server.MapPath(string.Format("~/Config/{0}.txt", name));
+            if (File.Exists(overridePath))
+                return File.ReadAllText(overridePath);
+
+            return ConfigurationManager.AppSettings[name];
+        }
+    }
+
+    public static class SettingsHelpers
+    {
+        public static string HealthGraphApiKey(this SettingsProvider provider)
+        {
+            return provider.ReadSetting("HealthGraph.ApiKey");
+        }
+    }
+}

File Web/Web.csproj

View file
  • Ignore whitespace
     <Compile Include="Services\HealthGraph\FitnessActivityApiResponse.cs" />
     <Compile Include="Services\HealthGraph\HealthGraphClient.cs" />
     <Compile Include="Services\HealthGraph\UserApiResponse.cs" />
+    <Compile Include="Services\SettingsProvider.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="Content\noun_project_246.svg" />