// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.Threading.Tasks; using Refit; namespace TestNew { public class T1 { public int F1 { get; set; } public string F2 { get; set; } public T1() : this(0, "") { } public T1(int f1, string f2) { this.F1 = f1; this.F2 = f2; } public override string ToString() { return string.Format("{{{0},{1}}}", F1, F2); } } [Headers("Content-Type: application/json", "Accept: application/json")] public interface IT1Service { [Get("/t1/{f1}")] Task GetOne(int f1); [Get("/t1")] Task> GetAll(); [Get("/t1")] Task> GetSome(int start, int finish); [Post("/t1")] Task Post([Body] T1 o); [Put("/t1")] Task Put([Body] T1 o); [Delete("/t1/{f1}")] Task Delete(int f1); } public class ClientRefit { private static void TestGetOne(string urlprefix, int f1) { IT1Service api = RestService.For(urlprefix); Console.WriteLine(api.GetOne(f1).Result); } private static void TestGetAll(string urlprefix) { IT1Service api = RestService.For(urlprefix); foreach(T1 o in api.GetAll().Result) Console.Write(o); Console.WriteLine(); } private static void TestGetSome(string urlprefix, int start, int finish) { IT1Service api = RestService.For(urlprefix); foreach (T1 o in api.GetSome(start, finish).Result) Console.Write(o); Console.WriteLine(); } private static void TestPost(string urlprefix, T1 o) { IT1Service api = RestService.For(urlprefix); Console.WriteLine(api.Post(o).Result); } private static void TestPut(string urlprefix, T1 o) { IT1Service api = RestService.For(urlprefix); api.Put(o).Wait(); } private static void TestDelete(string urlprefix, int f1) { IT1Service api = RestService.For(urlprefix); api.Delete(f1).Wait(); } private static void Test(string urlprefix) { Console.WriteLine("**** " + urlprefix + " (application/json) ****"); TestGetOne(urlprefix, 123); TestGetAll(urlprefix); TestGetSome(urlprefix, 10, 12); TestPost(urlprefix, new T1(123, "ABC")); TestPut(urlprefix, new T1(123, "ABC")); TestDelete(urlprefix, 123); } public static void Main(string[] args) { Test("http://localhost:81/testapi_slim.php"); } } }