public override void Write(Stream outputStream, ICacheItem cacheItem) { ResponseCacheItem item = (ResponseCacheItem)cacheItem; StringBuilder result = new StringBuilder(); result.Append(item.Url + "\n"); result.Append(item.CreationTime.Ticks.ToString("0")); Utils.WriteString(outputStream, result.ToString()); Utils.WriteString(outputStream, item.Response); }
public override void Write(Stream outputStream, ICacheItem cacheItem) { ResponseCacheItem item = (ResponseCacheItem)cacheItem; StringBuilder result = new StringBuilder(); result.Append(item.Url.AbsoluteUri + "\n"); result.Append(item.CreationTime.Ticks.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); UtilityMethods.WriteString(outputStream, result.ToString()); UtilityMethods.WriteString(outputStream, item.Response); }
public override ICacheItem Read(Stream inputStream) { string s = UtilityMethods.ReadString(inputStream); string response = UtilityMethods.ReadString(inputStream); string[] chunks = s.Split('\n'); // Corrupted cache record, so throw IOException which is then handled and returns partial cache. if (chunks.Length != 2) { throw new IOException("Unexpected number of chunks found"); } string url = chunks[0]; DateTime creationTime = new DateTime(long.Parse(chunks[1], System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo)); ResponseCacheItem item = new ResponseCacheItem(new Uri(url), response, creationTime); return(item); }
public override ICacheItem Read(Stream inputStream) { string s = Utils.ReadString(inputStream); string response = Utils.ReadString(inputStream); string[] chunks = s.Split('\n'); // Corrupted cache record, so throw IOException which is then handled and returns partial cache. if (chunks.Length != 2) { throw new IOException("Unexpected number of chunks found"); } string url = chunks[0]; DateTime creationTime = new DateTime(long.Parse(chunks[1])); ResponseCacheItem item = new ResponseCacheItem(); item.Url = url; item.CreationTime = creationTime; item.Response = response; return(item); }
private T GetResponse <T>(Dictionary <string, string> parameters, TimeSpan cacheTimeout) where T : IFlickrParsable, new() { // Flow for GetResponse. // 1. Check API Key // 2. Calculate Cache URL. // 3. Check Cache for URL. // 4. Get Response if not in cache. // 5. Write Cache. // 6. Parse Response. CheckApiKey(); parameters["api_key"] = ApiKey; // If performing one of the old 'flickr.auth' methods then use old authentication details. var method = parameters["method"]; // User of obsolete AuthToken property while we transition over to the new OAuth authentication process. #pragma warning disable 612,618 if (method.StartsWith("flickr.auth") && !method.EndsWith("oauth.checkToken")) { if (!String.IsNullOrEmpty(AuthToken)) { parameters["auth_token"] = AuthToken; } } else { // If OAuth Token exists or no authentication required then use new OAuth if (!String.IsNullOrEmpty(OAuthAccessToken) || String.IsNullOrEmpty(AuthToken)) { parameters.Remove("api_key"); OAuthGetBasicParameters(parameters); if (!String.IsNullOrEmpty(OAuthAccessToken)) { parameters["oauth_token"] = OAuthAccessToken; } } else { parameters["auth_token"] = AuthToken; } } #pragma warning restore 612,618 var url = CalculateUri(parameters, !String.IsNullOrEmpty(sharedSecret)); lastRequest = url; string responseXml; if (InstanceCacheDisabled) { responseXml = FlickrResponder.GetDataResponse(this, BaseUri.AbsoluteUri, parameters); } else { var urlComplete = url; var cached = (ResponseCacheItem)Cache.Responses.Get(urlComplete, cacheTimeout, true); if (cached != null) { Debug.WriteLine("Cache hit."); responseXml = cached.Response; } else { Debug.WriteLine("Cache miss."); responseXml = FlickrResponder.GetDataResponse(this, BaseUri.AbsoluteUri, parameters); var resCache = new ResponseCacheItem(new Uri(urlComplete), responseXml, DateTime.UtcNow); Cache.Responses.Shrink(Math.Max(0, Cache.CacheSizeLimit - responseXml.Length)); Cache.Responses[urlComplete] = resCache; } } lastResponse = responseXml; //responseXml = responseXml.Trim(); var reader = new XmlTextReader(new StringReader(responseXml)) { WhitespaceHandling = WhitespaceHandling.None, }; if (!reader.ReadToDescendant("rsp")) { throw new XmlException("Unable to find response element 'rsp' in Flickr response"); } while (reader.MoveToNextAttribute()) { if (reader.LocalName == "stat" && reader.Value == "fail") { throw ExceptionHandler.CreateResponseException(reader); } } reader.MoveToElement(); reader.Read(); var item = new T(); item.Load(reader); return(item); }
public override ICacheItem Read(Stream inputStream) { string s = UtilityMethods.ReadString(inputStream); string response = UtilityMethods.ReadString(inputStream); string[] chunks = s.Split('\n'); // Corrupted cache record, so throw IOException which is then handled and returns partial cache. if (chunks.Length != 2) throw new IOException("Unexpected number of chunks found"); string url = chunks[0]; var creationTime = new DateTime(long.Parse(chunks[1], System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo)); var item = new ResponseCacheItem(new Uri(url), response, creationTime); return item; }
private T GetResponse <T>(Dictionary <string, string> parameters, TimeSpan cacheTimeout) where T : IFlickrParsable, new() { // Flow for GetResponse. // 1. Check API Key // 2. Calculate Cache URL. // 3. Check Cache for URL. // 4. Get Response if not in cache. // 5. Write Cache. // 6. Parse Response. CheckApiKey(); parameters["api_key"] = ApiKey; // If performing one of the old 'flickr.auth' methods then use old authentication details. var method = parameters["method"]; // User of obsolete AuthToken property while we transition over to the new OAuth authentication process. #pragma warning disable 612,618 if (method.StartsWith("flickr.auth", StringComparison.Ordinal) && !method.EndsWith("oauth.checkToken", StringComparison.Ordinal)) { if (!string.IsNullOrEmpty(AuthToken)) { parameters["auth_token"] = AuthToken; } } else { // If OAuth Token exists or no authentication required then use new OAuth if (!string.IsNullOrEmpty(OAuthAccessToken) || string.IsNullOrEmpty(AuthToken)) { parameters.Remove("api_key"); OAuthGetBasicParameters(parameters); if (!string.IsNullOrEmpty(OAuthAccessToken)) { parameters["oauth_token"] = OAuthAccessToken; } } else { parameters["auth_token"] = AuthToken; } } #pragma warning restore 612,618 var url = CalculateUri(parameters, !string.IsNullOrEmpty(sharedSecret)); lastRequest = url; string responseXml; if (InstanceCacheDisabled) { responseXml = FlickrResponder.GetDataResponse(this, BaseUri.AbsoluteUri, parameters); } else { var urlComplete = url; var cached = (ResponseCacheItem)Cache.Responses.Get(urlComplete, cacheTimeout, true); if (cached != null) { Debug.WriteLine("Cache hit."); responseXml = cached.Response; } else { Debug.WriteLine("Cache miss."); responseXml = FlickrResponder.GetDataResponse(this, BaseUri.AbsoluteUri, parameters); var resCache = new ResponseCacheItem(new Uri(urlComplete), responseXml, DateTime.UtcNow); Cache.Responses.Shrink(Math.Max(0, Cache.CacheSizeLimit - responseXml.Length)); Cache.Responses[urlComplete] = resCache; } } lastResponse = responseXml; var item = new T(); item.Load(responseXml); return(item); }
private Response GetResponse(Hashtable parameters, TimeSpan cacheTimeout) { // Calulate URL StringBuilder UrlStringBuilder = new StringBuilder(BaseUrl, 2 * 1024); StringBuilder HashStringBuilder = new StringBuilder(_sharedSecret, 2 * 1024); UrlStringBuilder.Append("?"); parameters["api_key"] = _apiKey; if( _apiToken != null && _apiToken.Length > 0 ) { parameters["auth_token"] = _apiToken; } string[] keys = new string[parameters.Keys.Count]; parameters.Keys.CopyTo(keys, 0); Array.Sort(keys); foreach(string key in keys) { if( UrlStringBuilder.Length > BaseUrl.Length + 1 ) UrlStringBuilder.Append("&"); UrlStringBuilder.Append(key); UrlStringBuilder.Append("="); UrlStringBuilder.Append(Utils.UrlEncode((string)parameters[key])); HashStringBuilder.Append(key); HashStringBuilder.Append(parameters[key]); } if (_sharedSecret != null && _sharedSecret.Length > 0) { if (UrlStringBuilder.Length > BaseUrl.Length + 1) { UrlStringBuilder.Append("&"); } UrlStringBuilder.Append("api_sig="); UrlStringBuilder.Append(Md5Hash(HashStringBuilder.ToString())); } string url = UrlStringBuilder.ToString(); _lastRequest = url; _lastResponse = string.Empty; if( CacheDisabled ) { string responseXml = DoGetResponse(url); _lastResponse = responseXml; return Deserialize(responseXml); } else { ResponseCacheItem cached = (ResponseCacheItem) Cache.Responses.Get(url, cacheTimeout, true); if (cached != null) { System.Diagnostics.Debug.WriteLine("Cache hit"); _lastResponse = cached.Response; return Deserialize(cached.Response); } else { System.Diagnostics.Debug.WriteLine("Cache miss"); string responseXml = DoGetResponse(url); _lastResponse = responseXml; ResponseCacheItem resCache = new ResponseCacheItem(); resCache.Response = responseXml; resCache.Url = url; resCache.CreationTime = DateTime.UtcNow; FlickrNet.Response response = Deserialize(responseXml); if( response.Status == ResponseStatus.OK ) { Cache.Responses.Shrink(Math.Max(0, Cache.CacheSizeLimit - responseXml.Length)); Cache.Responses[url] = resCache; } return response; } } }
public override ICacheItem Read(Stream inputStream) { string s = Utils.ReadString(inputStream); string response = Utils.ReadString(inputStream); string[] chunks = s.Split('\n'); // Corrupted cache record, so throw IOException which is then handled and returns partial cache. if( chunks.Length != 2 ) throw new IOException("Unexpected number of chunks found"); string url = chunks[0]; DateTime creationTime = new DateTime(long.Parse(chunks[1])); ResponseCacheItem item = new ResponseCacheItem(); item.Url = url; item.CreationTime = creationTime; item.Response = response; return item; }