internal static async Task <LahetaViestiResponse> Post(HttpClient client, LahetaViestiRequest initiatedRequest)
        {
            var response = await client.PostAsJsonAsync("api/asti/lahetaviesti", 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 <LahetaViestiResponseStateCode>());

                // TODO: The JSON is parsed twice here, but it really shouldn't matter here since they're so small
                return(JsonConvert.DeserializeObject <LahetaViestiResponse>(
                           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);
            }
        }
Example #2
0
        /// <summary>
        /// Sends printable 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> > SendPrintableMessageToViestit(List <PrintableViestitMessage> msgList)
        {
            msgList.ForEach((msg) =>
            {
                Validator.ValidateObject(msg, new ValidationContext(msg), true);
                Validator.ValidateObject(msg.Address, new ValidationContext(msg.Address)); // TODO: Is this required?
            });

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

            foreach (PrintableViestitMessage msg in msgList)
            {
                var req = new LahetaViestiRequest
                {
                    CustomerId = msg.SocialSecurityNumber,
                    Files      = new List <RequestFile>()
                    {
                        ViestitMessageFile.ToRequestFile(msg.File)
                    },
                    Id                   = msg.Id,
                    MsgId                = msg.MsgId,
                    Topic                = msg.Topic,
                    SenderName           = msg.SenderName,
                    Text                 = msg.Text,
                    RecipientName        = msg.Address.RecipientName,
                    StreetAddress        = msg.Address.StreetAddress,
                    PostalCode           = msg.Address.PostalCode,
                    City                 = msg.Address.City,
                    CountryCode          = msg.Address.CountryCode,
                    PrintingProvider     = msg.PrintingProvider,
                    SendAlsoAsPrinted    = msg.SendAlsoAsPrinted,
                    UsePrinting          = !msg.TestingOnlyDoNotSendPrinted,
                    ReadConfirmation     = msg.ReadConfirmation,
                    ReceivedConfirmation = msg.ReceivedConfirmation
                };
                InitRequest(req);
                reqList.Add(HubApi.SendPrintableMessageToViestit.Post(_client, req));
            }

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

            return(data.ConvertAll((response) =>
                                   new SentMessageStatus
            {
                StateCode = response.StateCode.ToMessageStateCode(),
                StateDescription = response.StateCodeDescription,
                Id = response.MessageId,
            }));
        }