Exemple #1
0
        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 <List <BenefitsClaimSummary> > GetBenefits(string personReference)
        {
            var cacheResponse = await _cacheProvider.GetStringAsync($"{personReference}-{ECacheKeys.Benefits}");

            if (!string.IsNullOrEmpty(cacheResponse))
            {
                return(JsonConvert.DeserializeObject <List <BenefitsClaimSummary> >(cacheResponse));
            }

            var sessionId = await _sessionProvider.GetSessionId(personReference);

            var url = _queryBuilder
                      .Add("docid", "hbsel")
                      .Add("sessionId", sessionId)
                      .Build();

            var body = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("claimsts", "All")
            });

            var response = await _gateway.PostAsync(url, body);

            var content = await response.Content.ReadAsStringAsync();

            var parsedResponse = _xmlParser.DeserializeXmlStringToType <BenefitsClaimsSummaryResponse>(content, "HBSelectDoc");
            var claimSummary   = parsedResponse.Claims.Summary;

            claimSummary.ForEach(_ => _.PersonName = parsedResponse.PersonName);

            _ = _cacheProvider.SetStringAsync($"{personReference}-{ECacheKeys.Benefits}", JsonConvert.SerializeObject(claimSummary));

            return(claimSummary);
        }