internal static async Task <LisaaKohteitaResponse> Post(HttpClient client, LisaaKohteitaRequest initiatedRequest)
        {
            var response = await client.PostAsJsonAsync("api/asti/lisaakohteita", initiatedRequest);

            try
            {
                var json = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    throw new HttpRequestException((int)response.StatusCode + " HttpRequestException, Content: " + json);
                }

                // Check first just the state code
                HandleResponseStateCode(JObject.Parse(json).SelectToken(ResponseStateCodePropertyName).ToObject <ResponseStateCode>());

                // TODO: The JSON is parsed twice here, but it really shouldn't matter here since they're so small
                return(JsonConvert.DeserializeObject <LisaaKohteitaResponse>(
                           json,
                           new JsonSerializerSettings {
                    MissingMemberHandling = MissingMemberHandling.Error
                }));
            }
            // Any JsonExceptions mean that the API has changed, and client needs update
            catch (JsonException e)
            {
                throw new ClientFaultException(e);
            }
        }
Пример #2
0
        /// <summary>
        /// Sends messages to customers through suomi.fi/viestit -service.
        /// Throws ClientFaultException in case of this Client working improperly.
        /// Throws custom Keha.SuomiFiViestitHub.Client.Exceptions on returned error codes.
        /// </summary>
        /// <param name="msgList">Messages are given as a list of ViestitMessages</param>
        /// <returns>Returns the StateCode of the handled message along with supplementing information</returns>
        public async Task <List <SentMessageStatus> > SendMessageToViestit(List <ViestitMessage> msgList)
        {
            msgList.ForEach((msg) =>
            {
                Validator.ValidateObject(msg, new ValidationContext(msg), true);
                msg.Files?.ForEach((file) => Validator.ValidateObject(file, new ValidationContext(file), true));
                msg.Links?.ForEach((link) => Validator.ValidateObject(link, new ValidationContext(link), true));
            });

            var reqList = new List <Task <LisaaKohteitaResponse> >();

            foreach (ViestitMessage msg in msgList)
            {
                var req = new LisaaKohteitaRequest
                {
                    CustomerId = msg.SocialSecurityNumber,
                    Files      = msg.Files != null
                        ? msg.Files.ConvertAll(ViestitMessageFile.ToRequestFile)
                        : new List <RequestFile>(),
                    Links = msg.Links != null
                        ? msg.Links.ConvertAll(ViestitMessageLink.ToRequestLink)
                        : new List <RequestLink>(),
                    Id                   = msg.Id,
                    MsgId                = msg.MsgId,
                    Topic                = msg.Topic,
                    SenderName           = msg.SenderName,
                    Text                 = msg.Text,
                    ReadConfirmation     = msg.ReadConfirmation,
                    ReceivedConfirmation = msg.ReceivedConfirmation
                };
                InitRequest(req);
                reqList.Add(HubApi.SendMessageToViestit.Post(_client, req));
            }

            var data = new List <LisaaKohteitaResponse>(await Task.WhenAll(reqList));

            return(data.ConvertAll((response) =>
                                   new SentMessageStatus // NOTE: We can hardcode [0] here since the API only allows arrays with one element
            {
                StateCode = response.Targets[0].Customers[0].MessageStateCode,
                StateDescription = response.Targets[0].Customers[0].MessageStateDescription,
                Id = response.MessageId
            }));
        }