Files
LaDOSE/LaDOSE.Src/LaDOSE.REST/RestService.cs

172 lines
5.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using LaDOSE.DTO;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Serialization.Json;
namespace LaDOSE.REST
{
public class RestService
{
public RestClient Client { get; set; }
public RestService() { }
public void Connect(Uri url, string user, string password)
{
2019-03-09 14:03:25 +01:00
Client = new RestClient(url);
var restRequest = new RestRequest("users/auth", Method.POST);
restRequest.AddJsonBody(new {username = user, password = password});
var response = Client.Post(restRequest);
if (response.IsSuccessful)
{
JsonDeserializer d = new JsonDeserializer();
var applicationUser = d.Deserialize<ApplicationUser>(response);
Client.Authenticator = new JwtAuthenticator($"{applicationUser.Token}");
}
else
{
throw new Exception("unable to contact services");
}
}
2019-03-09 14:03:25 +01:00
#region PostFix
private T Post<T>(string resource,T entity)
{
var json = new RestSharp.Serialization.Json.JsonSerializer();
var jsonD = new RestSharp.Serialization.Json.JsonDeserializer();
var request = new RestRequest();
request.Method = Method.POST;
request.Resource = resource;
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-type", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json; charset=utf-8", json.Serialize(entity), ParameterType.RequestBody);
request.AddObject(entity);
var response = Client.Execute(request);
//var content = response.Content; // raw content as string
try
{
return jsonD.Deserialize<T>(response);
}
catch (Exception)
{
return default(T);
}
2019-03-12 21:41:30 +01:00
}
private R Post<P,R>(string resource, P entity)
{
var json = new RestSharp.Serialization.Json.JsonSerializer();
var jsonD = new RestSharp.Serialization.Json.JsonDeserializer();
var request = new RestRequest();
request.Method = Method.POST;
request.Resource = resource;
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-type", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json; charset=utf-8", json.Serialize(entity), ParameterType.RequestBody);
//request.AddObject(entity);
var response = Client.Execute(request);
//var content = response.Content; // raw content as string
try
{
return jsonD.Deserialize<R>(response);
}
catch (Exception)
{
return default(R);
}
2019-03-09 14:03:25 +01:00
}
#endregion
#region WordPress
2019-03-12 22:23:38 +01:00
public List<WPEventDTO> GetEvents()
{
var restRequest = new RestRequest("/api/wordpress/WPEvent", Method.GET);
2019-03-12 22:23:38 +01:00
var restResponse = Client.Get<List<WPEventDTO>>(restRequest);
return restResponse.Data;
}
public WPEventDTO GetNextEvent()
{
var restRequest = new RestRequest("/api/wordpress/NextEvent", Method.GET);
var restResponse = Client.Get<WPEventDTO>(restRequest);
return restResponse.Data;
}
2019-03-09 14:03:25 +01:00
2019-03-12 21:41:30 +01:00
public string CreateChallonge(int gameId, int eventId)
2019-03-09 14:03:25 +01:00
{
var restRequest = new RestRequest($"/api/wordpress/CreateChallonge/{gameId}/{eventId}", Method.GET);
2019-03-12 21:41:30 +01:00
var restResponse = Client.Get(restRequest);
return restResponse.Content;
}
2019-03-12 22:23:38 +01:00
public string CreateChallonge2(int gameId, int eventId, List<WPUserDTO> optionalPlayers)
2019-03-12 21:41:30 +01:00
{
2019-03-12 22:23:38 +01:00
var restResponse = Post<List<WPUserDTO>,string>($"/api/wordpress/CreateChallonge/{gameId}/{eventId}",optionalPlayers);
2019-03-12 21:41:30 +01:00
return restResponse;
2019-03-09 14:03:25 +01:00
}
public bool RefreshDb()
{
var restRequest = new RestRequest("/api/Wordpress/UpdateDb", Method.GET);
var restResponse = Client.Get<bool>(restRequest);
return restResponse.Data;
}
2019-03-12 22:23:38 +01:00
public List<WPUserDTO> GetUsers(int wpEventId, int gameId)
2019-03-09 14:03:25 +01:00
{
var restRequest = new RestRequest($"/api/Wordpress/GetUsers/{wpEventId}/{gameId}", Method.GET);
2019-03-12 22:23:38 +01:00
var restResponse = Client.Get<List<WPUserDTO>>(restRequest);
2019-03-09 14:03:25 +01:00
return restResponse.Data;
}
2019-03-12 22:23:38 +01:00
public List<WPUserDTO> GetUsersOptions(int wpEventId, int gameId)
2019-03-09 14:03:25 +01:00
{
var restRequest = new RestRequest($"/api/Wordpress/GetUsersOptions/{wpEventId}/{gameId}", Method.GET);
2019-03-12 22:23:38 +01:00
var restResponse = Client.Get<List<WPUserDTO>>(restRequest);
2019-03-09 14:03:25 +01:00
return restResponse.Data;
}
#endregion
#region Games
2019-03-12 22:23:38 +01:00
public List<GameDTO> GetGames()
{
var restRequest = new RestRequest("/api/Game", Method.GET);
2019-03-12 22:23:38 +01:00
var restResponse = Client.Get<List<GameDTO>>(restRequest);
return restResponse.Data;
}
2019-03-09 14:03:25 +01:00
2019-03-14 01:31:56 +01:00
public GameDTO UpdateGame(GameDTO game)
2019-03-09 14:03:25 +01:00
{
2019-03-14 01:31:56 +01:00
return Post("Api/Game", game);
}
public bool DeleteGame(int gameId)
{
var restRequest = new RestRequest($"/api/Game/{gameId}", Method.DELETE);
var restResponse = Client.Execute(restRequest);
return restResponse.IsSuccessful;
2019-03-09 14:03:25 +01:00
}
#endregion
#region Events
#endregion
}
}