Ejemplo n.º 1
0
        /// <summary>
        /// Appends a new message into an inbox
        /// </summary>
        /// <param name="inboxId">The id of the requested inbox</param>
        /// <param name="message">The details of the message to be imported</param>
        /// <returns>The conversation reference to the newly created conversation</returns>
        public ImportMessageResponse ImportMessage(string inboxId, ImportMessageRequest message)
        {
            _baseRoute = "inboxes/{inbox_id}/imported_messages";

            var request = base.BuildRequest(Method.POST);

            request.AddParameter("inbox_id", inboxId, ParameterType.UrlSegment);

            if (message.HasAttachments())
            {
                var parameters = JsonConvert.DeserializeObject <Dictionary <string, object> >(JsonConvert.SerializeObject(Mapper.Map <ImportMessageMultipartFormRequest>(message), new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                }));
                foreach (var p in parameters)
                {
                    request.AddParameter(p.Key.ToString(), p.Value);
                }

                for (int i = 0; i < message.Attachments.Count(); i++)
                {
                    var attach    = message.Attachments[i];
                    var file      = File.ReadAllBytes(attach.Path);
                    var fileParam = FileParameter.Create($"attachments[{i}]", file, attach.Name, attach.ContentType);
                    request.Files.Add(fileParam);
                }
                request.AlwaysMultipartFormData = true;
                return(_client.Execute <ImportMessageResponse>(request));
            }
            else
            {
                return(_client.Execute <ImportMessageResponse>(request, message));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Appends a new message into an inbox
        /// </summary>
        /// <param name="inboxId">The id of the requested inbox</param>
        /// <param name="message">The details of the message to be imported</param>
        /// <returns>The conversation reference to the newly created conversation</returns>
        public ImportMessageResponse ImportMessage(string inboxId, ImportMessageRequest message)
        {
            _baseRoute = "inboxes/{inbox_id}/imported_messages";

            var request = base.BuildRequest(Method.POST);

            request.AddParameter("inbox_id", inboxId, ParameterType.UrlSegment);

            if (message.HasAttachments())
            {
                // Manually map ImportMessageRequest to ImportMessageMultipartFormRequest (AutoMapper broke w 3.1 update)
                ImportMessageMultipartFormRequest importMessageMultipartFormRequest = new ImportMessageMultipartFormRequest
                {
                    Sender      = message.Sender,
                    To          = message.To,
                    Cc          = message.Cc,
                    Bcc         = message.Bcc,
                    Subject     = message.Subject,
                    Body        = message.Body,
                    BodyFormat  = message.BodyFormat,
                    Type        = message.Type,
                    ExternalId  = message.ExternalId,
                    CreatedAt   = message.CreatedAt,
                    Tags        = message.Tags,
                    Attachments = message.Attachments,
                    Metadata    = message.Metadata,
                };

                var parameters = JsonConvert.DeserializeObject <Dictionary <string, object> >(JsonConvert.SerializeObject(importMessageMultipartFormRequest, new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                }));
                foreach (var p in parameters)
                {
                    request.AddParameter(p.Key.ToString(), p.Value);
                }

                for (int i = 0; i < message.Attachments.Count(); i++)
                {
                    var attach    = message.Attachments[i];
                    var file      = File.ReadAllBytes(attach.Path);
                    var fileParam = FileParameter.Create($"attachments[{i}]", file, attach.Name, attach.ContentType);
                    request.Files.Add(fileParam);
                }
                request.AlwaysMultipartFormData = true;
                return(_client.Execute <ImportMessageResponse>(request));
            }
            else
            {
                return(_client.Execute <ImportMessageResponse>(request, message));
            }
        }