Last active
June 29, 2018 13:46
-
-
Save Whistler092/a597fd4004ec24b650cc53db530d70e7 to your computer and use it in GitHub Desktop.
ApiCalls Using C# y RestSharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using RestSharp; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Net; | |
namespace NodeClientAdmin | |
{ | |
internal class ApiCalls | |
{ | |
internal void CreateNewFromServer() | |
{ | |
var client = new RestClient(URLService); | |
var request = new RestRequest("/Server/ServerInfo", Method.POST); | |
request.AddHeader("accept", "application/json"); | |
request.AddHeader("content-type", "application/json"); | |
var requestObject = new | |
{ | |
id = keyApp, | |
k = Tools.CriptData(U) | |
}; | |
request.AddParameter("application/json", JsonConvert.SerializeObject(requestObject), ParameterType.RequestBody); | |
IRestResponse response = client.Execute(request); | |
if (!string.IsNullOrEmpty(response.ErrorMessage)) | |
throw new Exception(response.ErrorMessage); | |
if (response.StatusCode.Equals(HttpStatusCode.BadRequest) || response.StatusCode.Equals(HttpStatusCode.InternalServerError)) | |
throw new Exception(response.Content); | |
if (response.StatusCode.Equals(HttpStatusCode.OK)) | |
{ | |
var content = response.Content; | |
currentServer = JsonConvert.DeserializeObject<TLic>(content); | |
} | |
} | |
internal static List<string> LoadServers() | |
{ | |
string URLService = ConfigurationManager.AppSettings["url:Lic"]; | |
string serverhash = ConfigurationManager.AppSettings["serverhash:id"]; | |
Logs.Event("Cargando " + URLService + "/Conf/Servers para el ID " + serverhash, "News.CurrentInstances"); | |
var client = new RestClient(URLService); | |
var request = new RestRequest("Conf/Servers", Method.GET); | |
request.AddHeader("accept", "application/json"); | |
request.AddHeader("content-type", "application/json"); | |
IRestResponse response = client.Execute(request); | |
if (!string.IsNullOrEmpty(response.ErrorMessage)) | |
throw new Exception(response.ErrorMessage); | |
if (response.StatusCode.Equals(HttpStatusCode.BadRequest) || response.StatusCode.Equals(HttpStatusCode.InternalServerError)) | |
throw new Exception(response.Content); | |
if (response.StatusCode.Equals(HttpStatusCode.OK)) | |
{ | |
Logs.Event(HttpStatusCode.OK.ToString(), "News.CurrentInstances"); | |
var lists = JsonConvert.DeserializeObject<Server>(response.Content); | |
return lists; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment