public static T GetConfigSetting <T>(string key) { try { var appSetting = ConfigurationManager.AppSettings[key]; if (string.IsNullOrWhiteSpace(appSetting)) { throw new ConfigurationErrorsException( string.Format("Helpers.AppSettings: key {0} was null or empty", key)); } var converter = TypeDescriptor.GetConverter(typeof(T)); return((T)(converter.ConvertFromInvariantString(appSetting))); } catch (ConfigurationErrorsException ex) { AsLogger.LogWarning("Configuration value was missing from configuration file. Returning default value.", ex); return(default(T)); } catch (Exception ex) { AsLogger.LogError("Unexpected exception accessing configuration file or data", ex); throw; } }
public bool Login() { try { LoginAction(); return(true); } catch (WebException ex) //Note we're using a webexception since login happens with webclient, everything else with httpclient { AsLogger.LogWarning("caught invalid login attempt, returning false. error was: ", ex); } return(false); }
private async Task <HttpResponseMessage> GetActionAsync(string apiUri) { //ToDo: Returning a string response is a design flaw in this context since we may want to handle 404s and other errors //which make sense for the REST API but would blend in with Network Errors if we convert to a string right away. //Error handling should happen upstream when we're more sure of what the request is about if (apiUri.StartsWith(@"/")) { apiUri = apiUri.TrimStart('/'); } var uriString = String.Format("{0}/{1}", _baseUrl, apiUri); AsLogger.LogDebug(String.Format("Requesting URL: {0}", uriString)); AsLogger.LogDebug(String.Format("Current Session Key: [ {0} ]", _sessionKey)); try { var response = await _httpClient.GetAsync(uriString).ConfigureAwait(false); if (response.IsSuccessStatusCode) { AsLogger.LogDebug("[GetActionAsync] Request Successful, returning response content"); return(response); } else { var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); AsLogger.LogWarning(String.Format("[GetActionAsync] Request did not return a success status code. Code [ {0} : {1} ] Message Body [ {2} ]", (int)response.StatusCode, response.StatusCode, responseString)); return(response); } } catch (HttpRequestException rex) { AsLogger.LogError(String.Format("[ArchivesSpaceConnectionHandler.GetActionAsync] API request to [ {0} ] failed. Exception follows.", uriString), rex); throw; } }