Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(585)

Unified Diff: Src/GoogleApis.WinRT/Apis/Util/Store/StorageDataStore.cs

Issue 14433060: Issue 351: Reimplement OAuth2 (Step 6): WinRT Support (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Patch Set: Noam comments + rename Authenticate to Authorize Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Src/GoogleApis.Auth.WinRT/packages.config ('k') | Src/GoogleApis.WinRT/GoogleApis.WinRT.csproj » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Src/GoogleApis.WinRT/Apis/Util/Store/StorageDataStore.cs
===================================================================
new file mode 100644
--- /dev/null
+++ b/Src/GoogleApis.WinRT/Apis/Util/Store/StorageDataStore.cs
@@ -0,0 +1,85 @@
+/*
+Copyright 2013 Google Inc
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+using System;
+using System.IO;
+using System.Text;
+using System.Threading.Tasks;
+using Windows.Storage;
+
+using Google.Apis.Json;
+using Google.Apis.Logging;
+
+namespace Google.Apis.Util.Store
+{
+ /// <summary>
+ /// Windows Store data store that implements <see cref="IDataStore"/>. This store creates a different file for each
+ /// combination of type and key.
+ /// </summary>
+ public class StroageDataStore : IDataStore
+ {
+ // TODO(peleyal): consider adding also PasswordValutDataStore
+
+ private static readonly StorageFolder LocalFolder = ApplicationData.Current.LocalFolder;
+ private static readonly ILogger Logger = ApplicationContext.Logger.ForType<StroageDataStore>();
+
+ #region IDataStore Members
+
+ public async Task StoreAsync<T>(string key, T value)
+ {
+ StorageFile file = await LocalFolder.CreateFileAsync(GenerateStoredKey(key, typeof(T)),
+ CreationCollisionOption.ReplaceExisting);
+
+ var content = Encoding.UTF8.GetBytes(NewtonsoftJsonSerializer.Instance.Serialize(value));
+ using (var stream = await file.OpenStreamForWriteAsync())
+ {
+ await stream.WriteAsync(content, 0, content.Length);
+ }
+ }
+
+ public async Task DeleteAsync<T>(string key)
+ {
+ var storedKey = GenerateStoredKey(key, typeof(T));
+ var file = await LocalFolder.CreateFileAsync(storedKey, CreationCollisionOption.OpenIfExists);
+ await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
+ }
+
+ public async Task<T> GetAsync<T>(string key)
+ {
+ StorageFile file = await LocalFolder.CreateFileAsync(GenerateStoredKey(key, typeof(T)),
+ CreationCollisionOption.OpenIfExists);
+ var content = await FileIO.ReadTextAsync(file);
+ if (string.IsNullOrEmpty(content))
+ {
+ return default(T);
+ }
+ return NewtonsoftJsonSerializer.Instance.Deserialize<T>(content);
+
+ }
+
+ public async Task ClearAsync()
+ {
+ await LocalFolder.DeleteAsync();
+ }
+
+ #endregion
+
+ private string GenerateStoredKey(string key, Type t)
+ {
+ return string.Format("{0}-{1}", t.FullName, key);
+ }
+ }
+}
« no previous file with comments | « Src/GoogleApis.Auth.WinRT/packages.config ('k') | Src/GoogleApis.WinRT/GoogleApis.WinRT.csproj » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b