public async Task <string> GetSessionId() { var url = _queryBuilder .Add("docid", "crmlogin") .Add("userid", _configuration.Username) .Add("password", _configuration.Password) .Build(); var response = await _gateway.GetAsync(url); if (response.StatusCode != HttpStatusCode.OK) { throw new Exception($"Civica is unavailable. Responded with status code: {response.StatusCode}"); } var xmlResponse = await response.Content.ReadAsStringAsync(); var deserializedResponse = _xmlParser.DeserializeXmlStringToType <SessionIdModel>(xmlResponse, "Login").Result; if (!deserializedResponse.ErrorCode.Text.Equals("5")) { throw new Exception($"API login unsuccessful, check credentials. Actual response: {xmlResponse}"); } var sessionId = deserializedResponse.SessionID; if (string.IsNullOrWhiteSpace(sessionId)) { throw new Exception("No session id returned"); } return(sessionId); }
public async Task Process(List <IAction> actions, FormSchema formSchema, string formName) { var sessionGuid = _sessionHelper.GetSessionGuid(); var mappingData = await _mappingService.Map(sessionGuid, formName); foreach (var action in actions) { var response = new HttpResponseMessage(); var submitSlug = action.Properties.PageActionSlugs.FirstOrDefault(_ => _.Environment.ToLower().Equals(_environment.EnvironmentName.ToS3EnvPrefix().ToLower())); if (submitSlug == null) { throw new ApplicationException("ValidateService::Process, there is no PageActionSlug defined for this environment"); } var entity = _actionHelper.GenerateUrl(submitSlug.URL, mappingData.FormAnswers); if (!string.IsNullOrEmpty(submitSlug.AuthToken)) { _gateway.ChangeAuthenticationHeader(submitSlug.AuthToken); } response = await _gateway.GetAsync(entity.Url); if (!response.IsSuccessStatusCode) { throw new ApplicationException($"ValidateService::Process, http request to {entity.Url} returned an unsuccessful status code, Response: {JsonConvert.SerializeObject(response)}"); } } }
private async Task <Map> ConvertLatLng(Map map) { try { var result = await _gateway.GetAsync($"wfs?&service=wfs&version=1.0.0&request=getfeature&typename=flooding:vw_click_reproject&viewparams=long:{map.Lng};lat:{map.Lat};&outputformat=json&click_reproject_4326_osgb"); var response = JsonConvert.DeserializeObject <MapResponse>(await result.Content.ReadAsStringAsync()); var coordinates = response.features.First(); if (coordinates == null) { throw new Exception("FloodingService:: ConvertLatLng:: No features found in response"); } map.Lng = coordinates.properties.EastingNorthing.Split(',')[0]; map.Lat = coordinates.properties.EastingNorthing.Split(',')[1]; return(map); } catch (Exception ex) { throw new Exception($"FloodingService:: ConvertLatLng:: Error message: {ex.Message}", ex); } }
public async Task Process(List <IAction> actions, FormSchema formSchema, string formName) { var answers = new List <Answers>(); var sessionGuid = _sessionHelper.GetSessionGuid(); var mappingData = await _mappingService.Map(sessionGuid, formName); foreach (var action in actions) { var response = new HttpResponseMessage(); var submitSlug = action.Properties.PageActionSlugs.FirstOrDefault(_ => _.Environment.ToLower().Equals(_environment.EnvironmentName.ToS3EnvPrefix().ToLower())); if (submitSlug == null) { throw new ApplicationException("RetrieveExternalDataService::Process, there is no PageActionSlug defined for this environment"); } var entity = _actionHelper.GenerateUrl(submitSlug.URL, mappingData.FormAnswers); if (!string.IsNullOrEmpty(submitSlug.AuthToken)) { _gateway.ChangeAuthenticationHeader(submitSlug.AuthToken); } if (entity.IsPost) { response = await _gateway.PostAsync(entity.Url, mappingData.Data); } else { response = await _gateway.GetAsync(entity.Url); } if (!response.IsSuccessStatusCode) { throw new ApplicationException($"RetrieveExternalDataService::Process, http request to {entity.Url} returned an unsuccessful status code, Response: {JsonConvert.SerializeObject(response)}"); } if (response.Content == null) { throw new ApplicationException($"RetrieveExternalDataService::Process, response content from {entity.Url} is null."); } var content = await response.Content.ReadAsStringAsync(); if (string.IsNullOrWhiteSpace(content)) { throw new ApplicationException($"RetrieveExternalDataService::Process, Gateway {entity.Url} responded with empty reference"); } answers.Add(new Answers { QuestionId = action.Properties.TargetQuestionId, Response = JsonConvert.DeserializeObject <string>(content) }); } mappingData.FormAnswers.Pages.FirstOrDefault(_ => _.PageSlug.ToLower().Equals(mappingData.FormAnswers.Path.ToLower())).Answers.AddRange(answers); await _distributedCache.SetStringAsync(sessionGuid, JsonConvert.SerializeObject(mappingData.FormAnswers), CancellationToken.None); }
public async Task <BenefitsClaim> GetBenefitDetails(string personReference, string claimReference, string placeReference) { var key = $"{personReference}-{claimReference}-{placeReference}-{ECacheKeys.ClaimDetails}"; var cacheResponse = await _cacheProvider.GetStringAsync(key); if (!string.IsNullOrEmpty(cacheResponse)) { return(JsonConvert.DeserializeObject <BenefitsClaim>(cacheResponse)); } var sessionId = await _sessionProvider.GetSessionId(personReference); var url = _queryBuilder .Add("sessionId", sessionId) .Add("docid", "hbdet") .Add("claimref", claimReference) .Add("placeref", placeReference) .Build(); var response = await _gateway.GetAsync(url); var responseContent = await response.Content.ReadAsStringAsync(); BenefitsClaim parsedResponse; try { parsedResponse = _xmlParser.DeserializeXmlStringToType <BenefitsClaim>(responseContent, "HBClaimDetails"); } catch (Exception ex) { throw new Exception($"Failed to deserialize XML - Person reference: {personReference}, Claim reference: {claimReference}, Place reference {placeReference}, Response: {responseContent}", ex.InnerException); } _ = _cacheProvider.SetStringAsync(key, JsonConvert.SerializeObject(parsedResponse)); return(parsedResponse); }