diff --git a/CakeMail.RestClient.UnitTests/Templates.cs b/CakeMail.RestClient.UnitTests/Templates.cs index 9e6e68f..f4d5541 100644 --- a/CakeMail.RestClient.UnitTests/Templates.cs +++ b/CakeMail.RestClient.UnitTests/Templates.cs @@ -33,13 +33,15 @@ public void CreateTemplateCategory_with_minimal_parameters() r.Resource == "/TemplateV2/CreateCategory/" && r.Parameters.Count(p => p.Name == "apikey" && (string)p.Value == API_KEY && p.Type == ParameterType.HttpHeader) == 1 && r.Parameters.Count(p => p.Type == ParameterType.HttpHeader) == 1 && - r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 5 && + r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 7 && r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 && - r.Parameters.Count(p => p.Name == "label[en_US]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 && - r.Parameters.Count(p => p.Name == "label[fr_FR]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 - ))).Returns(new RestResponse() + r.Parameters.Count(p => p.Name == "label[0][language]" && (string)p.Value == "en_US" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[0][name]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[1][language]" && (string)p.Value == "fr_FR" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[1][name]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 + ))).Returns(new RestResponse() { StatusCode = HttpStatusCode.OK, ContentType = "json", @@ -71,12 +73,12 @@ public void CreateTemplateCategory_with_no_labels() r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 - ))).Returns(new RestResponse() - { - StatusCode = HttpStatusCode.OK, - ContentType = "json", - Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId) - }); + ))).Returns(new RestResponse() + { + StatusCode = HttpStatusCode.OK, + ContentType = "json", + Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId) + }); // Act var apiClient = new CakeMailRestClient(API_KEY, mockRestClient.Object); @@ -104,18 +106,20 @@ public void CreateTemplateCategory_with_isvisiblebydefault_false() r.Resource == "/TemplateV2/CreateCategory/" && r.Parameters.Count(p => p.Name == "apikey" && (string)p.Value == API_KEY && p.Type == ParameterType.HttpHeader) == 1 && r.Parameters.Count(p => p.Type == ParameterType.HttpHeader) == 1 && - r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 5 && + r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 7 && r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "0" && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 && - r.Parameters.Count(p => p.Name == "label[en_US]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 && - r.Parameters.Count(p => p.Name == "label[fr_FR]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 - ))).Returns(new RestResponse() - { - StatusCode = HttpStatusCode.OK, - ContentType = "json", - Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId) - }); + r.Parameters.Count(p => p.Name == "label[0][language]" && (string)p.Value == "en_US" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[0][name]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[1][language]" && (string)p.Value == "fr_FR" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[1][name]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 + ))).Returns(new RestResponse() + { + StatusCode = HttpStatusCode.OK, + ContentType = "json", + Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId) + }); // Act var apiClient = new CakeMailRestClient(API_KEY, mockRestClient.Object); @@ -143,18 +147,20 @@ public void CreateTemplateCategory_with_templatescanbecopied_false() r.Resource == "/TemplateV2/CreateCategory/" && r.Parameters.Count(p => p.Name == "apikey" && (string)p.Value == API_KEY && p.Type == ParameterType.HttpHeader) == 1 && r.Parameters.Count(p => p.Type == ParameterType.HttpHeader) == 1 && - r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 5 && + r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 7 && r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "0" && p.Type == ParameterType.GetOrPost) == 1 && - r.Parameters.Count(p => p.Name == "label[en_US]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 && - r.Parameters.Count(p => p.Name == "label[fr_FR]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 - ))).Returns(new RestResponse() - { - StatusCode = HttpStatusCode.OK, - ContentType = "json", - Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId) - }); + r.Parameters.Count(p => p.Name == "label[0][language]" && (string)p.Value == "en_US" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[0][name]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[1][language]" && (string)p.Value == "fr_FR" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[1][name]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 + ))).Returns(new RestResponse() + { + StatusCode = HttpStatusCode.OK, + ContentType = "json", + Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId) + }); // Act var apiClient = new CakeMailRestClient(API_KEY, mockRestClient.Object); @@ -182,12 +188,14 @@ public void CreateTemplateCategory_with_clientid() r.Resource == "/TemplateV2/CreateCategory/" && r.Parameters.Count(p => p.Name == "apikey" && (string)p.Value == API_KEY && p.Type == ParameterType.HttpHeader) == 1 && r.Parameters.Count(p => p.Type == ParameterType.HttpHeader) == 1 && - r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 6 && + r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 8 && r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 && - r.Parameters.Count(p => p.Name == "label[en_US]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 && - r.Parameters.Count(p => p.Name == "label[fr_FR]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[0][language]" && (string)p.Value == "en_US" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[0][name]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[1][language]" && (string)p.Value == "fr_FR" && p.Type == ParameterType.GetOrPost) == 1 && + r.Parameters.Count(p => p.Name == "label[1][name]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 && r.Parameters.Count(p => p.Name == "client_id" && (long)p.Value == CLIENT_ID && p.Type == ParameterType.GetOrPost) == 1 ))).Returns(new RestResponse() { diff --git a/CakeMail.RestClient/CakeMailRestClient.cs b/CakeMail.RestClient/CakeMailRestClient.cs index e31aab2..804dac1 100644 --- a/CakeMail.RestClient/CakeMailRestClient.cs +++ b/CakeMail.RestClient/CakeMailRestClient.cs @@ -2961,14 +2961,20 @@ public long CreateTemplateCategory(string userKey, IDictionary l }; if (labels != null) { + var labelsCount = 0; foreach (var label in labels) { - parameters.Add(new KeyValuePair(string.Format("label[{0}]", label.Key), label.Value)); + parameters.Add(new KeyValuePair(string.Format("label[{0}][language]", labelsCount), label.Key)); + parameters.Add(new KeyValuePair(string.Format("label[{0}][name]", labelsCount), label.Value)); + labelsCount++; } } if (clientId.HasValue) parameters.Add(new KeyValuePair("client_id", clientId.Value)); - return ExecuteRequest(path, parameters); + // The data returned when creating a new category is a little bit unusual + // Instead of simply returning the unique identifier of the new record like all the other 'Create' methods, for example: {"status":"success","data":"4593766"} + // this method return an object with a single property called 'id' containing the unique dietifyer of the new record, like this: {"status":"success","data":{"id":"14052"}} + return ExecuteRequest(path, parameters, "id"); } /// @@ -2989,7 +2995,11 @@ public bool DeleteTemplateCategory(string userKey, long categoryId, long? client }; if (clientId.HasValue) parameters.Add(new KeyValuePair("client_id", clientId.Value)); - return ExecuteRequest(path, parameters); + // The data returned when creating a new category is a little bit unusual + // Instead of returning a boolean value to indicate success, it returns an empty array!!! + // For example: {"status":"success","data":[]} + ExecuteRequest(path, parameters); + return true; } /// diff --git a/README.md b/README.md index 74efc00..99c9a50 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,8 @@ CakeMailRestAPI is available as a Nuget package. + **2.0.0** - Unique identifiers changed to 'long' instead of 'int'. - "Magic strings" replaced with enums. For example, instead of specifying sort direction with 'asc' and 'desc', you can now use SortDirection.Ascending and SortDirection.Descending. - + - Fix bug in CreateTemplateCategory which prevents creating new categories + - Fix bug in DeleteTemplateCategory which causes an exception to be thrown despite the fact the category was successfuly deleted + **1.0.0** - Initial release