using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace BPSConsoleTest { public class RequestInfo { public string ApiToCall { get; set; } public string Method {get; set; } public string Body { get; set; } public CookieContainer CookieContainer { get; set; } } public class LoginResponse { public string apiKey { get; set; } public string sessionName { get; set; } public string sessionId { get; set; } public string userAccountUrl { get; set; } } public class Session { public string ServerAddress { get; set; } private LoginResponse loginInfo; private CookieContainer cookieContainer; public Session(String server = "10.215.118.38") { this.ServerAddress = server; } public void Login() { RequestInfo requestInfo = new RequestInfo { Method = "POST", ApiToCall = "v1/auth/session", Body = "{\"username\":\"admin\", \"password\":\"admin\"}" }; ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; Request requestLogin = new Request(requestInfo); string response = requestLogin.ExecuteRequest(ServerAddress); cookieContainer = requestInfo.CookieContainer; loginInfo = JsonConvert.DeserializeObject<LoginResponse>(response); } public string ExecuteApi(RequestInfo requestInfo) { requestInfo.CookieContainer = this.cookieContainer; Request request = new Request(requestInfo); string response = request.ExecuteRequest(ServerAddress); return response; } public LoginResponse getLoginInfo() { return loginInfo; } } class Request { RequestInfo requestInfo; internal Request(RequestInfo requestInfo) { this.requestInfo = requestInfo; } internal string ExecuteRequest(string serverAddress) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("https://{0}/api/{1}", serverAddress, requestInfo.ApiToCall)); request.UserAgent = ".NET Framework Example Client"; request.ContentType = "application/json"; request.Accept = "application/json;q=0.9"; this.requestInfo.CookieContainer = this.requestInfo.CookieContainer ?? new CookieContainer(); request.CookieContainer = this.requestInfo.CookieContainer; request.Method = requestInfo.Method; byte[] originalStream = Encoding.UTF8.GetBytes(requestInfo.Body); request.ContentLength = originalStream.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(originalStream, 0, originalStream.Length); dataStream.Close(); WebResponse response = request.GetResponse(); // Display the status. Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. Console.WriteLine(responseFromServer); // Clean up the streams. reader.Close(); dataStream.Close(); Console.WriteLine(responseFromServer); //Console.ReadLine(); return responseFromServer; } } class Program { static void Main(string[] args) { Session newSession = new Session("10.215.118.38"); newSession.Login(); //reserve ports RequestInfo request = new RequestInfo { Method = "POST", ApiToCall = "v1/bps/ports/operations/reserve", Body = "{\"slot\":\"3\", \"portList\":[0,1], \"group\":\"1\", \"force\":\"true\"}" }; newSession.ExecuteApi(request); //start a test request = new RequestInfo { Method = "POST", ApiToCall = "v1/bps/tests/operations/start", Body = "{\"modelname\":\"AppSim\", \"group\":\"1\", \"neighborhood\":\"BreakingPoint Routing\"}" }; newSession.ExecuteApi(request); Console.WriteLine("Press ENTER to exit!"); Console.ReadLine(); } } }
marți, 8 martie 2016
BPS 8.0: REST API C# Console Test Program
Abonați-vă la:
Postare comentarii (Atom)
Niciun comentariu:
Trimiteți un comentariu