/// <summary> /// Creates a list on mailchimp /// </summary> /// <param name="list">The model that contains all info about the list that will be created</param> /// <returns></returns> public MailChimpWrapperResponse <ListCreateResponseModel> Create(ListCreateModel list) { try { // Initialize the RestRequest from RestSharp.Newtonsoft so the serializer will user Newtonsoft defaults. var request = new RestRequest(ResourcesEndpoints.ListCreate(), Method.POST); // Adds the resource request.AddHeader("content-type", "application/json"); request.JsonSerializer = new CustomNewtonsoftSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore, }); // Validates the object var validationContext = new ValidationContext(list, serviceProvider: null, items: null); var validationResults = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(list, validationContext, validationResults, false); if (isValid) { request.AddJsonBody(list); // Execute the request var response = restClient.Execute(request); // If the request return ok, then return MailChimpWrapperResponse with deserialized object if (response.StatusCode == System.Net.HttpStatusCode.OK) { return new MailChimpWrapperResponse <ListCreateResponseModel> { objectRespose = JsonConvert.DeserializeObject <ListCreateResponseModel>(response.Content) } } ; // Check If MailChimp is Offline if (response.StatusCode == System.Net.HttpStatusCode.GatewayTimeout) { return(ErrorResponse <ListCreateResponseModel>(new Exception("MailChimp Offline"))); } // If an error occurs, encapsulates the error in MailChimpWrapperResponse var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content); return(new MailChimpWrapperResponse <ListCreateResponseModel> { hasErrors = true, error = errorContent }); } // If the object was not valid then creates and ErrorModel to send back var error = new ErrorModel { type = "Internal Method Error.", title = "One or more fields were not validated. Look in detail for more information", status = 0, detail = Util.GetValidationsErrors(validationResults) }; return(new MailChimpWrapperResponse <ListCreateResponseModel> { hasErrors = true, error = error }); } catch (Exception ex) { return(ErrorResponse <ListCreateResponseModel>(ex)); } }
/// <summary> /// Updates the list by its id based on ListCreateModel object /// </summary> /// <param name="listId">The Id of the list to be updated</param> /// <param name="list">The model that contains all info about the list that will be created</param> /// <returns></returns> public MailChimpWrapperResponse <ListCreateResponseModel> Update(string listId, ListCreateModel list) { try { // Initialize the RestRequest from RestSharp.Newtonsoft so the serializer will user Newtonsoft defaults. var request = new RestRequest(ResourcesEndpoints.ListEdit(listId), Method.PATCH); // Adds the resource request.AddHeader("content-type", "application/json"); request.JsonSerializer = new CustomNewtonsoftSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore, }); // Validates the object var validationContext = new ValidationContext(list, serviceProvider: null, items: null); var validationResults = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(list, validationContext, validationResults, false); if (isValid) { request.AddJsonBody(list); // Execute the request var response = restClient.Execute(request); // If the request return ok, then return MailChimpWrapperResponse with deserialized object if (response.StatusCode == System.Net.HttpStatusCode.OK) { return new MailChimpWrapperResponse <ListCreateResponseModel> { objectRespose = JsonConvert.DeserializeObject <ListCreateResponseModel>(response.Content) } } ; // If an error occurs, encapsulates the error in MailChimpWrapperResponse var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content); return(new MailChimpWrapperResponse <ListCreateResponseModel> { hasErrors = true, error = errorContent }); } // If the object was not valid then creates and ErrorModel to send back var error = new ErrorModel { type = "Internal Method Error.", title = string.Format("Field {0} missing", validationResults[0]?.MemberNames.FirstOrDefault()), status = 0, detail = validationResults[0].ErrorMessage }; return(new MailChimpWrapperResponse <ListCreateResponseModel> { hasErrors = true, error = error }); } catch (Exception ex) { return(ErrorResponse <ListCreateResponseModel>(ex)); } }