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); }
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; } }