public static async Task <string> GetStringAsync(string url, CancellationToken cancellation = default) { try { var result = await InternalUtils.CmGetDataAsync(url, UserAgent, cancellation : cancellation); if (cancellation.IsCancellationRequested) { return(null); } return(result == null ? null : Encoding.UTF8.GetString(result)); } catch (Exception e) { Logging.Warning(e); return(null); } }
public static async Task <Tuple <string, bool> > GetStaticDataAsync([NotNull] string id, TimeSpan maxAge, IProgress <AsyncProgressEntry> progress = null, CancellationToken cancellation = default(CancellationToken)) { var file = new FileInfo(FilesStorage.Instance.GetFilename("Static", $"{id}.zip")); if (file.Exists && (JustLoadedStaticData.Contains(id) || DateTime.Now - file.LastWriteTime < maxAge)) { return(Tuple.Create(file.FullName, false)); } var result = await InternalUtils.CmGetDataAsync($"static/get/{id}", UserAgent, file.Exists?file.LastWriteTime : (DateTime?)null, progress, cancellation).ConfigureAwait(false); if (cancellation.IsCancellationRequested) { return(null); } if (result != null && result.Item1.Length != 0) { Logging.Write($"Fresh version of {id} loaded, from {result.Item2?.ToString() ?? "UNKNOWN"}"); var lastWriteTime = result.Item2 ?? DateTime.Now; await FileUtils.WriteAllBytesAsync(file.FullName, result.Item1, cancellation).ConfigureAwait(false); file.Refresh(); file.LastWriteTime = lastWriteTime; JustLoadedStaticData.Add(id); return(Tuple.Create(file.FullName, true)); } if (!file.Exists) { return(null); } Logging.Write($"Cached {id} used"); JustLoadedStaticData.Add(id); return(Tuple.Create(file.FullName, false)); }
public static Task <byte[]> GetDataAsync(string url, IProgress <double?> progress = null, CancellationToken cancellation = default(CancellationToken)) { return(InternalUtils.CmGetDataAsync(url, UserAgent, progress, cancellation)); }