public async Task <T> Download <T>(Uri uri) where T : class { await EnsureClientAuthorizationHeader(); try { var response = await _client.GetAsync(uri); LastContent = await response.Content.ReadAsStringAsync(); HttpStatusCodeDecision = ExamineResponse(response); switch (HttpStatusCodeDecision) { case HttpStatusCodeDecision.RethrowException: var exception = LastException ?? new Exception( $"A manufactured exception has occured in {nameof(SiteConnector)} after receiving status code {LastCode} and Content of: {LastContent}"); _logger.Error(exception, $"Forced Exception from {nameof(SiteConnector)} recieving {LastCode} and Content of: {LastContent} and Content of: {LastContent}"); throw exception; case HttpStatusCodeDecision.HandleException: var generatedException = LastException ?? new Exception( $"A manufactured exception has occured in {nameof(SiteConnector)} after receiving status code {LastCode}"); _logger.Error(generatedException, $"Exception from {nameof(SiteConnector)} recieving {LastCode} and Content of: {LastContent} from {uri}"); return(null); case HttpStatusCodeDecision.ReturnNull: _logger.Debug( $"Ignoring Return value from {nameof(SiteConnector)} receiving {LastCode} and Content of: {LastContent}"); return(null); default: _logger.Info($"Successful call to site from {nameof(SiteConnector)} recieving {LastCode}"); var content = await response.Content.ReadAsStringAsync(); if (typeof(T) == typeof(string)) { return(content as T); } return(JsonConvert.DeserializeObject <T>(content)); } } catch (Exception e) { _logger.Debug($"Call to sub site failed {nameof(SiteConnector)} with {e.HResult} returning null response. Stack: {e.StackTrace}"); return(null); } }
public async Task <T> Upload <T>(Uri uri, string content) where T : class { await EnsureClientAuthorizationHeader(); try { var postContent = new StringContent(content); var response = await _client.PostAsync(uri, postContent); LastContent = await response.Content.ReadAsStringAsync(); HttpStatusCodeDecision = ExamineResponse(response); switch (HttpStatusCodeDecision) { case HttpStatusCodeDecision.HandleException: LastException = LastException ?? new Exception($"An enforced exception has occured in {nameof(SiteConnector)}"); throw LastException; case HttpStatusCodeDecision.ReturnNull: return(null); default: if (typeof(T) == typeof(string)) { return(LastContent as T); } return(JsonConvert.DeserializeObject <T>(LastContent)); } } catch (Exception e) { _logger.Debug($"Call to sub site failed {nameof(SiteConnector)} with {e.HResult} returning null response. Stack: {e.StackTrace}"); return(null); } }