示例#1
0
        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;
        }
示例#2
0
        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"));
        }
示例#3
0
        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;
            }
        }
示例#4
0
        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);
        }
示例#5
0
        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);
        }