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 static void SetArchivesSpaceHeader(this HttpClient client, string sessionKey) { var archivesSpaceHeaderName = "X-ArchivesSpace-Session"; if (client.DefaultRequestHeaders.Contains(archivesSpaceHeaderName)) { AsLogger.LogDebug("[SetArchivesSpaceHeader] Existing header found, removing from request"); client.DefaultRequestHeaders.Remove(archivesSpaceHeaderName); } AsLogger.LogDebug(String.Format("[SetArchivesSpaceHeader] Setting ArchivesSpace Header with name [ {0} ] to value [ {1} ]", archivesSpaceHeaderName, sessionKey)); client.DefaultRequestHeaders.Add(archivesSpaceHeaderName, sessionKey); }
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); }
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 <string> PostActionAsync(string apiUri, IEnumerable <KeyValuePair <string, string> > postParams) { var uri = String.Format("{0}/{1}", _baseUrl, apiUri); AsLogger.LogDebug(String.Format("Posting to URI: {0}", uri)); var postBody = new FormUrlEncodedContent(postParams); var response = await _httpClient.PostAsync(uri, postBody).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return(responseString); }
private async Task <string> PostActionAsync(string apiUri) { var uri = String.Format("{0}/{1}", _baseUrl, apiUri); AsLogger.LogDebug(String.Format("Posting to URI: {0}", uri)); var httpContent = new StringContent(""); //Empty, we're just posting to the URI in this method var response = await _httpClient.PostAsync(uri, httpContent).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return(responseString); }
private void LoginAction() { //This can't be async and needs to happen before we set the global request headers of the httpclient field using (var wc = new WebClient()) { AsLogger.LogDebug(String.Format("ASpace Login routine starting for user with username [ {0} ]. Current session key is [ {1} ]", _userCred.Username, _sessionKey ?? "null")); string loginUri = String.Format("{0}/users/{1}/login", _baseUrl, _userCred.Username); var postParams = new NameValueCollection(); postParams.Add("password", _userCred.Password); var response = wc.UploadValues(loginUri, postParams); var responseString = Encoding.UTF8.GetString(response); var responseJson = JObject.Parse(responseString); _sessionKey = responseJson["session"].Value <string>(); AsLogger.LogDebug(String.Format("New session key set: [ {0} ]", _sessionKey)); UserData = responseJson["user"].Value <JObject>(); _canImpersonate = UserData.ToString().Contains("become_user"); _httpClient.SetArchivesSpaceHeader(_sessionKey); } }
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; } } }