static void Main3(string[] args) { Console.WriteLine("Checking Rext performance to GET postman-echo.com/get?foo1=bar1&foo2=bar2 \n============================"); _rext = new RextHttpClient(); restart: var s = new Stopwatch(); s.Start(); for (int i = 0; i < 10; i++) { var r = _rext.GetString(new RextOptions { Url = "https://postman-echo.com/get?foo1=bar1&foo2=bar2", ContentType = "application/json" }).Result; Console.WriteLine($"{i + 1}. {r.StatusCode} {_rext.Stopwatch.ElapsedMilliseconds.ToString("N4")}ms"); } s.Stop(); Console.WriteLine($"Total time: {s.ElapsedMilliseconds.ToString("N4")}ms"); if (Console.ReadLine() == "R") { goto restart; } Console.ReadKey(); }
/// <summary> /// Use Bearer Authentication by supplying just the token /// </summary> /// <param name="client"></param> /// <param name="token">API bearer token</param> /// <returns></returns> public static IRextHttpClient UseBearerAuthentication(this IRextHttpClient client, string token) { if (!string.IsNullOrEmpty(token)) { client.Headers.TryAdd("Authorization", $"Bearer {token}"); } return(client); }
/// <summary> /// Use Basic Authentication by supplying just the username and password /// </summary> /// <param name="client"></param> /// <param name="username">API username</param> /// <param name="password">API password</param> /// <returns></returns> public static IRextHttpClient UseBasicAuthentication(this IRextHttpClient client, string username, string password) { // encode credentials if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)) { string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")); client.Headers.TryAdd("Authorization", $"Basic {credentials}"); } return(client); }
/// <summary> /// Add a single header item with key and value /// </summary> /// <param name="client"></param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public static IRextHttpClient AddHeader(this IRextHttpClient client, string key, string value) { if (client.Headers.ContainsKey(key)) { client.Headers.Remove(key); } client.Headers.TryAdd(key, value); return(client); }
/// <summary> /// Add a Dictionary of headers /// </summary> /// <param name="client"></param> /// <param name="headers"></param> /// <returns></returns> public static IRextHttpClient AddHeader(this IRextHttpClient client, IDictionary <string, string> headers) { foreach (var i in headers) { if (client.Headers.ContainsKey(i.Key)) { client.Headers.Remove(i.Key); } client.Headers.TryAdd(i.Key, i.Value); } return(client); }
public void Init() { _rext = new RextHttpClient(); }
static void Main2(string[] args) { try { RextHttpClient.Setup(opt => { opt.HttpConfiguration = new RextHttpCongifuration { //BaseUrl = "localhost:44316/api/home", //ProxyAddress = "http://127.0.0.1:80", ThrowExceptionOnDeserializationFailure = false, ThrowExceptionIfNotSuccessResponse = false, RelaxSslCertValidation = true //Timeout = 60 }; opt.SuppressRextExceptions = false; opt.BeforeCall = delegate() { Console.WriteLine("---About to initiate http task"); }; opt.AfterCall = delegate() { Console.WriteLine("---End of http task"); }; opt.OnError = delegate() { Console.WriteLine("---Error occured"); }; opt.OnStatusCode = (int code) => ErrorCodeHandler(); opt.StatusCodesToHandle = new int[] { 401, 500 }; }); _RunTest: Console.WriteLine("Hit 'R' to run test"); if (Console.ReadLine().ToUpper() == "R") { Console.Clear(); _rext = new RextHttpClient(); Console.WriteLine("Hello Rext!"); string url1 = "https://fudhubapi.dynamicbra.in/api/product/getall"; string url2 = "http://httpstat.us/500"; var header = new Dictionary <string, string> { { "client_secret", "xxx12345" }, { "role", "admin" } }; var header_single = new { header_single = "header obj from GetJSON" }; //var rsp = _rext.UseBasicAuthentication("user", "pwd") // //.UseBearerAuthentication("877834780948087yihfsjhfjs==") // .AddHeader(new Dictionary<string, string> // { // { "item_1", "one" }, // { "item_2", "one" } // }) // .AddHeader("single_obj", "o_b_j") // .GetJSON<string>(url, // new // { // name = "Jordan Pickford" // }, // header_single).GetAwaiter().GetResult(); var rsp = _rext.GetJSON <dynamic>(url1).GetAwaiter().GetResult(); //var rsp = _rext.GetString("https://localhost:44316/api/home/getstring").GetAwaiter().GetResult(); Console.WriteLine($"{rsp.StatusCode} - {rsp.Message} - Duration: {_rext.Stopwatch.ElapsedMilliseconds}ms"); Console.WriteLine(rsp.Data); //var rsp = _rext.GetJSON<object>("https://localhost:44316/api/home/status?code=401", null, new { api_key = "12345" }).GetAwaiter().GetResult(); //var rsp = _rext.GetJSON<object>("getperson?id=1001", new { name = "joe" }, new { api_key = "12345" }).GetAwaiter().GetResult(); //Console.WriteLine($"{rsp.StatusCode} - {rsp.Message} - Duration: {_rext.Stopwatch?.ElapsedMilliseconds}ms"); //Console.WriteLine(rsp?.Data); var p = new Person { Name = "Vicky Kay", Location = "London, UK", Status = false, NextOkKin = new Person { Name = "William", Location = "Lisbon", Status = true } }; //var rsp = _rext.PostXML<Person>(new RextOptions //{ // Url = "https://localhost:44316/api/home/createperson", // Payload = p //}).GetAwaiter().GetResult(); //Console.WriteLine($"{rsp.StatusCode} - {rsp.Message} - Duration: {_rext.Stopwatch.ElapsedMilliseconds}ms"); //Console.WriteLine($"Name: {rsp.Data.Name} - Location: {rsp.Data.Location}"); //var rsp = _rext.GetXML<ArrayOfPerson>("https://localhost:44316/api/home/getpeoplelist") // .GetAwaiter().GetResult(); //Console.WriteLine($"{rsp.StatusCode} - {rsp.Message} - Duration: {_rext.Stopwatch.ElapsedMilliseconds}ms"); //foreach (var i in rsp.Data.Person) //{ // Console.WriteLine($"Name: {i.Name}, Location: {i.Location}"); //} //var rsp = _rext.PostForm<Person>("https://localhost:44316/api/home/createpersonform", p) // .GetAwaiter().GetResult(); //Console.WriteLine($"{rsp.StatusCode} - {rsp.Message} - Duration: {_rext.Stopwatch.ElapsedMilliseconds}ms"); //Console.WriteLine($"Name: {rsp.Data.Name} - Location: {rsp.Data.Location}"); //var headers = new Dictionary<string, string> //{ // { "header1", "value 1" }, // { "header2", "value 2" } //}; //var rsp = _rext.AddHeader(headers) // .AddHeader("header3", "value 3") // .UseBearerAuthentication("ueyuywyt.iduizcg0e.kiuwnk==") // .UseBasicAuthentication("api_username", "api_passkey") // .PostJSON<@Person>(new RextOptions // { // Url = "http://myapp.com/api/employee/getemployee", // ContentType = "application/xml", // Header = new { header4 = "value4" } // }); Console.WriteLine("--------------"); goto _RunTest; } else { Console.WriteLine("--------------"); Console.ReadKey(); } } catch (Exception ex) { Console.WriteLine($"Exception: {ex.Message}"); } Console.ReadKey(); }