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

Side by Side 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 11 years ago
Left:
Right:
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 unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 Copyright 2011 Google Inc
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 using System;
18 using System.IO;
19 using System.Threading.Tasks;
20
21 using Google.Apis.Util.Store;
22 using Google.Apis.Json;
23
24 namespace Google.Apis.Apis.Util.Store
25 {
26 /// <summary>
27 /// File data store that implements <seealso cref="IDataStore"/>. This store creates a different file for each·
28 /// combination of type and key.
29 /// </summary>
30 public class FileDataStore : IDataStore
31 {
32 readonly string folderPath;
33 /// <summary>Gets the full folder path.</summary>
34 public string FolderPath { get { return folderPath; } }
35
36 /// <summary>
37 /// Constructs a new file data store with the specified folder. This fol der is created (if it doesn't exist·
38 /// yet) under <seealso cref="Environment.SpecialFolder.ApplicationData" />.
39 /// </summary>
40 /// <param name="folder">Folder name</param>
41 public FileDataStore(string folder)
42 {
43 folderPath = Path.Combine(Environment.GetFolderPath(Environment.Spec ialFolder.ApplicationData), folder);
44 if (!Directory.Exists(folderPath))
45 {
46 Directory.CreateDirectory(folderPath);
47 }
48 }
49
50 /// <summary>
51 /// Stores the given value for the given key. It creates a new file (nam ed <see cref="GetStoredKey"/>) in·
52 /// <see cref="FolderPath"/>.
53 /// </summary>
54 /// <typeparam name="T">The type to store in the data store</typeparam>
55 /// <param name="key">The key</param>
56 /// <param name="value">The value to store in the data store</param>
57 public Task Store<T>(string key, T value)
58 {
59 if (string.IsNullOrEmpty(key))
60 {
61 throw new ArgumentException("Key MUST have a value");
62 }
63
64 var serialized = NewtonsoftJsonSerializer.Instance.Serialize(value);
65 var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T)) );
66 File.WriteAllText(filePath, serialized);
67 return TaskEx.Delay(0);
68 }
69
70 /// <summary>
71 /// Deletes the given key. It deletes the <see cref="GetStoredKey"/> nam ed file in <see cref="FolderPath"/>.
72 /// </summary>
73 /// <param name="key">The key to delete from the data store</param>
74 /// <param name="t">The type of the stored value</param>
75 public Task Delete(string key, Type t)
76 {
77 if (string.IsNullOrEmpty(key))
78 {
79 throw new ArgumentException("Key MUST have a value");
80 }
81 if (t == null)
82 {
83 throw new ArgumentException("Type can't be null");
84 }
85
86 var filePath = Path.Combine(folderPath, GetStoredKey(key, t));
87 if (File.Exists(filePath))
88 {
89 File.Delete(filePath);
90 }
91 return TaskEx.Delay(0);
92 }
93
94 /// <summary>
95 /// Returns the stored value for the given key or <c>null</c> if the mat ching file (<see cref="GetStoredKey"/>
96 /// in <see cref="FolderPath"/> doesn't exist.
97 /// </summary>
98 /// <typeparam name="T">The type to retrieve</typeparam>
99 /// <param name="key">The key to retrieve from the data store</param>
100 /// <returns>The stored object</returns>
101 public Task<T> Get<T>(string key)
102 {
103 if (string.IsNullOrEmpty(key))
104 {
105 throw new ArgumentException("Key MUST have a value");
106 }
107
108 TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
109 var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T)) );
110 if (File.Exists(filePath))
111 {
112 try
113 {
114 var obj = File.ReadAllText(filePath);
115 tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize< T>(obj));
116 }
117 catch (Exception ex)
118 {
119 tcs.SetException(ex);
120 }
121 }
122 else
123 {
124 tcs.SetResult(default(T));
125 }
126 return tcs.Task;
127 }
128
129 /// <summary>
130 /// Clears all values in the data store. This method deletes all files i n <see cref="FolderPath"/>.
131 /// </summary>
132 public Task Clear()
133 {
134 if (Directory.Exists(folderPath))
135 {
136 Directory.Delete(folderPath, true);
137 Directory.CreateDirectory(folderPath);
138 }
139
140 return TaskEx.Delay(0);
141 }
142
143 /// <summary>Creates a unique stored key based on the key and the class type.</summary>
144 /// <param name="key">The object key</param>
145 /// <param name="t">The type to store or retrieve</param>
146 public static string GetStoredKey(string key, Type t)
147 {
148 return string.Format("{0}-{1}", t.FullName, key);
149 }
150 }
151 }
OLDNEW

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