diff --git a/Factories/AddressFactory.cs b/Factories/AddressFactory.cs index 7a28a899..21a73fec 100644 --- a/Factories/AddressFactory.cs +++ b/Factories/AddressFactory.cs @@ -18,5 +18,11 @@ public AddressFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } - } + + public AddressFactory() + : base() + { + } + + } } diff --git a/Factories/CarrierFactory.cs b/Factories/CarrierFactory.cs index 897654bd..2e52cb85 100644 --- a/Factories/CarrierFactory.cs +++ b/Factories/CarrierFactory.cs @@ -17,6 +17,11 @@ public class CarrierFactory : GenericFactory public CarrierFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public CarrierFactory() + : base() + { + } } } diff --git a/Factories/CartFactory.cs b/Factories/CartFactory.cs index 8a99a7d3..6dec8ae5 100644 --- a/Factories/CartFactory.cs +++ b/Factories/CartFactory.cs @@ -17,6 +17,11 @@ public class CartFactory : GenericFactory public CartFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public CartFactory() + : base() + { + } } } diff --git a/Factories/CategoryFactory.cs b/Factories/CategoryFactory.cs index f225ae6b..a68dc89d 100644 --- a/Factories/CategoryFactory.cs +++ b/Factories/CategoryFactory.cs @@ -18,5 +18,10 @@ public CategoryFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public CategoryFactory() + : base() + { + } } } diff --git a/Factories/CombinationFactory.cs b/Factories/CombinationFactory.cs index dee5cc2e..420facaa 100644 --- a/Factories/CombinationFactory.cs +++ b/Factories/CombinationFactory.cs @@ -18,5 +18,10 @@ public CombinationFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public CombinationFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/ConfigurationFactory.cs b/Factories/ConfigurationFactory.cs index 5f107eab..ef260132 100644 --- a/Factories/ConfigurationFactory.cs +++ b/Factories/ConfigurationFactory.cs @@ -18,5 +18,10 @@ public ConfigurationFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public ConfigurationFactory() + : base() + { + } } } diff --git a/Factories/CountryFactory.cs b/Factories/CountryFactory.cs index 8cf6c60c..b63cfaf0 100644 --- a/Factories/CountryFactory.cs +++ b/Factories/CountryFactory.cs @@ -18,5 +18,10 @@ public CountryFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public CountryFactory() + : base() + { + } } } diff --git a/Factories/CurrencyFactory.cs b/Factories/CurrencyFactory.cs index 2af17540..b80a4d96 100644 --- a/Factories/CurrencyFactory.cs +++ b/Factories/CurrencyFactory.cs @@ -18,5 +18,10 @@ public CurrencyFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public CurrencyFactory() + : base() + { + } } } diff --git a/Factories/CustomerFactory.cs b/Factories/CustomerFactory.cs index 7762915b..467cb98a 100644 --- a/Factories/CustomerFactory.cs +++ b/Factories/CustomerFactory.cs @@ -18,5 +18,10 @@ public CustomerFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public CustomerFactory() + : base() + { + } } } diff --git a/Factories/CustomerMessageFactory.cs b/Factories/CustomerMessageFactory.cs index 68e38e86..2321f0c5 100644 --- a/Factories/CustomerMessageFactory.cs +++ b/Factories/CustomerMessageFactory.cs @@ -18,5 +18,10 @@ public CustomerMessageFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public CustomerMessageFactory() + : base() + { + } } } diff --git a/Factories/CustomerThreadFactory.cs b/Factories/CustomerThreadFactory.cs index 0342bbb3..4c0fe429 100644 --- a/Factories/CustomerThreadFactory.cs +++ b/Factories/CustomerThreadFactory.cs @@ -18,5 +18,10 @@ public CustomerThreadFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public CustomerThreadFactory() + : base() + { + } } } diff --git a/Factories/FilterFactory.cs b/Factories/FilterFactory.cs new file mode 100644 index 00000000..3a696894 --- /dev/null +++ b/Factories/FilterFactory.cs @@ -0,0 +1,234 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Bukimedia.PrestaSharp.Factories +{ + /// + /// Class to be user ine Fcatory method GetByFilter + /// + public class FilterFactory + { + + /// + /// Data sort type + /// + public enum SortType + { + ASC, + DESC + } + + /// + /// Example: key:name value:Apple + /// + public Dictionary Filter = new Dictionary(); + + /// + /// Stack of Sort + /// + public Dictionary SortStack = new Dictionary(); + + /// + /// Field_ASC or Field_DESC. Example: name_ASC or name_DESC + /// For set Use SetSort(field, ordertype) + /// + public string Sort + { + get + { + if (SortStack.Count == 0) { return ""; } + + string str = "["; + foreach (KeyValuePair r in SortStack) + { + str += r.Key + "_" + r.Value.ToString() + ","; + } + str = str.Remove(str.Length - 1); ; + str += "]"; + return str; + } + } + + /// + /// Example: + /// 5 limit to 5. + /// 9,5 Only include the first 5 elements starting from the 10th element. + /// + public string Limit; + + /// + /// Fields to display Example: ["id", "reference"] + /// + public List Display = new List(); + + /// + /// Class to be user ine Fcatory method GetByFilter + /// + public FilterFactory() + { + + } + + /// + /// Class to be user ine Fcatory method GetByFilter + /// Example: key:name value:Apple + /// + /// + /// + public FilterFactory(string key, string value) + { + this.Filter.Add(key, value); + } + + /// + /// Example: key:name value:Apple + /// + /// + /// + /// + public FilterFactory AddFilter(string key, string value) + { + this.Filter.Add(key, value); + return this; + } + + /// + /// Example: key:name value:1 + /// + /// + /// + /// + public FilterFactory AddFilter(string key, int value) + { + this.AddFilter(key, value.ToString()); + return this; + } + + /// + /// Example: key:name value:2 + /// + /// + /// + /// + public FilterFactory AddFilter(string key, long value) + { + this.AddFilter(key, value.ToString()); + return this; + } + + /// + /// Filter values that starts by value + /// + /// + /// + /// + public FilterFactory AddFilterStarts(string key, string value) + { + this.Filter.Add(key, "[" + value + "]%"); + return this; + } + + /// + /// Filter values that ends by value + /// + /// + /// + /// + public FilterFactory AddFilterEnds(string key, string value) + { + this.Filter.Add(key, "%[" + value + "]"); + return this; + } + + /// + /// Filter values that ends by value + /// + /// + /// + /// + public FilterFactory AddFilterContains(string key, string value) + { + this.Filter.Add(key, "%[" + value + "]%"); + return this; + } + + /// + /// Filter values that ends by value + /// + /// + /// + /// + public FilterFactory AddFilterMultipleValues(string key, List value) + { + this.Filter.Add(key, "[" + string.Join("|", value) + "]"); + return this; + } + + /// + /// Filter values that between start an end + /// + /// + /// + /// + public FilterFactory AddFilterBetween(string key, string start, string end) + { + this.Filter.Add(key, "[" + start + "," + end + "]"); + return this; + } + + /// + /// Fields to display Example: ["id", "reference"] + /// + /// + /// + public FilterFactory AddDisplay(string name) + { + this.Display.Add(name); + return this; + } + + /// + /// Data order type + /// + /// + /// + /// + public FilterFactory AddSort(string field, SortType type) + { + this.SortStack.Add(field, type); + return this; + } + + /// + /// Limit of data fetched + /// + /// + /// + /// + public FilterFactory SetLimit(int limit, int offset = 0) + { + this.Limit = offset.ToString() + "," + limit.ToString(); + return this; + } + + /// + /// Convert to a string the field list to disply + /// + /// + /// + public static string DispalyToString(List display) + { + string disp = "full"; + if (display.Count() >= 1) + { + disp = "["; + display.ForEach(d => { disp += d + ","; }); + disp = disp.Remove(disp.Length - 1); ; + disp += "]"; + } + return disp; + } + + } +} diff --git a/Factories/GenericFactory.cs b/Factories/GenericFactory.cs index 41a35740..558b2afa 100644 --- a/Factories/GenericFactory.cs +++ b/Factories/GenericFactory.cs @@ -17,6 +17,11 @@ public GenericFactory(string BaseUrl, string Account, string Password) : base(Ba { } + public GenericFactory() + { + + } + public T Get(long id) { RestRequest request = this.RequestForGet(pluralEntityName, id, singularEntityName); @@ -48,7 +53,7 @@ public List UpdateList(List Entities) { EntitiesToAdd.Add(Entity); } - + RestRequest request = this.RequestForUpdateList(singularEntityName, EntitiesToAdd); return this.Execute>(request); @@ -94,18 +99,26 @@ public List GetByFilter(Dictionary Filter, string Sort, strin /// public List GetByFilter(Dictionary Filter, string Sort, string Limit, List Display) { - string disp = "full"; - if (Display.Count() >= 1) - { - disp = "["; - Display.ForEach(d => { disp += d + ","; }); - disp = disp.Remove(disp.Length - 1); ; - disp += "]"; - } + string disp = FilterFactory.DispalyToString(Display); RestRequest request = this.RequestForFilter(pluralEntityName, disp, Filter, Sort, Limit, pluralEntityName); return this.ExecuteForFilter>(request); } + /// + /// More information about filtering: http://doc.prestashop.com/display/PS14/Chapter+8+-+Advanced+Use + /// + /// + /// + public List GetByFilter(FilterFactory filter) + { + RestRequest request = this.RequestForFilter( + pluralEntityName, + filter, + pluralEntityName + ); + return this.ExecuteForFilter>(request); + } + /// /// More information about filtering: http://doc.prestashop.com/display/PS14/Chapter+8+-+Advanced+Use /// @@ -120,6 +133,27 @@ public List GetIdsByFilter(Dictionary Filter, string Sort, return (from t in aux where t.id.HasValue select t.id.Value).ToList(); } + /// + /// More information about filtering: http://doc.prestashop.com/display/PS14/Chapter+8+-+Advanced+Use + /// Display in FilterFactory doesn't apply to this Method, + /// because allways display field id + /// + /// + /// + public List GetIdsByFilter(FilterFactory filter) + { + RestRequest request = this.RequestForFilter( + pluralEntityName, + "[id]", + filter.Filter, + filter.Sort, + filter.Limit, + pluralEntityName + ); + List aux = this.Execute>(request); + return (from t in aux where t.id.HasValue select t.id.Value).ToList(); + } + /// /// Get all entities. /// @@ -146,5 +180,18 @@ public List AddList(List Entities) return this.Execute>(request); } + /// + /// Verify if the Item exists + /// + /// + /// + public bool Exists(long id) + { + Dictionary filter = new Dictionary(); + filter.Add("id", id.ToString()); + return this.GetByFilter(filter, "id_asc", "1").Count == 0 + ? false : true; + } + } } diff --git a/Factories/GroupFactory.cs b/Factories/GroupFactory.cs index a01d2389..0d6e8601 100644 --- a/Factories/GroupFactory.cs +++ b/Factories/GroupFactory.cs @@ -18,5 +18,10 @@ public GroupFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public GroupFactory() + : base() + { + } } } diff --git a/Factories/GuestFactory.cs b/Factories/GuestFactory.cs index 191ecb56..d3f59cdf 100644 --- a/Factories/GuestFactory.cs +++ b/Factories/GuestFactory.cs @@ -18,5 +18,10 @@ public GuestFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public GuestFactory() + : base() + { + } } } diff --git a/Factories/ImageFactory.cs b/Factories/ImageFactory.cs index 1888c57a..ddfd3b03 100644 --- a/Factories/ImageFactory.cs +++ b/Factories/ImageFactory.cs @@ -15,6 +15,11 @@ public ImageFactory(string BaseUrl, string Account, string SecretKey) { } + public ImageFactory() + : base() + { + } + #region Protected methods protected List GetAllImages(string Resource){ @@ -22,6 +27,17 @@ public ImageFactory(string BaseUrl, string Account, string SecretKey) return this.Execute>(request); } + /// + /// Verify if a resourse exist + /// + /// + /// + /// + protected bool Exists(string Resource, long id) + { + return this.GetAllImages(Resource).Exists(i=>i.id == id); + } + protected List GetAllImageTypes(string Resource) { RestRequest request = this.RequestForFilter("images/" + Resource, "full", null, null, null, "image_types"); @@ -117,6 +133,16 @@ public byte[] GetManufacturerImage(long ManufacturerId, long ImageId) return this.ExecuteForImage(request); } + /// + /// Verify if a manufacturer image exist + /// + /// + /// + public bool ManufacturerImageExists(long id_image) + { + return this.Exists("manufacturers", id_image); + } + #endregion Manufacturer images #region Product images @@ -219,6 +245,16 @@ public byte[] GetProductImage(long ProductId, long ImageId) return this.ExecuteForImage(request); } + /// + /// Verify if a product image exist + /// + /// + /// + public bool ProductImageExists(long id_image) + { + return this.Exists("products", id_image); + } + #endregion Product images #region Category images @@ -270,6 +306,16 @@ public byte[] GetCategoryImage(long CategoryId, string TypeName) return this.ExecuteForImage(request); } + /// + /// Verify if a category image exist + /// + /// + /// + public bool CategoryImageExists(long id_image) + { + return this.Exists("categories", id_image); + } + #endregion Category images } diff --git a/Factories/LanguageFactory.cs b/Factories/LanguageFactory.cs index e9377020..52b5b7f8 100644 --- a/Factories/LanguageFactory.cs +++ b/Factories/LanguageFactory.cs @@ -18,5 +18,10 @@ public LanguageFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public LanguageFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/ManufacturerFactory.cs b/Factories/ManufacturerFactory.cs index ed0f2ffb..03d18f5a 100644 --- a/Factories/ManufacturerFactory.cs +++ b/Factories/ManufacturerFactory.cs @@ -17,6 +17,11 @@ public class ManufacturerFactory : GenericFactory public ManufacturerFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public ManufacturerFactory() + : base() + { + } } } diff --git a/Factories/OrderCarrierFactory.cs b/Factories/OrderCarrierFactory.cs index 870b8797..91645fc1 100644 --- a/Factories/OrderCarrierFactory.cs +++ b/Factories/OrderCarrierFactory.cs @@ -17,6 +17,11 @@ public class OrderCarrierFactory : GenericFactory public OrderCarrierFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public OrderCarrierFactory() + : base() + { + } } } diff --git a/Factories/OrderCartRuleFactory.cs b/Factories/OrderCartRuleFactory.cs index 7531a202..472ca5f5 100644 --- a/Factories/OrderCartRuleFactory.cs +++ b/Factories/OrderCartRuleFactory.cs @@ -18,5 +18,10 @@ public OrderCartRuleFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public OrderCartRuleFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/OrderDetailFactory.cs b/Factories/OrderDetailFactory.cs index 00ed1835..a9f18d06 100644 --- a/Factories/OrderDetailFactory.cs +++ b/Factories/OrderDetailFactory.cs @@ -18,5 +18,10 @@ public OrderDetailFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public OrderDetailFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/OrderFactory.cs b/Factories/OrderFactory.cs index 97d201c4..ddec830f 100644 --- a/Factories/OrderFactory.cs +++ b/Factories/OrderFactory.cs @@ -17,6 +17,11 @@ public class OrderFactory : GenericFactory public OrderFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public OrderFactory() + : base() + { + } } } diff --git a/Factories/OrderHistoryFactory.cs b/Factories/OrderHistoryFactory.cs index d0bcaace..d2e0a8d6 100644 --- a/Factories/OrderHistoryFactory.cs +++ b/Factories/OrderHistoryFactory.cs @@ -18,5 +18,10 @@ public OrderHistoryFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public OrderHistoryFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/OrderInvoiceFactory.cs b/Factories/OrderInvoiceFactory.cs index 66a7978a..e7b360f3 100644 --- a/Factories/OrderInvoiceFactory.cs +++ b/Factories/OrderInvoiceFactory.cs @@ -18,5 +18,10 @@ public OrderInvoiceFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public OrderInvoiceFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/OrderPaymentFactory.cs b/Factories/OrderPaymentFactory.cs index 56ece199..b75029d7 100644 --- a/Factories/OrderPaymentFactory.cs +++ b/Factories/OrderPaymentFactory.cs @@ -18,5 +18,10 @@ public OrderPaymentFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public OrderPaymentFactory() + : base() + { + } } } diff --git a/Factories/OrderStateFactory.cs b/Factories/OrderStateFactory.cs index d31685be..e9bd4739 100644 --- a/Factories/OrderStateFactory.cs +++ b/Factories/OrderStateFactory.cs @@ -17,6 +17,11 @@ public class OrderStateFactory : GenericFactory public OrderStateFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public OrderStateFactory() + : base() + { + } } } diff --git a/Factories/ProductFactory.cs b/Factories/ProductFactory.cs index b9af4221..21fce434 100644 --- a/Factories/ProductFactory.cs +++ b/Factories/ProductFactory.cs @@ -18,5 +18,10 @@ public ProductFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public ProductFactory() + :base() + { + } } } diff --git a/Factories/ProductFeatureFactory.cs b/Factories/ProductFeatureFactory.cs index e903ff94..abdbd0c5 100644 --- a/Factories/ProductFeatureFactory.cs +++ b/Factories/ProductFeatureFactory.cs @@ -18,5 +18,10 @@ public ProductFeatureFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public ProductFeatureFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/ProductFeatureValueFactory.cs b/Factories/ProductFeatureValueFactory.cs index ec23ed77..c4f005b5 100644 --- a/Factories/ProductFeatureValueFactory.cs +++ b/Factories/ProductFeatureValueFactory.cs @@ -18,5 +18,10 @@ public ProductFeatureValueFactory(string BaseUrl, string Account, string SecretK : base(BaseUrl, Account, SecretKey) { } + + public ProductFeatureValueFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/ProductOptionFactory.cs b/Factories/ProductOptionFactory.cs index b416aed3..0d426ba1 100644 --- a/Factories/ProductOptionFactory.cs +++ b/Factories/ProductOptionFactory.cs @@ -18,5 +18,10 @@ public ProductOptionFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public ProductOptionFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/ProductOptionValueFactory.cs b/Factories/ProductOptionValueFactory.cs index b1af5f9a..623f15eb 100644 --- a/Factories/ProductOptionValueFactory.cs +++ b/Factories/ProductOptionValueFactory.cs @@ -18,5 +18,10 @@ public ProductOptionValueFactory(string BaseUrl, string Account, string SecretKe : base(BaseUrl, Account, SecretKey) { } + + public ProductOptionValueFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/ProductSupplierFactory.cs b/Factories/ProductSupplierFactory.cs index af09e075..29bcff7d 100644 --- a/Factories/ProductSupplierFactory.cs +++ b/Factories/ProductSupplierFactory.cs @@ -17,5 +17,10 @@ public ProductSupplierFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public ProductSupplierFactory() + : base() + { + } } } diff --git a/Factories/RestSharpFactory.cs b/Factories/RestSharpFactory.cs index a0a36d73..fc499d46 100644 --- a/Factories/RestSharpFactory.cs +++ b/Factories/RestSharpFactory.cs @@ -15,47 +15,44 @@ namespace Bukimedia.PrestaSharp.Factories { public abstract class RestSharpFactory { - protected string BaseUrl{get;set;} - protected string Account{get;set;} - protected string Password{get;set;} + protected string BaseUrl { get; set; } + protected string Account { get; set; } + protected string Password { get; set; } public RestSharpFactory(string BaseUrl, string Account, string Password) { this.BaseUrl = BaseUrl; this.Account = Account; this.Password = Password; + // If you set one time the credentials in a Factory then is + // not necessary to declare again because it will be cached in the + // static class Credentials + Credentials.BaseUrl = this.BaseUrl; + Credentials.Account = this.Account; + Credentials.Password = this.Password; } - protected T Execute(RestRequest Request) where T : new() + public RestSharpFactory() + { + this.BaseUrl = Credentials.BaseUrl; + this.Account = Credentials.Account; + this.Password = Credentials.Password; + } + + protected T Execute(RestRequest request) where T : new() { var client = new RestClient(); client.AddHandler("text/html", new PrestaSharpTextErrorDeserializer()); client.BaseUrl = new Uri(this.BaseUrl); //client.Authenticator = new HttpBasicAuthenticator(this.Account, this.Password); - Request.AddParameter("ws_key", this.Account, ParameterType.QueryString); // used on every request - if (Request.Method == Method.GET) + request.AddParameter("ws_key", this.Account, ParameterType.QueryString); // used on every request + if (request.Method == Method.GET) { client.ClearHandlers(); client.AddHandler("text/xml", new Bukimedia.PrestaSharp.Deserializers.PrestaSharpDeserializer()); } - var response = client.Execute(Request); - if (response.StatusCode == HttpStatusCode.InternalServerError - || response.StatusCode == HttpStatusCode.ServiceUnavailable - || response.StatusCode == HttpStatusCode.BadRequest - || response.StatusCode == HttpStatusCode.Unauthorized - || response.StatusCode == HttpStatusCode.MethodNotAllowed - || response.StatusCode == HttpStatusCode.Forbidden - || response.StatusCode == HttpStatusCode.NotFound - || response.StatusCode == 0) - { - string RequestParameters = Environment.NewLine; - foreach (RestSharp.Parameter Parameter in Request.Parameters) - { - RequestParameters += Parameter.Value + Environment.NewLine + Environment.NewLine; - } - var Exception = new PrestaSharpException(RequestParameters + response.Content, response.ErrorMessage, response.StatusCode, response.ErrorException); - throw Exception; - } + var response = client.Execute(request); + this.CheckResponse(response, request); return response.Data; } @@ -82,73 +79,71 @@ public RestSharpFactory(string BaseUrl, string Account, string Password) } } - protected T ExecuteForFilter(RestRequest Request) where T : new() + protected T ExecuteForFilter(RestRequest request) where T : new() { var client = new RestClient(); client.BaseUrl = new Uri(this.BaseUrl); //client.Authenticator = new HttpBasicAuthenticator(this.Account, this.Password); - Request.AddParameter("ws_key", this.Account, ParameterType.QueryString); // used on every request + request.AddParameter("ws_key", this.Account, ParameterType.QueryString); // used on every request client.ClearHandlers(); client.AddHandler("text/xml", new Bukimedia.PrestaSharp.Deserializers.PrestaSharpDeserializer()); - var response = client.Execute(Request); - if (response.StatusCode == HttpStatusCode.InternalServerError - || response.StatusCode == HttpStatusCode.ServiceUnavailable - || response.StatusCode == HttpStatusCode.BadRequest - || response.StatusCode == HttpStatusCode.Unauthorized - || response.StatusCode == HttpStatusCode.MethodNotAllowed - || response.StatusCode == HttpStatusCode.Forbidden - || response.StatusCode == HttpStatusCode.NotFound - || response.StatusCode == 0) - { - string RequestParameters = Environment.NewLine; - foreach (RestSharp.Parameter Parameter in Request.Parameters) - { - RequestParameters += Parameter.Value + Environment.NewLine + Environment.NewLine; - } - var Exception = new PrestaSharpException(RequestParameters + response.Content, response.ErrorMessage, response.StatusCode, response.ErrorException); - throw Exception; - } + var response = client.Execute(request); + this.CheckResponse(response, request); return response.Data; } - protected List ExecuteForGetIds(RestRequest Request, string RootElement) where T : new() + protected List ExecuteForGetIds(RestRequest request, string RootElement) where T : new() { var client = new RestClient(); client.BaseUrl = new Uri(this.BaseUrl); //client.Authenticator = new HttpBasicAuthenticator(this.Account, this.Password); - Request.AddParameter("ws_key", this.Account, ParameterType.QueryString); - var response = client.Execute(Request); + request.AddParameter("ws_key", this.Account, ParameterType.QueryString); + var response = client.Execute(request); + this.CheckResponse(response, request); XDocument xDcoument = XDocument.Parse(response.Content); var ids = (from doc in xDcoument.Descendants(RootElement) select long.Parse(doc.Attribute("id").Value)).ToList(); return ids; } - protected byte[] ExecuteForImage(RestRequest Request) + protected byte[] ExecuteForImage(RestRequest request) { var client = new RestClient(); client.BaseUrl = new Uri(this.BaseUrl); //client.Authenticator = new HttpBasicAuthenticator(this.Account, this.Password); - Request.AddParameter("ws_key", this.Account, ParameterType.QueryString); - var response = client.Execute(Request); + request.AddParameter("ws_key", this.Account, ParameterType.QueryString); + var response = client.Execute(request); + this.CheckResponse(response, request); + return response.RawBytes; + } + + protected void CheckResponse(IRestResponse response, RestRequest request) + { + if (response.StatusCode == HttpStatusCode.Unauthorized) + { + throw new PrestaSharpNotAuthorized(response.ErrorMessage); + } + if (response.StatusCode == HttpStatusCode.InternalServerError || response.StatusCode == HttpStatusCode.ServiceUnavailable || response.StatusCode == HttpStatusCode.BadRequest - || response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.MethodNotAllowed || response.StatusCode == HttpStatusCode.Forbidden || response.StatusCode == HttpStatusCode.NotFound || response.StatusCode == 0) { + if (response.Content.Trim().Equals("")) + { + throw new PrestaSharpNotFoundException(response.ErrorMessage); + } + string RequestParameters = Environment.NewLine; - foreach (RestSharp.Parameter Parameter in Request.Parameters) + foreach (RestSharp.Parameter Parameter in request.Parameters) { RequestParameters += Parameter.Value + Environment.NewLine + Environment.NewLine; } - var Exception = new PrestaSharpException(RequestParameters + response.Content, response.ErrorMessage, response.StatusCode, response.ErrorException); - throw Exception; + throw new PrestaSharpException(RequestParameters + response.Content, response.ErrorMessage, response.StatusCode, response.ErrorException); } - return response.RawBytes; } protected RestRequest RequestForGet(string Resource, long? Id, string RootElement) @@ -205,7 +200,7 @@ protected RestRequest RequestForAddImage(string Resource, long? Id, string Image request.AddFile("image", ImagePath); return request; } - + /// /// More information about image management: http://doc.prestashop.com/display/PS15/Chapter+9+-+Image+management /// @@ -269,7 +264,7 @@ protected RestRequest RequestForUpdate(string Resource, long? Id, Entities.Prest request.Parameters[1].Value = request.Parameters[1].Value.ToString().Replace(" xmlns=\"Bukimedia/PrestaSharp/Entities/AuxEntities\"", "");// "xmlns=\"\""); return request; } - // For Update List Of Products - start + // For Update List Of Products - start protected RestRequest RequestForUpdateList(string Resource, List Entities) { var request = new RestRequest(); @@ -329,7 +324,7 @@ protected RestRequest RequestForDelete(string Resource, long? Id) /// /// /// - protected RestRequest RequestForFilter(string Resource, string Display, Dictionary Filter, string Sort, string Limit, string RootElement) + protected RestRequest RequestForFilter(string Resource, string Display, Dictionary Filter, string Sort, string Limit, string RootElement) { var request = new RestRequest(); request.Resource = Resource; @@ -358,6 +353,24 @@ protected RestRequest RequestForFilter(string Resource, string Display, Dictiona return request; } + /// + /// More information about filtering: http://doc.prestashop.com/display/PS14/Chapter+8+-+Advanced+Use + /// + /// + /// + /// + /// + protected RestRequest RequestForFilter(string Resource, FilterFactory filter, string RootElement) + { + return this.RequestForFilter(Resource, + FilterFactory.DispalyToString(filter.Display), + filter.Filter, + filter.Sort, + filter.Limit, + RootElement + ); + } + protected RestRequest RequestForAddOrderHistory(string Resource, List Entities) { var request = new RestRequest(); diff --git a/Factories/ShopFactory.cs b/Factories/ShopFactory.cs index 8be6f509..245e6b6d 100644 --- a/Factories/ShopFactory.cs +++ b/Factories/ShopFactory.cs @@ -18,5 +18,10 @@ public ShopFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public ShopFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/SpecificPriceFactory.cs b/Factories/SpecificPriceFactory.cs index 9fb6eaa1..3f7be19c 100644 --- a/Factories/SpecificPriceFactory.cs +++ b/Factories/SpecificPriceFactory.cs @@ -9,7 +9,7 @@ namespace Bukimedia.PrestaSharp.Factories { - public class SpecificPriceFactory : GenericFactory + public class SpecificPriceFactory : GenericFactory { protected override string singularEntityName { get { return "specific_price"; } } protected override string pluralEntityName { get { return "specific_prices"; } } @@ -18,5 +18,10 @@ public SpecificPriceFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } - } + + public SpecificPriceFactory() + : base() + { + } + } } diff --git a/Factories/SpecificPriceRuleFactory.cs b/Factories/SpecificPriceRuleFactory.cs index e6adcde4..381b0a0f 100644 --- a/Factories/SpecificPriceRuleFactory.cs +++ b/Factories/SpecificPriceRuleFactory.cs @@ -9,7 +9,7 @@ namespace Bukimedia.PrestaSharp.Factories { - public class SpecificPriceRuleFactory : GenericFactory + public class SpecificPriceRuleFactory : GenericFactory { protected override string singularEntityName { get { return "specific_price_rule"; } } protected override string pluralEntityName { get { return "specific_price_rules"; } } @@ -18,5 +18,10 @@ public SpecificPriceRuleFactory(string BaseUrl, string Account, string SecretKey : base(BaseUrl, Account, SecretKey) { } - } + + public SpecificPriceRuleFactory() + : base() + { + } + } } diff --git a/Factories/StateFactory.cs b/Factories/StateFactory.cs index 1f8f9c40..f005adcd 100644 --- a/Factories/StateFactory.cs +++ b/Factories/StateFactory.cs @@ -17,6 +17,11 @@ public class StateFactory : GenericFactory public StateFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public StateFactory() + : base() + { + } } } diff --git a/Factories/StockAvailableFactory.cs b/Factories/StockAvailableFactory.cs index fd332621..eb882f6e 100644 --- a/Factories/StockAvailableFactory.cs +++ b/Factories/StockAvailableFactory.cs @@ -18,5 +18,10 @@ public StockAvailableFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public StockAvailableFactory() + : base() + { + } } } \ No newline at end of file diff --git a/Factories/SupplierFactory.cs b/Factories/SupplierFactory.cs index 0f4466fb..f28b1ada 100644 --- a/Factories/SupplierFactory.cs +++ b/Factories/SupplierFactory.cs @@ -18,5 +18,10 @@ public SupplierFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public SupplierFactory() + : base() + { + } } } diff --git a/Factories/TagFactory.cs b/Factories/TagFactory.cs index aa80dd41..a0890ccf 100644 --- a/Factories/TagFactory.cs +++ b/Factories/TagFactory.cs @@ -18,5 +18,10 @@ public TagFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public TagFactory() + : base() + { + } } } diff --git a/Factories/TaxFactory.cs b/Factories/TaxFactory.cs index 0a14792c..63553557 100644 --- a/Factories/TaxFactory.cs +++ b/Factories/TaxFactory.cs @@ -17,6 +17,11 @@ public class TaxFactory : GenericFactory public TaxFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public TaxFactory() + : base() + { + } } } diff --git a/Factories/TaxRuleFactory.cs b/Factories/TaxRuleFactory.cs index a4828243..6bab37e7 100644 --- a/Factories/TaxRuleFactory.cs +++ b/Factories/TaxRuleFactory.cs @@ -18,5 +18,10 @@ public TaxRuleFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public TaxRuleFactory() + : base() + { + } } } diff --git a/Factories/TaxRuleGroupFactory.cs b/Factories/TaxRuleGroupFactory.cs index a8a75010..81999279 100644 --- a/Factories/TaxRuleGroupFactory.cs +++ b/Factories/TaxRuleGroupFactory.cs @@ -17,6 +17,11 @@ public class TaxRuleGroupFactory : GenericFactory public TaxRuleGroupFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public TaxRuleGroupFactory() + : base() + { + } } } diff --git a/Factories/WarehouseFactory.cs b/Factories/WarehouseFactory.cs index 4fa0a166..e496b8fe 100644 --- a/Factories/WarehouseFactory.cs +++ b/Factories/WarehouseFactory.cs @@ -18,5 +18,10 @@ public WarehouseFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { } + + public WarehouseFactory() + : base() + { + } } } diff --git a/Factories/WarehouseProductLocationFactory.cs b/Factories/WarehouseProductLocationFactory.cs index 038e2fe1..8a6af483 100644 --- a/Factories/WarehouseProductLocationFactory.cs +++ b/Factories/WarehouseProductLocationFactory.cs @@ -16,6 +16,11 @@ public class WarehouseProductLocationFactory : GenericFactory public ZoneFactory(string BaseUrl, string Account, string SecretKey) : base(BaseUrl, Account, SecretKey) { - } + } + + public ZoneFactory() + : base() + { + } } } diff --git a/Lib/Credentials.cs b/Lib/Credentials.cs new file mode 100644 index 00000000..c7330e27 --- /dev/null +++ b/Lib/Credentials.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bukimedia.PrestaSharp.Lib +{ + /// + /// Static class that chaches the PrastaShop API Credencials + /// + internal static class Credentials + { + /// + /// The Presta shop api Url + /// Ex: http://www.myweb.com/api + /// + public static string BaseUrl { get; set; } + + /// + /// Presta shop api key + /// + public static string Account { get; set; } + + /// + /// Always blank??? + /// + public static string Password = ""; + + } +} diff --git a/Lib/Functions.cs b/Lib/Functions.cs index 25f6f773..a6fc98f2 100644 --- a/Lib/Functions.cs +++ b/Lib/Functions.cs @@ -630,5 +630,28 @@ internal static string ReplaceLastOccurrence(string Source, string Find, string string result = Source.Remove(Place, Find.Length).Insert(Place, Replace); return result; } + + /// + /// remove caracteres as in Prestashop Validate:isCatalogName($name) + /// + /// + /// + internal static string CleanAsIsCatalogName(string str) + { + Regex replace = new Regex("^|\\)|\\(|<|>|;|=|#|{|}|\b", RegexOptions.IgnoreCase); + return replace.Replace(str, " "); + } + + /// + /// remove caracteres as in Prestashop Validate:isGenericName($name) + /// + /// + /// + internal static string CleanAsIsGenericName(string str) + { + Regex replace = new Regex("\\)|\\(|<|>|#|=|{|}|\b", RegexOptions.IgnoreCase); + return replace.Replace(str, " "); + } + } } diff --git a/PrestaSharpException.cs b/PrestaSharpException.cs index eddd0382..1f71fc8d 100644 --- a/PrestaSharpException.cs +++ b/PrestaSharpException.cs @@ -18,6 +18,11 @@ public PrestaSharpException() { } + public PrestaSharpException(string Message) + : base(Message) + { + } + public PrestaSharpException(string ResponseContent, string ResponseErrorMessage, Exception ResponseErrorException) : base(ResponseContent + " " + ResponseErrorMessage, ResponseErrorException) { @@ -32,6 +37,5 @@ public PrestaSharpException(string ResponseContent, string ResponseErrorMessage, this.ResponseErrorMessage = ResponseErrorMessage; this.ResponseHttpStatusCode = ResponseHttpStatusCode; } - } } diff --git a/PrestaSharpNotAuthorized.cs b/PrestaSharpNotAuthorized.cs new file mode 100644 index 00000000..e7500e38 --- /dev/null +++ b/PrestaSharpNotAuthorized.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace Bukimedia.PrestaSharp +{ + class PrestaSharpNotAuthorized : PrestaSharpException + { + public PrestaSharpNotAuthorized() + : base() + { + } + + public PrestaSharpNotAuthorized(string Message) + : base(Message) + { + } + + public PrestaSharpNotAuthorized(string ResponseContent, string ResponseErrorMessage, Exception ResponseErrorException) + : base(ResponseContent, ResponseErrorMessage, ResponseErrorException) + { + this.ResponseContent = ResponseContent; + this.ResponseErrorMessage = ResponseErrorMessage; + } + + public PrestaSharpNotAuthorized(string ResponseContent, string ResponseErrorMessage, HttpStatusCode ResponseHttpStatusCode, Exception ResponseErrorException) + : base(ResponseContent, ResponseErrorMessage, ResponseHttpStatusCode, ResponseErrorException) + { + this.ResponseContent = ResponseContent; + this.ResponseErrorMessage = ResponseErrorMessage; + this.ResponseHttpStatusCode = ResponseHttpStatusCode; + } + } +} diff --git a/PrestaSharpNotFoundException.cs b/PrestaSharpNotFoundException.cs new file mode 100644 index 00000000..b25023d0 --- /dev/null +++ b/PrestaSharpNotFoundException.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace Bukimedia.PrestaSharp +{ + /// + /// Exception for entities not found in database + /// + class PrestaSharpNotFoundException : PrestaSharpException + { + public PrestaSharpNotFoundException() + : base() + { + } + + public PrestaSharpNotFoundException(string Message) + : base(Message) + { + } + + public PrestaSharpNotFoundException(string ResponseContent, string ResponseErrorMessage, Exception ResponseErrorException) + : base(ResponseContent, ResponseErrorMessage ,ResponseErrorException) + { + this.ResponseContent = ResponseContent; + this.ResponseErrorMessage = ResponseErrorMessage; + } + + public PrestaSharpNotFoundException(string ResponseContent, string ResponseErrorMessage, HttpStatusCode ResponseHttpStatusCode, Exception ResponseErrorException) + : base(ResponseContent, ResponseErrorMessage, ResponseHttpStatusCode, ResponseErrorException) + { + this.ResponseContent = ResponseContent; + this.ResponseErrorMessage = ResponseErrorMessage; + this.ResponseHttpStatusCode = ResponseHttpStatusCode; + } + + } +} diff --git a/README.md b/README.md index 26159877..08fc542b 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,15 @@ PrestaSharp uses the RestSharp library to consume the Prestashop services. string Account = "ASDLKJOIQWEPROQWUPRPOQPPRQOW"; string Password = ""; ManufacturerFactory ManufacturerFactory = new ManufacturerFactory(BaseUrl, Account, Password); + + OR + + Credentials.BaseUrl = "http://www.myweb.com/api"; + Credentials.Account = "ASDLKJOIQWEPROQWUPRPOQPPRQOW"; + Credentials.Password = ""; + ManufacturerFactory ManufacturerFactory = new ManufacturerFactory(); + + ``` 2) Perform CRUD actions through the client: @@ -54,6 +63,12 @@ PrestaSharp uses the RestSharp library to consume the Prestashop services. dtn.Add("name", "Metallica"); List manufacturers = ManufacturerFactory.GetByFilter(dtn, null, null); ``` + OR +``` + FilterFactory filter = new FilterFactory(); + filter.AddFilter(nameof(manufacturer.name), "Metallica"); + List manufacturers = ManufacturerFactory.GetByFilter(filter); +``` 4) Get by filter with wildcards. This sample retrieves the manufacturers which name starts with "Metall": @@ -62,6 +77,12 @@ PrestaSharp uses the RestSharp library to consume the Prestashop services. dtn.Add("name", "[Metall]%"); List manufacturers = ManufacturerFactory.GetByFilter(dtn, null, null); ``` + OR +``` + FilterFactory filter = new FilterFactory(); + filter.AddFilterStarts(nameof(manufacturer.name), "Metall"); + List manufacturers = ManufacturerFactory.GetByFilter(filter); +``` 5) Get ids by filter. This sample retrieves the list of the manufacturers ids which name is "Metallica": @@ -70,6 +91,12 @@ PrestaSharp uses the RestSharp library to consume the Prestashop services. dtn.Add("name", "Metallica"); List ids = ManufacturerFactory.GetIdsByFilter(dtn, null, null); ``` + OR +``` + FilterFactory filter = new FilterFactory(); + filter.AddFilter(nameof(manufacturer.name), "Metallica"); + List manufacturers = ManufacturerFactory.GetIdsByFilter(filter); +``` 6) Get ids by filter with wildcards. This sample retrieves the list of the manufacturers ids which name starts with "Metall": @@ -86,6 +113,14 @@ PrestaSharp uses the RestSharp library to consume the Prestashop services. dtn.Add("name", "[Metall]%"); List manufacturers = ManufacturerFactory.GetByFilter(dtn, "name_ASC", "5"); ``` + OR +``` + FilterFactory filter = new FilterFactory(); + filter.AddFilterStarts(nameof(manufacturer.name), "Metall"); + filter.AddSort(nameof(manufacturer.name), FilterFactory.SortType.ASC); + filter.SetLimit(5, 0); + List manufacturers = ManufacturerFactory.GetByFilter(filter); +``` 8) Get by filter for pagination. This sample retrieves the top five manufacturers from tenth position in ascendent sorting which name starts with "Metall": @@ -94,6 +129,14 @@ PrestaSharp uses the RestSharp library to consume the Prestashop services. dtn.Add("name", "[Metall]%"); List manufacturers = ManufacturerFactory.GetByFilter(dtn, "name_ASC", "[9,5]"); ``` + OR +``` + FilterFactory filter = new FilterFactory(); + filter.AddFilterStarts(nameof(manufacturer.name), "Metall"); + filter.AddSort(nameof(manufacturer.name), FilterFactory.SortType.ASC); + filter.SetLimit(5, 9); + List manufacturers = ManufacturerFactory.GetByFilter(filter); +``` 9) Get by filter by range date. This sample retrieves the orders in a date range: @@ -106,6 +149,31 @@ PrestaSharp uses the RestSharp library to consume the Prestashop services. filter.Add("date_add", "[" + dFrom + "," + dTo + "]"); List PrestaSharpOrderIds = this.OrderFactory.GetIdsByFilter(filter, "id_DESC", null); ``` + OR +``` + DateTime StartDate = new DateTime (2016, 1, 1); + DateTime StartDate = new DateTime (2016, 1, 31); + string dFrom = string.Format("{0:yyyy-MM-dd HH:mm:ss}", StartDate); + string dTo = string.Format("{0:yyyy-MM-dd HH:mm:ss}", EndDate); + FilterFactory filter = new FilterFactory(); + filter.AddFilterBetween("date_add", dFrom, dTo); + filter.AddSort("id", FilterFactory.SortType.ASC); + List PrestaSharpOrderIds = this.OrderFactory.GetIdsByFilter(filter); +``` + +10) Verify if a product exists + +``` + ProductFactory productFactory = new ProductFactory(); + var exists = productFactory.Exists(1); +``` + +11) Verify if a product exists + +``` + ImageFactory imageFactory = new ImageFactory(); + var exists = imageFactory.ProductImageExists(1); // id_image +``` ## Supported resources - Address