Ejemplo n.º 1
0
        //Takes organizationId and returns the specified amount of the latest Difi messages for that organization as IEnumerable of DifiMessage objects
        public async Task <IEnumerable <DifiMessage> > GetDifiMessagesAsync(string organizationId, Direction direction)
        {
            //Passes the orgId to the config cache in order to retrive information about the Organization's API's etc
            var orgConfig = await _configService.GetConfigAsync(organizationId);

            //Variable request does the api query and puts json response in variable response
            var request = new HttpRequestMessage(HttpMethod.Get, $"{orgConfig.IntegrationPoint}/api/conversations?size=50&direction={direction}");

            request.Headers.Add("Accept", "application/json");
            var response = await Client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                //Read the response as stream and deserialize it into the variable difiMessages(a list) - return the difiMessages list
                using var responseStream = await response.Content.ReadAsStreamAsync();

                var difiMessages = await JsonSerializer.DeserializeAsync <DifiMessageContent>(responseStream);

                return(_messagesService.AddLatestStatusOnList(difiMessages.content));
            }
            else if (response.StatusCode.Equals(500))
            {
                throw new ServiceUnavailableException("Difi integrationpoint is unavailable");
            }
            else
            {
                var ex = response.StatusCode.ToString();
                throw new DifiException(ex);
            }
        }