Skip to content

Commit

Permalink
Merge pull request #445 from bostick23/master
Browse files Browse the repository at this point in the history
Added AttachmentFactory
  • Loading branch information
EduLeonPavon authored Aug 26, 2022
2 parents 1160863 + 47cf669 commit 6b16e36
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
19 changes: 19 additions & 0 deletions PrestaSharp/Entities/AuxEntities/AssociationsAttachments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Bukimedia.PrestaSharp.Entities.AuxEntities
{
[XmlType(Namespace = "Bukimedia/PrestaSharp/Entities/AuxEntities")]
public class AssociationsAttachments : PrestaShopEntity
{
public List<AuxEntities.products> products { get; set; }
public AssociationsAttachments()
{
products = new List<products>();
}
}
}
15 changes: 15 additions & 0 deletions PrestaSharp/Entities/AuxEntities/attachments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Bukimedia.PrestaSharp.Entities.AuxEntities
{
[XmlType(Namespace = "Bukimedia/PrestaSharp/Entities/AuxEntities")]
public class attachment : GenericAssociation
{

}
}
28 changes: 28 additions & 0 deletions PrestaSharp/Entities/attachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Bukimedia.PrestaSharp.Entities.AuxEntities;
using RestSharp.Serializers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Bukimedia.PrestaSharp.Entities
{
[XmlType(Namespace = "Bukimedia/PrestaSharp/Entities")]
public class attachment : PrestaShopEntity
{
public long id { get; set; }
public string file { get; set; }
public string file_name { get; set; }
public long file_size { get; set; }
public string mime { get; set; }
public List<AuxEntities.language> name { get; set; }
public List<AuxEntities.language> description { get; set; }
public AssociationsAttachments associations { get; set; }

public attachment()
{
}
}
}
59 changes: 59 additions & 0 deletions PrestaSharp/Factories/AttachmentFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Bukimedia.PrestaSharp.Entities;
using Bukimedia.PrestaSharp.Factories;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Bukimedia.PrestaSharp.Factories
{
public class AttachmentFactory : RestSharpFactory
{
public AttachmentFactory(string BaseUrl, string Account, string SecretKey)
: base(BaseUrl, Account, SecretKey)
{
}

#region Protected methods

public List<Entities.attachment> GetAllAttachments()
{
RestRequest request = this.RequestForFilter("attachments/", "full", null, null, null, "attachments");
return this.Execute<List<Entities.attachment>>(request);
}
public Entities.attachment GetAttachmentByInstance(long Id)
{
RestRequest request = this.RequestForFilter("attachments/" + Id, null, null, null, null, "attachments");
Entities.attachment obj = this.Execute<List<Entities.attachment>>(request)?.First() ?? null;
return obj;
}
public Entities.attachment AddAttachmentFile(string filePath)
{
RestRequest request = this.RequestForAddAttachment(filePath);
return this.ExecuteForAttachment<Entities.attachment>(request);
}

public Entities.attachment UpdateAttachmentFile(long id, string filePath)
{
RestRequest request = this.RequestForUpdateAttachment(filePath, id);
return this.ExecuteForAttachment<Entities.attachment>(request);
}
public void DeleteAttachment(long id)
{
RestRequest request = this.RequestForDelete("attachments", id);
this.Execute<Entities.attachment>(request);
}

#endregion
#region Public Methods
public Entities.attachment UpdateAttachment(attachment attachment)
{
RestRequest request = this.RequestForUpdate("attachments", attachment.id, attachment);
return this.Execute<Entities.attachment>(request);
}
#endregion
}
}
55 changes: 55 additions & 0 deletions PrestaSharp/Factories/RestSharpFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,60 @@ public static byte[] ImageToBinary(string imagePath)
fileStream.Close();
return buffer;
}
protected T ExecuteForAttachment<T>(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);
// Aggiunto meccanismo di "bypass" del controllo sulla validità del certificato SSL
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
// Fine modifica
var response = client.Execute<T>(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.Name + "=" + Parameter.Value + Environment.NewLine + Environment.NewLine;
}
var Exception = new PrestaSharpException(RequestParameters + response.Content, response.ErrorMessage, response.StatusCode, response.ErrorException);
throw Exception;
}
return response.Data;
}
protected RestRequest RequestForAddAttachment(string filePath)
{
var request = new RestRequest();
request.Resource = "/attachments/file/";
request.Method = Method.POST;
request.RequestFormat = DataFormat.Xml;
string fileName = System.IO.Path.GetFileName(filePath);
request.AddParameter("name", fileName);
request.AddParameter("file_name", fileName);
request.AddFile("file", filePath, "application/pdf");
return request;
}
protected RestRequest RequestForUpdateAttachment(string filePath, long id)
{
var request = new RestRequest();
request.Resource = "/attachments/file/" + id;
request.Method = Method.PUT;
request.RequestFormat = DataFormat.Xml;
string fileName = System.IO.Path.GetFileName(filePath);
request.AddParameter("name", fileName);
request.AddParameter("file_name", fileName);
request.AddFile("file", filePath, "application/pdf");
return request;
}
}
}

0 comments on commit 6b16e36

Please sign in to comment.