private ServiceResponse SubscribeWithMergeVars(string email, dynamic mergeVars, bool enableDoubleOptIn) { ServiceResponse serviceResponse = new ServiceResponse(); var urlTemplate = String.Format("{0}{1}/subscribe.json/", MailChimpServiceConfiguration.Settings.ServiceUrl, MailChimpServiceConfiguration.Settings.ListsRelatedSection); var subscriber = new Subscriber(); subscriber.DoubleOptIn = enableDoubleOptIn; subscriber.ApiKey = _apiKey; subscriber.ListId = MailChimpServiceConfiguration.Settings.SubscriberListId; var emailObject = new Email { EmailValue = email }; subscriber.Email = emailObject; subscriber.UpdateExisting = true; if (mergeVars != null) { subscriber.MergeVars = mergeVars; } try { var responseData = PostHelpers.PostJson(urlTemplate, subscriber.ToString()); var deserializedData = JsonConvert.DeserializeObject<Email>(responseData); _log.DebugFormat("MailChimpService Call : {0}, response json : {1}", subscriber, deserializedData); serviceResponse.IsSuccesful = deserializedData.EmailValue != null && deserializedData.Euid != null && deserializedData.Leid != null; serviceResponse.ResponseJson = responseData; } catch (WebException exception) { using (Stream stream = exception.Response.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); String responseString = reader.ReadToEnd(); serviceResponse.ResponseJson = responseString; _log.Error(responseString); } _log.Error(exception); serviceResponse.IsSuccesful = false; } return serviceResponse; }
private Task<MailChimpServiceResponse<Email>> SubscribeAsync(string email, string listId, MergeVariables mergeVars, string emailType, bool doubleOptIn, bool updateExisting, bool replaceInterests, bool sendWelcome) { var url = Urls.List + "/subscribe.json"; var subscriber = new Subscriber { ListId = listId, Email = new Email { EmailValue = email }, MergeVars = mergeVars, EmailType = emailType, DoubleOptIn = doubleOptIn, UpdateExisting = updateExisting, ReplaceInterests = replaceInterests, SendWelcome = sendWelcome }; return Execute<Email>(url, subscriber); }