public async Task PersistAsync(string data, InOutOptions options) { if (ThrowBehavior.HasFlag(ThrowOption.ThrowOnPersist)) { throw new IOException($"Mock exception has been thrown at {this.GetType().Name}"); } await Task.Delay(Duration); return; }
public async Task <string> FetchAsync(InOutOptions options = InOutOptions.None) { if (ThrowBehavior.HasFlag(ThrowOption.ThrowOnFetch)) { throw new IOException($"Mock exception has been thrown at {this.GetType().Name}"); } await Task.Delay(Duration); return(await File.ReadAllTextAsync("../fetch.json")); }
public async Task PersistAsync(string data, InOutOptions options) { string sourcePath = GetType() .GetField(options.ToString(), BindingFlags.NonPublic | BindingFlags.Instance) .GetValue(this) as string; try { await File.WriteAllTextAsync(sourcePath, data, Token); } catch (System.Exception) { _logger.Log(LogLevel.Error, $"Failed to write to '{sourcePath}'! Check if this process has the required IO persmissions!"); throw; } }
public async Task <string> FetchAsync(InOutOptions options) { string sourcePath = GetType() .GetField(options.ToString(), BindingFlags.NonPublic | BindingFlags.Instance) .GetValue(this) as string; string result; try { result = await File.ReadAllTextAsync(sourcePath, Token); } catch (System.Exception) { _logger.Log(LogLevel.Error, $"Failed to read from '{sourcePath}'! Check if the file exists, or if this process has the required IO persmissions!"); throw; } return(result); }
public async Task <string> FetchAsync(InOutOptions options = InOutOptions.None) { string response; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Add(AuthHeaderKey, AuthHeaderValue); client.Timeout = TimeSpan.FromSeconds(Timeout); try { response = await client.GetStringAsync(ApiUrl, Token); } catch (HttpRequestException) { _logger.Log(LogLevel.Error, $"Failed to complete a request to '{ApiUrl}'! Check your network connection or modify the URL in the 'appsettings.json' file."); throw; } } return(response); }