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

Unified Diff: Src/GoogleApis.DotNet4/Apis/Util/Store/FileDataStore.cs

Issue 13412046: Reimplement OAuth2 library - Step 1 (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Patch Set: minor Created 10 years, 6 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
Index: Src/GoogleApis.DotNet4/Apis/Util/Store/FileDataStore.cs
===================================================================
new file mode 100644
--- /dev/null
+++ b/Src/GoogleApis.DotNet4/Apis/Util/Store/FileDataStore.cs
@@ -0,0 +1,151 @@
+/*
+Copyright 2011 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.Threading.Tasks;
+
+using Google.Apis.Util.Store;
+using Google.Apis.Json;
+
+namespace Google.Apis.Apis.Util.Store
+{
+ /// <summary>
+ /// File data store that implements <seealso cref="IDataStore"/>. This store creates a different file for each
+ /// combination of type and key.
+ /// </summary>
+ public class FileDataStore : IDataStore
+ {
+ readonly string folderPath;
+ /// <summary>Gets the full folder path.</summary>
+ public string FolderPath { get { return folderPath; } }
+
+ /// <summary>
+ /// Constructs a new file data store with the specified folder. This folder is created (if it doesn't exist
+ /// yet) under <seealso cref="Environment.SpecialFolder.ApplicationData"/>.
+ /// </summary>
+ /// <param name="folder">Folder name</param>
+ public FileDataStore(string folder)
+ {
+ folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), folder);
+ if (!Directory.Exists(folderPath))
+ {
+ Directory.CreateDirectory(folderPath);
+ }
+ }
+
+ /// <summary>
+ /// Stores the given value for the given key. It creates a new file (named <see cref="GetStoredKey"/>) in
+ /// <see cref="FolderPath"/>.
+ /// </summary>
+ /// <typeparam name="T">The type to store in the data store</typeparam>
+ /// <param name="key">The key</param>
+ /// <param name="value">The value to store in the data store</param>
+ public Task Store<T>(string key, T value)
+ {
+ if (string.IsNullOrEmpty(key))
+ {
+ throw new ArgumentException("Key MUST have a value");
+ }
+
+ var serialized = NewtonsoftJsonSerializer.Instance.Serialize(value);
+ var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T)));
+ File.WriteAllText(filePath, serialized);
+ return TaskEx.Delay(0);
+ }
+
+ /// <summary>
+ /// Deletes the given key. It deletes the <see cref="GetStoredKey"/> named file in <see cref="FolderPath"/>.
+ /// </summary>
+ /// <param name="key">The key to delete from the data store</param>
+ /// <param name="t">The type of the stored value</param>
+ public Task Delete(string key, Type t)
+ {
+ if (string.IsNullOrEmpty(key))
+ {
+ throw new ArgumentException("Key MUST have a value");
+ }
+ if (t == null)
+ {
+ throw new ArgumentException("Type can't be null");
+ }
+
+ var filePath = Path.Combine(folderPath, GetStoredKey(key, t));
+ if (File.Exists(filePath))
+ {
+ File.Delete(filePath);
+ }
+ return TaskEx.Delay(0);
+ }
+
+ /// <summary>
+ /// Returns the stored value for the given key or <c>null</c> if the matching file (<see cref="GetStoredKey"/>
+ /// in <see cref="FolderPath"/> doesn't exist.
+ /// </summary>
+ /// <typeparam name="T">The type to retrieve</typeparam>
+ /// <param name="key">The key to retrieve from the data store</param>
+ /// <returns>The stored object</returns>
+ public Task<T> Get<T>(string key)
+ {
+ if (string.IsNullOrEmpty(key))
+ {
+ throw new ArgumentException("Key MUST have a value");
+ }
+
+ TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
+ var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T)));
+ if (File.Exists(filePath))
+ {
+ try
+ {
+ var obj = File.ReadAllText(filePath);
+ tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize<T>(obj));
+ }
+ catch (Exception ex)
+ {
+ tcs.SetException(ex);
+ }
+ }
+ else
+ {
+ tcs.SetResult(default(T));
+ }
+ return tcs.Task;
+ }
+
+ /// <summary>
+ /// Clears all values in the data store. This method deletes all files in <see cref="FolderPath"/>.
+ /// </summary>
+ public Task Clear()
+ {
+ if (Directory.Exists(folderPath))
+ {
+ Directory.Delete(folderPath, true);
+ Directory.CreateDirectory(folderPath);
+ }
+
+ return TaskEx.Delay(0);
+ }
+
+ /// <summary>Creates a unique stored key based on the key and the class type.</summary>
+ /// <param name="key">The object key</param>
+ /// <param name="t">The type to store or retrieve</param>
+ public static string GetStoredKey(string key, Type t)
+ {
+ return string.Format("{0}-{1}", t.FullName, key);
+ }
+ }
+}

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