Jump to content

Webservice e .net Framework


Totti

Recommended Posts

  • 1 year later...

Ho studiato PHP e adesso faccio diretto con questo, purtroppo c'è poco supporto qui.

 

Comunque io quella dll l'ho usata qualche volta e funzionava, ogni tanto qualche bug ma andava.

 

Se posti un pò di codice vedo di aiutarti.

Link to comment
Share on other sites

Hello Totti, thanks for your help.

This is my code but always I received the error 401 Unutorized, but what I can't understand is that I received the same errore if I try to access from web. If you want to access by web at your presta-webservice in username what you will put in username?

 

well, this is my C# code


    public class Prestashop_WebClient
    {
        private string WebService_URL = "http://mydomain.com/eshop/api";
        private string WebService_LoginName = "I22R8VQ91YR2KGPHYVPA4I7TU661CPIM     ";
        private string WebService_Password = "";

        public Prestashop_WebClient()
        {

        }

        public string ReadRecord(string TableName, string UniqueID)
        {
            try{
                string RequestURL = WebService_URL + "/" + TableName; // +"/" + UniqueID;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(RequestURL);
                webRequest.Credentials = new NetworkCredential(WebService_LoginName, WebService_Password);
                webRequest.Method = CRUDmethod.Read;
                webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string strData = reader.ReadToEnd();
                reader.Close();
                return strData;
            } catch(Exception ex)
            {
                MessageBox.Show("ReadRecord-" + ex.Message);
                return "";
            }
        }

        public string WriteRecord(string TableName, string UniqueID)
        {
            try
            {
                string RequestURL = WebService_URL + "/" + TableName; // +"/" + UniqueID;
                
                //products/1
                string BaseUrl = "http://www.mydomain.com.com/eshop/api";
                string Account = "X0SSWJII2RP9PVC586QP8QKMLL4K2TQG";    //X0SSWJII2RP9PVC586QP8QKMLL4K2TQG
                string Password = "";

                ManufacturerFactory ManufacturerFactory = new ManufacturerFactory(BaseUrl, Account, Password);
                Bukimedia.PrestaSharp.Entities.manufacturer Manufacturer = ManufacturerFactory.Get(162); //162 = ofba; 5 = acer
                Manufacturer.name = "Iron Maiden";
                Manufacturer.active = 1;
                ManufacturerFactory.Add(Manufacturer);
                ManufacturerFactory.Update(Manufacturer);
                ManufacturerFactory.Delete(Manufacturer);

                ProductFactory ProductShop = new ProductFactory(BaseUrl, Account, Password);
                Bukimedia.PrestaSharp.Entities.product MyProduct = new Bukimedia.PrestaSharp.Entities.product();
                MyProduct = ProductShop.Add(MyProduct);

                BaseUrl = "http://www.mydomain.com/eshop/api/images";
                 Account = "I22R8VQ91YR2KGPHYVPA4I7TU661CPIM";
                Password = "";
                ImageFactory ProductImageShop = new ImageFactory(BaseUrl, Account, Password);
                ProductImageShop.AddProductImage((long)MyProduct.id, "C:\\MyImage.jpg");

 

this is the Class - RestSharpFactory.cs
              

using Bukimedia.PrestaSharp.Deserializers;
using Bukimedia.PrestaSharp.Lib;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using RestSharp.Authenticators;

namespace Bukimedia.PrestaSharp.Factories
{
    public abstract class RestSharpFactory
    {
        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;
        }

        protected T Execute<T>(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("Account", this.Account, ParameterType.UrlSegment); // used on every request
            if (Request.Method == Method.GET)
            {
                client.ClearHandlers();
                client.AddHandler("text/xml", new Bukimedia.PrestaSharp.Deserializers.PrestaSharpDeserializer());
            }
            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.Value + Environment.NewLine + Environment.NewLine;
                }
                var Exception = new PrestaSharpException(RequestParameters + response.Content, response.ErrorMessage, response.StatusCode, response.ErrorException);
                throw Exception;
            }
            return response.Data;
        }

        protected void ExecuteAsync<T>(RestRequest Request) where T : new()
        {
            var client = new RestClient(this.BaseUrl);
            try
            {
                client.ExecuteAsync(Request, response =>
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Console.WriteLine(response.ToString());
                    }
                    else
                    {
                        Console.WriteLine(response.ToString());
                    }
                });
            }
            catch (Exception error)
            {
                error.ToString();
            }
        }

        protected T ExecuteForFilter<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("Account", this.Account, ParameterType.UrlSegment); // used on every request
            client.ClearHandlers();
            client.AddHandler("text/xml", new Bukimedia.PrestaSharp.Deserializers.PrestaSharpDeserializer());
            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.Value + Environment.NewLine + Environment.NewLine;
                }
                var Exception = new PrestaSharpException(RequestParameters + response.Content, response.ErrorMessage, response.StatusCode, response.ErrorException);
                throw Exception;
            }
            return response.Data;
        }

        protected List<long> ExecuteForGetIds<T>(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("Account", this.Account, ParameterType.UrlSegment);
            var response = client.Execute<T>(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)
        {
            var client = new RestClient();
            client.BaseUrl = new Uri(this.BaseUrl);
            client.Authenticator = new HttpBasicAuthenticator(this.Account, this.Password);
            Request.AddParameter("Account", this.Account, ParameterType.UrlSegment);
            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;
            }
            return response.RawBytes;
        }

        protected RestRequest RequestForGet(string Resource, long? Id, string RootElement)
        {
            var request = new RestRequest();
            request.Resource = Resource + "/" + Id;
            request.RootElement = RootElement;
            return request;
        }

        protected RestRequest RequestForAdd(string Resource, List<Entities.PrestaShopEntity> Entities)
        {
            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);
            return request;
        }

        /// <summary>
        /// More information about image management: http://doc.prestashop.com/display/PS15/Chapter+9+-+Image+management
        /// </summary>
        /// <param name="Resource"></param>
        /// <param name="Id"></param>
        /// <param name="ImagePath"></param>
        /// <returns></returns>
        protected RestRequest RequestForAddImage(string Resource, long? Id, string ImagePath)
        {
            if (Id == null)
            {
                throw new ApplicationException("The Id field cannot be null.");
            }
            var request = new RestRequest();
            request.Resource = "/images/" + Resource + "/" + Id;
            request.Method = Method.POST;
            request.RequestFormat = DataFormat.Xml;
            request.AddFile("image", ImagePath);
            return request;
        }
        
        /// <summary>
        /// More information about image management: http://doc.prestashop.com/display/PS15/Chapter+9+-+Image+management
        /// </summary>
        /// <param name="Resource"></param>
        /// <param name="Id"></param>
        /// <param name="Image"></param>
        /// <returns></returns>
        protected RestRequest RequestForAddImage(string Resource, long? Id, byte[] Image)
        {
            if (Id == null)
            {
                throw new ApplicationException("The Id field cannot be null.");
            }
            var request = new RestRequest();
            request.Resource = "/images/" + Resource + "/" + Id;
            request.Method = Method.POST;
            request.RequestFormat = DataFormat.Xml;
            request.AddFile("image", Image, "dummy.png");
            return request;
        }

        /// <summary>
        /// More information about image management: http://doc.prestashop.com/display/PS15/Chapter+9+-+Image+management
        /// </summary>
        /// <param name="Resource"></param>
        /// <param name="Id"></param>
        /// <param name="ImagePath"></param>
        /// <returns></returns>
        protected RestRequest RequestForUpdateImage(string Resource, long Id, string ImagePath)
        {
            var request = new RestRequest();
            request.Resource = "/images/" + Resource + "/" + Id;

            // BUG

            request.Method = Method.PUT;
            request.RequestFormat = DataFormat.Xml;
            request.AddFile("image", ImagePath);
            return request;
        }

        protected RestRequest RequestForUpdate(string Resource, long? Id, Entities.PrestaShopEntity PrestashopEntity)
        {
            if (Id == null)
            {
                throw new ApplicationException("Id is required to update something.");
            }
            var request = new RestRequest();
            request.RootElement = "prestashop";
            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=\"\"");
            return request;
        }

        protected RestRequest RequestForDeleteImage(string Resource, long? ResourceId, long? ImageId)
        {
            if (ResourceId == null)
            {
                throw new ApplicationException("Id is required to delete something.");
            }
            var request = new RestRequest();
            request.RootElement = "prestashop";
            request.Resource = "/images/" + Resource + "/" + ResourceId;
            if (ImageId != null)
            {
                request.Resource += "/" + ImageId;
            }
            request.Method = Method.DELETE;
            request.RequestFormat = DataFormat.Xml;
            return request;
        }

        protected RestRequest RequestForDelete(string Resource, long? Id)
        {
            if (Id == null)
            {
                throw new ApplicationException("Id is required to delete something.");
            }
            var request = new RestRequest();
            request.RootElement = "prestashop";
            request.Resource = Resource + "/" + Id;
            request.Method = Method.DELETE;
            request.RequestFormat = DataFormat.Xml;
            return request;
        }

        /// <summary>
        /// More information about filtering: http://doc.prestashop.com/display/PS14/Chapter+8+-+Advanced+Use
        /// </summary>
        /// <param name="Resource"></param>
        /// <param name="Display"></param>
        /// <param name="Filter"></param>
        /// <param name="Sort"></param>
        /// <param name="Limit"></param>
        /// <param name="RootElement"></param>
        /// <returns></returns>
        protected RestRequest RequestForFilter(string Resource, string Display, Dictionary<string,string> Filter, string Sort, string Limit, string RootElement)
        {
            var request = new RestRequest();
            request.Resource = Resource;
            request.RootElement = RootElement;
            if (Display != null)
            {
                request.AddParameter("display", Display);
            }
            if (Filter != null)
            {
                foreach (string Key in Filter.Keys)
                {
                    request.AddParameter("filter[" + Key + "]", Filter[Key]);
                }
            }
            if (Sort != null)
            {
                request.AddParameter("sort", Sort);
            }
            if (Limit != null)
            {
                request.AddParameter("limit", Limit);
            }
            return request;
        }

        protected RestRequest RequestForAddOrderHistory(string Resource, List<Entities.PrestaShopEntity> Entities)
        {
            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);
            request.AddParameter("sendemail", 1);
            return request;
        }

        public static byte[] ImageToBinary(string imagePath)
        {
            FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, (int)fileStream.Length);
            fileStream.Close();
            return buffer;
        }
    }
}
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...