Skip to content

Commit

Permalink
Refactor: homogenize request body building
Browse files Browse the repository at this point in the history
Fixes #347
  • Loading branch information
mowcixo committed Mar 29, 2019
1 parent 8c49db3 commit d8c8814
Showing 1 changed file with 24 additions and 38 deletions.
62 changes: 24 additions & 38 deletions Factories/RestSharpFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Bukimedia.PrestaSharp.Entities;
using RestSharp.Authenticators;

namespace Bukimedia.PrestaSharp.Factories
Expand All @@ -33,6 +34,24 @@ private void AddWsKey(ref RestRequest request)
request.AddParameter("ws_key", Account, ParameterType.QueryString); // used on every request
}

private void AddBody(ref RestRequest request, IEnumerable<PrestaShopEntity> entities)
{
request.RequestFormat = DataFormat.Xml;
request.XmlSerializer = new Serializers.PrestaSharpSerializer();
var serialized = string.Empty;
foreach (var entity in entities)
{
serialized += ((Serializers.PrestaSharpSerializer) request.XmlSerializer).PrestaSharpSerialize(entity);
}
serialized = "<prestashop>\n" + serialized + "\n</prestashop>";
request.AddParameter("application/xml", serialized, ParameterType.RequestBody);
}

private void AddBody(ref RestRequest request, PrestaShopEntity entity)
{
AddBody(ref request, new List<PrestaShopEntity> {entity});
}

#endregion

#region Protected
Expand Down Expand Up @@ -155,17 +174,8 @@ protected RestRequest RequestForAdd(string Resource, List<Entities.PrestaShopEnt
{
var request = new RestRequest();
request.Resource = Resource;
request.Method = Method.POST;
request.RequestFormat = DataFormat.Xml;
//Hack implementation in PrestaSharpSerializer to serialize PrestaSharp.Entities.AuxEntities.language
request.XmlSerializer = new Serializers.PrestaSharpSerializer();
string serialized = "";
foreach (Entities.PrestaShopEntity Entity in Entities)
{
serialized += ((Serializers.PrestaSharpSerializer)request.XmlSerializer).PrestaSharpSerialize(Entity);
}
serialized = "<prestashop>\n" + serialized + "\n</prestashop>";
request.AddParameter("application/xml", serialized, ParameterType.RequestBody);
request.Method = Method.POST;
AddBody(ref request, Entities);
return request;
}

Expand Down Expand Up @@ -242,15 +252,7 @@ protected RestRequest RequestForUpdate(string Resource, long? Id, Entities.Prest
request.Resource = Resource;
request.AddParameter("id", Id, ParameterType.UrlSegment);
request.Method = Method.PUT;
request.RequestFormat = DataFormat.Xml;
request.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer();
request.AddBody(PrestashopEntity);
//issue #81, #54 fixed
request.Parameters[1].Value = Functions.ReplaceFirstOccurrence(request.Parameters[1].Value.ToString(), "<" + PrestashopEntity.GetType().Name + ">", "<prestashop>\n<" + PrestashopEntity.GetType().Name + ">");
request.Parameters[1].Value = Functions.ReplaceLastOccurrence(request.Parameters[1].Value.ToString(), "</" + PrestashopEntity.GetType().Name + ">", "</" + PrestashopEntity.GetType().Name + ">\n</prestashop>");
//issue #36 fixed
request.Parameters[1].Value = request.Parameters[1].Value.ToString().Replace(" xmlns=\"Bukimedia/PrestaSharp/Entities\"", "");// "xmlns=\"\"");
request.Parameters[1].Value = request.Parameters[1].Value.ToString().Replace(" xmlns=\"Bukimedia/PrestaSharp/Entities/AuxEntities\"", "");// "xmlns=\"\"");
AddBody(ref request, PrestashopEntity);
return request;
}
// For Update List Of Products - start
Expand All @@ -259,15 +261,7 @@ protected RestRequest RequestForUpdateList(string Resource, List<Entities.Presta
var request = new RestRequest();
request.Resource = Resource;
request.Method = Method.PUT;
request.RequestFormat = DataFormat.Xml;
request.XmlSerializer = new Serializers.PrestaSharpSerializer();
string serialized = "";
foreach (Entities.PrestaShopEntity Entity in Entities)
{
serialized += ((Serializers.PrestaSharpSerializer)request.XmlSerializer).PrestaSharpSerialize(Entity);
}
serialized = "<prestashop>\n" + serialized + "\n</prestashop>";
request.AddParameter("application/xml", serialized, ParameterType.RequestBody);
AddBody(ref request, Entities);
return request;
}
// For Update List Of Products - end
Expand Down Expand Up @@ -347,15 +341,7 @@ protected RestRequest RequestForAddOrderHistory(string Resource, List<Entities.P
var request = new RestRequest();
request.Resource = Resource;
request.Method = Method.POST;
request.RequestFormat = DataFormat.Xml;
request.XmlSerializer = new Serializers.PrestaSharpSerializer();
string serialized = "";
foreach (Entities.PrestaShopEntity Entity in Entities)
{
serialized += ((Serializers.PrestaSharpSerializer)request.XmlSerializer).PrestaSharpSerialize(Entity);
}
serialized = "<prestashop>\n" + serialized + "\n</prestashop>";
request.AddParameter("application/xml", serialized);
AddBody(ref request, Entities);
request.AddParameter("sendemail", 1);
return request;
}
Expand Down

0 comments on commit d8c8814

Please sign in to comment.