Ejemplo n.º 1
0
        public void GetString_Valid()
        {
            var res = _rext.GetJSON <dynamic>(_baseUrl + "users/2").Result;

            Assert.IsTrue(res.IsSuccess);
        }
Ejemplo n.º 2
0
        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();
        }