/// <summary> /// Gets a index from isolated storage of the device. Returns null, if the index isn't found /// </summary> /// <typeparam name="T"></typeparam> /// <param name="requestURL"></param> /// <param name="collectionIdentifier"></param> /// <returns>The index file or null, if the index file isn't found</returns> public static async Task <T> getIndexAsync <T>(string requestURL, IonConfig config) where T : CacheIndex { try { // Generate filePath for index string filePath = FilePaths.getCacheIndicesFolderPath(config) + FilePaths.getFileName(requestURL) + IonConstants.JsonFileExtension; T cacheIndex = null; using (await fileLocks.ObtainLock(filePath).LockAsync().ConfigureAwait(false)) { // Create file or use existing file StorageFile file = await _localFolder.CreateFileAsync(filePath, CreationCollisionOption.OpenIfExists); // Read content of the file string content = await FileIO.ReadTextAsync(file); // Deserialize cache index cacheIndex = JsonConvert.DeserializeObject <T>(content); } fileLocks.ReleaseLock(filePath); return(cacheIndex); } catch (Exception e) { IonLogging.log("Error loading cacheIndex " + requestURL + " from isolated storage: " + e.Message, IonLogMessageTypes.ERROR); return(null); } }
/// <summary> /// Loads a collection from the isolated storage folder /// </summary> /// <param name="collectionIdentifier"></param> /// <returns>The collection or null, if the collection isn't found</returns> public static async Task <IonCollection> loadCollectionFromIsolatedStorageAsync(IonConfig config) { try { // Generate filePath for collection string filePath = FilePaths.getCollectionFolderPath(config) + config.collectionIdentifier + IonConstants.JsonFileExtension; IonCollection collection = null; using (await fileLocks.ObtainLock(filePath).LockAsync().ConfigureAwait(false)) { // Open file StorageFile file = await _localFolder.GetFileAsync(filePath); // Extract content string content = await FileIO.ReadTextAsync(file); // Deserialize collection collection = JsonConvert.DeserializeObject <IonCollection>(content); } fileLocks.ReleaseLock(filePath); return(collection); } catch (Exception e) { IonLogging.log("Error loading collection " + config.collectionIdentifier + " from isolated storeage. Message: " + e.Message, IonLogMessageTypes.ERROR); return(null); } }
/// <summary> /// Saves a index file to the isolated storage on the device /// </summary> /// <typeparam name="T"></typeparam> /// <param name="requestURL"></param> /// <param name="cacheIndex"></param> /// <param name="collectionIdentifier"></param> /// <returns></returns> public static async Task saveIndexAsync <T>(string requestURL, T cacheIndex, IonConfig config) where T : CacheIndex { try { // Generate filePath for index string filePath = FilePaths.getCacheIndicesFolderPath(config) + FilePaths.getFileName(requestURL) + IonConstants.JsonFileExtension; using (await fileLocks.ObtainLock(filePath).LockAsync().ConfigureAwait(false)) { // Open an existing file or create a new one StorageFile file = await _localFolder.CreateFileAsync(filePath, CreationCollisionOption.ReplaceExisting); // Serialize cache índex string cacheIndexSerialized = JsonConvert.SerializeObject(cacheIndex); // Write serialzed collection to file await FileIO.WriteTextAsync(file, cacheIndexSerialized); } fileLocks.ReleaseLock(filePath); } catch (Exception e) { IonLogging.log("Error saving cacheIndex to isolated storage. Message: " + e.Message, IonLogMessageTypes.ERROR); } }
/// <summary> /// Saves a page to the isolated storage folder /// </summary> /// <param name="page"></param> /// <returns></returns> public static async Task savePageToIsolatedStorageAsync(IonPage page, IonConfig config) { try { // Generate filePath for page string filePath = FilePaths.getPagesFolderPath(config) + page.identifier + IonConstants.JsonFileExtension; using (await fileLocks.ObtainLock(filePath).LockAsync().ConfigureAwait(false)) { // Create file or use existing file StorageFile file = await _localFolder.CreateFileAsync(filePath, CreationCollisionOption.ReplaceExisting); // Serialize collection string pageSerialized = JsonConvert.SerializeObject(page); // Write serialzed collection to file await FileIO.WriteTextAsync(file, pageSerialized); } fileLocks.ReleaseLock(filePath); } catch (Exception e) { IonLogging.log("Error saving page to isolated storage: " + e.Message, IonLogMessageTypes.ERROR); } }
/// <summary> /// Used to get a collection that is newer than the given date. Otherwise the content is empty /// </summary> /// <param name="identifier"></param> /// <param name="lastModified"></param> /// <returns></returns> public async Task <HttpResponseMessage> getCollectionAsync(string identifier, DateTime lastModified) { try { // Construct request string string requestString = _config.baseUrl + _config.locale + IonConstants.Slash + _config.collectionIdentifier + IonConstants.QueryBegin + IonConstants.QueryVariation + _config.variation; // Add the last-modified-header _client.DefaultRequestHeaders.Add("If-Modified-Since", lastModified.ToString("r")); // Recieve the response HttpResponseMessage response = await _client.GetAsync(requestString).ConfigureAwait(false); // Remove the last-modified-header _client.DefaultRequestHeaders.Remove("If-Modified-Since"); return(response); } catch (Exception e) { IonLogging.log("Error getting collection from server: " + e.Message, IonLogMessageTypes.ERROR); return(null); } }
/// <summary> /// Constructor with config file for initialization /// </summary> /// <param name="config"></param> public DataClient(IonConfig config) { try { _config = config; _client = new HttpClient(); _client.DefaultRequestHeaders.Authorization = _config.authenticationHeader; } catch (Exception e) { IonLogging.log("Error in configuring the data client: " + e.Message, IonLogMessageTypes.ERROR); } }
/// <summary> /// Used to get a page with a given identifier /// </summary> /// <param name="identifier"></param> /// <returns>Already parsed IonPage</returns> public async Task <HttpResponseMessage> getPageAsync(string identifier) { try { string requestString = _config.baseUrl + _config.locale + IonConstants.Slash + _config.collectionIdentifier + IonConstants.Slash + identifier + IonConstants.QueryBegin + IonConstants.QueryVariation + _config.variation; HttpResponseMessage response = await _client.GetAsync(requestString).ConfigureAwait(false); return(response); } catch (Exception e) { IonLogging.log("Error getting page response from server! " + e.Message, IonLogMessageTypes.ERROR); return(null); } }