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 void Impersonate(string targetUser) { if (_canImpersonate) { var apiUrl = string.Format("users/{0}/become-user", targetUser); var responseText = PostActionAsync(apiUrl).Result; //no deadlock b/c configure await is off var responseJson = JObject.Parse(responseText); UserData = responseJson["user"].Value <JObject>(); } else { var ex = new InvalidOperationException("user does not have permission to impersonate other users"); AsLogger.LogError(String.Format("user [ {0} ] attempted to impersonate user [ {1} ] but lacks become_user permission in ArchivesSpace", _userCred.Username, targetUser), ex); } }
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; } }
public async Task <string> GetAsync(string apiUri) { HttpResponseMessage response; if (string.IsNullOrEmpty(_sessionKey)) { LoginAction(); //and we're going to let it throw an unauthorized exception if it doesn't work since the user isn't checking first } try { response = await GetActionAsync(apiUri).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return(responseString); } catch (HttpRequestException ex) { //We are forcing a success code to continue, most likely error is an expired session so try that first AsLogger.LogError(String.Format("[ArchivesSpaceConnectionHandler.GetAsync] Error getting api resource [ {0} ] - attempting to login and retry", apiUri), ex); LoginAction(); try { response = GetActionAsync(apiUri).Result; response.EnsureSuccessStatusCode(); var responseString = response.Content.ReadAsStringAsync().Result; return(responseString); } catch (HttpRequestException rex) { AsLogger.LogError("[ArchivesSpaceConnectionHandler.GetAsync] Request failed after retrying login. Exception follows.", rex); throw; } } }