private void InitCache() { var bucket = new TokenBucket(300, 5); _sharedWebCache = new TokenCompliantCacheWrapper(new MemoryCacheMethod(), bucket); _sharedRenderCache = new MemoryCacheMethod(); }
private void InitCache() { var bucket = new TokenBucket(300, 5); _sharedTokenBucketMiddleware = new TokenComplianceMiddleware(bucket); _sharedWebCache = new MemoryCacheMethod(); _sharedRenderCache = new MemoryCacheMethod(); }
/// <summary> /// Creates a new <see cref="Connection"/> with a specified API key and locale. /// With optionally a custom user agent, /// HTTP client, /// caching method for API requests, /// and caching method for render API requests. /// </summary> /// <param name="accessToken">The API key.</param> /// <param name="locale">The locale.</param> /// <param name="userAgent">The User-Agent.</param> /// <param name="httpClient">The HTTP client.</param> /// <param name="cacheMethod">The cache method.</param> /// <param name="renderCacheMethod">The render cache method.</param> /// <param name="renderCacheDuration">The render cache duration (defaults to render API headers)</param> /// <exception cref="ArgumentException"><paramref name="accessToken"/> is incorrectly formatted.</exception> public Connection( string?accessToken, Locale locale, ICacheMethod?cacheMethod = null, ICacheMethod?renderCacheMethod = null, TimeSpan?renderCacheDuration = null, string?userAgent = null, IHttpClient?httpClient = null) { if (!string.IsNullOrWhiteSpace(accessToken) && !IsAccessTokenValid(accessToken)) { throw new ArgumentException("The access token is incorrectly formatted", nameof(accessToken)); } this.accessToken = accessToken ?? string.Empty; this.Locale = locale; string userAgentProduct = "Gw2Sharp"; try { string?fileVersion = FileVersionInfo.GetVersionInfo(typeof(Gw2Client).Assembly.Location).FileVersion; if (!string.IsNullOrWhiteSpace(fileVersion)) { userAgentProduct = $"Gw2Sharp/{new Version(fileVersion).ToString(3)}"; } } catch { /* Ignore */ } this.UserAgent = $"{userAgent}{(!string.IsNullOrWhiteSpace(userAgent) ? " " : "")}" + $"{userAgentProduct} (https://github.com/Archomeda/Gw2Sharp)"; this.httpClient = httpClient ?? new HttpClient(); this.cacheMethod = cacheMethod ?? new MemoryCacheMethod(); this.renderCacheMethod = renderCacheMethod ?? new NullCacheMethod(); this.RenderCacheDuration = renderCacheDuration ?? TimeSpan.Zero; this.middleware.CollectionChanged += this.Middleware_CollectionChanged; this.UseDefaultMiddleware(); #if NET5_0_OR_GREATER if (OperatingSystem.IsWindows()) #else if (Environment.OSVersion.Platform == PlatformID.Win32NT) #endif { #pragma warning disable CA1416 // We can't get a different platform than Windows here, but the analyzer doesn't understand this this.MumbleClientReaderFactory = x => new Gw2MumbleClientReader(x); #pragma warning restore CA1416 } else { this.MumbleClientReaderFactory = _ => new UnsupportedMumblePlatformClientReader(); } }
public ManagedConnection(string accessToken, ICacheMethod webApiCache, ICacheMethod renderCache = null, TimeSpan?renderCacheDuration = null) { string ua = $"BlishHUD/{Program.OverlayVersion}"; _internalConnection = new Connection(accessToken, GameService.Overlay.UserLocale.Value, webApiCache, renderCache, renderCacheDuration ?? TimeSpan.MaxValue, ua); _internalClient = new Gw2Client(_internalConnection).WebApi; Logger.Debug("Created managed Gw2Sharp connection {useragent}.", ua); SetupListeners(); }
public TokenCompliantCacheWrapper(ICacheMethod cacheMethod, TokenBucket tokenBucket) { _cache = cacheMethod; _bucket = tokenBucket; }