static async Task Run() { var settings = new JsonHCS_Settings() { CookieSupport = true, //I want to support sessions and thus cookies AddDefaultAcceptHeaders = true, //Adds default acceptance headers for json types UserAgent = "MyAwesomeSampleAgent" //Because why not, this is usually ignored anyways }; using (JsonHCS client = new JsonHCS(settings)) { //Use it const string Url = "https://www.w3schools.com/jquery/demo_ajax_json.js"; //change to actual url Console.WriteLine("Get json string:"); var json = await client.GetStringAsync(Url); Console.WriteLine(json); Console.WriteLine("Type: " + json.GetType().FullName); Console.WriteLine(); Console.WriteLine("Get object:"); var obj = await client.GetJsonAsync(Url); Console.WriteLine(obj); Console.WriteLine("Type: " + obj.GetType().FullName); Console.WriteLine(); Console.WriteLine("Get dynamic.firstName:"); dynamic objFN = await client.GetJsonAsync(Url); Console.WriteLine(objFN.firstName); Console.WriteLine("Type: " + objFN.firstName.GetType().FullName); Console.WriteLine(); Console.WriteLine("Get<ExpectedResponce> ToString:"); var ERobj = await client.GetJsonAsync <ExpectedResponce>(Url); Console.WriteLine(ERobj); Console.WriteLine("Type: " + ERobj.GetType().FullName); Console.WriteLine(); Console.WriteLine("Get JObject[\"firstname\"]:"); var JObj = await client.GetJObjectAsync(Url); Console.WriteLine(JObj["firstName"]); Console.WriteLine("Type: " + JObj["firstName"].GetType().FullName); Console.WriteLine(); } }
static async Task Run() { var client = new JsonHCS(new JsonHCS_Settings() { Timeout = 10000, ThrowOnFail = true, CatchErrors = false, AddJsonAcceptHeaders = true }); JsonHCSProxyGenerator pg = new JsonHCSProxyGenerator(client, new SignalRPlugin(), new ActionResultPlugin(), new BasicPlugin()); var proxy = pg.CreateClassProxy <ValuesController>("http://localhost:5000/"); await Task.Delay(5000); for (int o = 0; o < 4; o++) { //Proxy speed comparison: Stopwatch s = new Stopwatch(); s.Start(); for (int i = 0; i < 400; i++) { var all = await client.GetJsonAsync <IEnumerable <string> >("http://localhost:5000/api/values"); //Console.WriteLine(all.Count()); var one = await client.GetJsonAsync("http://localhost:5000/api/values/2"); //Console.WriteLine(one); await client.PostAsync("http://localhost:5000/api/values", "Value"); //Console.WriteLine("Done"); } s.Stop(); Console.WriteLine("Direct:"); Console.WriteLine(s.ElapsedMilliseconds); s.Reset(); s.Start(); for (int i = 0; i < 400; i++) { var allrequest = await proxy.Get(); if (allrequest.IsSuccess) { var all = await allrequest.GetResultAsync(); //Console.WriteLine(all.Count()); } var one = await(await proxy.Get(2)).GetResultAsync(); //Console.WriteLine(one); var post = await proxy.Post("Value"); //Console.WriteLine(post.IsSuccess ? "Done" : "Failed"); } s.Stop(); Console.WriteLine("Proxy:"); Console.WriteLine(s.ElapsedMilliseconds); } //SignalR plugin demo: var api = pg.CreateClassProxy <API>("http://localhost:5000/"); var hub = await api.Connect(); hub.On.Receive += On_Receive; var received = 0; using (hub.On.Message.Subscribe(s => { Console.WriteLine($"{DateTime.Now.TimeOfDay}: Subscription was updated to: {s}"); received++; }, () => Console.WriteLine($"{DateTime.Now.TimeOfDay}: Subscription closed!"))) { Console.WriteLine($"{DateTime.Now.TimeOfDay}: Sending Test 1"); await hub.Send.Broadcast("Test 1"); Console.WriteLine($"{DateTime.Now.TimeOfDay}: Sending Test 2"); await hub.Send.Broadcast("Test 2"); Console.WriteLine($"{DateTime.Now.TimeOfDay}: Sending Test 3"); await hub.Send.Broadcast("Test 3"); while (received < 3) { await Task.Delay(10); } } }