protected bool ReAuthenticate() { IRestRequest request; try { request = jiraApiRequestFactory.CreateReAuthenticateRequest(); } catch (AuthenticateNotYetCalledException) { return(false); } var client = restClientFactory.Create(true); _logger.Log(string.Format("Request: {0}", client.BuildUri(request))); IRestResponse response = client.Execute(request); _logger.Log(string.Format("Response: {0} - {1}", response.StatusCode, StringHelpers.Truncate(response.Content, 100))); if (response.StatusCode == HttpStatusCode.Unauthorized) { ErrorMessage = "Invalid username or password"; return(false); } if (response.StatusCode != HttpStatusCode.OK) { ErrorMessage = response.ErrorMessage; return(false); } ErrorMessage = ""; return(true); }
public T DoAuthenticatedRequest <T>(IRestRequest request) where T : new() { IRestClient client = restClientFactory.Create(); _logger.Log(string.Format("Request: {0}", client.BuildUri(request))); IRestResponse <T> response = client.Execute <T>(request); _logger.Log(string.Format("Response: {0} - {1}", response.StatusCode, StringHelpers.Truncate(response.Content, 100))); // If login session has expired, try to login, and then re-execute the original request if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadRequest) { _logger.Log("Try to re-authenticate"); if (!ReAuthenticate()) { throw new RequestDeniedException(); } _logger.Log(string.Format("Authenticated. Resend request: {0}", client.BuildUri(request))); response = client.Execute <T>(request); _logger.Log(string.Format("Response: {0} - {1}", response.StatusCode, StringHelpers.Truncate(response.Content, 100))); } if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created) { ErrorMessage = response.ErrorMessage; throw new RequestDeniedException(); } ErrorMessage = ""; return(response.Data); }