Page History: Web API
    Compare Page Revisions
 
    
    
    
    
    
    
Page Revision: 22.04.2015. 10:22:AM
Korištenje eVisitor Web API funkcionalnosti
Sučelje je izvedeno kao REST service (root URI: http://eVisitor/Rest). Kroz eVisitor Web API sučelje moguće je izvršiti sve operacije koje su dostupne kroz web sučelje same eVisitor aplikacije, pri čemu  vrijede ista sigurnosna i poslovna pravila. Primjeri korištenja biti će prikazani u C# programskom jeziku koristeći RestSharp REST klijent.
Prijava (login)
Da bi pristup API-ju bio dozvoljen potrebno se prijaviti (login) u sustav koristeći Authentication service API (URI: http://eVisitor/Resources/AspNetFormsAuth/Authentication/), koji implementira slijedeće metode:
Login
- Interface: (string UserName, string Password, bool PersistCookie) -> bool
- Primjer request data: {"UserName":"myusername","Password":"mypassword","PersistCookie":false}
- Odgovor je true pri uspješnom loginu, inače false. Pri uspješnom loginu odgvor servera sadrži i standardni authentication cookie koji se mora slati prilikom svakog poziva API REST servisa.
 
Logout
- Nema nikakvih parametara, potrebno je proslijediti standardni authentication cookie. Odgovor je prazan. 
 
Primjer prijave na sustav
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using RestSharp;
namespace Htz.Rhetos.Test
{
    [TestClass]
    public class WebApiTest
    {
        public static string AuthenticationRestServiceUrl = "http://localhost/HtzRhetosServer/Resources/AspNetFormsAuth/Authentication/";
        public static string RestServiceUrl = "http://localhost/HtzRhetosServer/Rest/";
        [TestMethod]
        public void CanAuthenticate()
        {
            var authCookie = Login();
            Assert.IsNotNull(authCookie);
            Logout(authCookie);
        }
        public static RestResponseCookie Login()
        {
            var restRequest = new RestRequest()
            {
                Method = Method.POST,
                Resource = "Login",
                RequestFormat = DataFormat.Json,
            };
            restRequest.AddBody(new { UserName = "123", Password = "123", PersistCookie = false });
            var restClient = new RestClient(AuthenticationRestServiceUrl);
            var response = restClient.Execute(restRequest);
            if (response.Content == "true")
                return response.Cookies.FirstOrDefault(x => x.Name == ".ASPXAUTH");
            return null;
        }
        public static void Logout(RestResponseCookie authCookie)
        {
            var restRequest = new RestRequest()
            {
                Method = Method.POST,
                Resource = "Logout",
                RequestFormat = DataFormat.Json,
            };
            restRequest.AddCookie(authCookie.Name, authCookie.Value);
            var restClient = new RestClient(AuthenticationRestServiceUrl);
            var response = restClient.Execute(restRequest);            
        }
    }
}
Dostupne metode