/// <summary> /// Stores the CacheExpirationRegister to disk. /// </summary> public virtual void SaveCacheState() { try { File.WriteAllLines(Config.ExpirationRegister, CacheExpirationRegister.Select(x => x.Key + "," + x.Value.ToString(CultureInfo.InvariantCulture))); } catch (DirectoryNotFoundException) { Directory.CreateDirectory(Config.CachePath); SaveCacheState(); } }
public override T Request <T>(Uri uri) { DateTime cachedUntil; if (CacheExpirationRegister.TryGetValue(uri, out cachedUntil)) { } var client = new WebClient(); string data = client.DownloadString(uri); throw new NotImplementedException(); }
/// <summary> /// Performs a request to the specified URI and returns an EveApiResponse of specified type. /// </summary> /// <typeparam name="T">The type parameter for the xml response.</typeparam> /// <param name="uri">The URI to request.</param> /// <returns></returns> public override T Request <T>(Uri uri) { DateTime cachedUntil; bool fromCache = CacheExpirationRegister.TryGetValue(uri, out cachedUntil) && DateTime.UtcNow < cachedUntil; string data = ""; HttpWebRequest request = HttpRequestHelper.CreateRequest(uri); request.ContentType = ContentType; request.CachePolicy = fromCache ? new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable) : new HttpRequestCachePolicy(HttpRequestCacheLevel.Reload); request.Proxy = null; try { data = HttpRequestHelper.GetContent(request); } catch (WebException e) { var response = (HttpWebResponse)e.Response; Debug.WriteLine("From cache: " + response.IsFromCache); if (response.StatusCode != HttpStatusCode.BadRequest) { throw new InvalidRequestException("Request caused a WebException.", e); } Stream responseStream = response.GetResponseStream(); if (responseStream == null) { throw new InvalidRequestException("Request caused a WebException.", e); } using (var reader = new StreamReader(responseStream)) { data = reader.ReadToEnd(); var error = Serializer.Deserialize <EveApiError>(data); throw new InvalidRequestException(error.Error.ErrorCode, error.Error.ErrorText, e); } } var xml = Serializer.Deserialize <T>(data); register(uri, xml); SaveCacheState(); return(xml); }
private void register(Uri uri, dynamic xml) { //if (o.GetType().Is) throw new System.Exception("Should never occur."); // TODO type check CacheExpirationRegister.AddOrUpdate(uri, xml.CachedUntil); }