{"id":5299,"date":"2017-09-06T19:52:07","date_gmt":"2017-09-06T19:52:07","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5299"},"modified":"2017-09-06T19:52:07","modified_gmt":"2017-09-06T19:52:07","slug":"trying-out-refit-a-rest-client-library","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/trying-out-refit-a-rest-client-library\/","title":{"rendered":"Trying out Refit (a REST client library)"},"content":{"rendered":"<p>REST is cool in that it&#8217;s lightweight and simple to use but lacks a lot of the tooling etc. that SOAP\/XML has.<\/p>\n<p>In .NET we have libraries such as ServiceStack (and others) to enable generation of REST services to be fairly painless, there are also a few client frameworks out there, but for this post I&#8217;m looking at Refit which (as the <a href=\"https:\/\/paulcbetts.github.io\/refit\/\" target=\"_blank\">documentation<\/a> states) is automatically type-safe and available in .NET standard 1.4 which also includes Xamarin, UWP and desktop. So pretty universal in it&#8217;s availability.<\/p>\n<p><strong>Show me the code!<\/strong><\/p>\n<p>Enough chat, let&#8217;s get into the code. I&#8217;m going to use ServiceStack to generate a simply REST service, the default one is the &#8220;Hello&#8221; service, which in code looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;Route(&quot;\/hello\/{Name}&quot;)]\r\npublic class Hello : IReturn&lt;HelloResponse&gt;\r\n{\r\n   public string Name { get; set; }\r\n}\r\n\r\npublic class HelloResponse\r\n{\r\n   public string Result { get; set; }\r\n}\r\n<\/pre>\n<p>and this will be called using http:\/\/127.0.0.1:8088\/hello\/World which in turn returns (in JSON format)<\/p>\n<p>{&quot;Result&quot;:&quot;Hello, World!&quot;}<\/p>\n<p>All pretty simple.<\/p>\n<p>One thing we will do, is switch ServiceStack to use JSON be default, so in the MyService generated code we&#8217;ll add an AddHeader attribute, so it will look like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyServices : Service\r\n{\r\n   &#x5B;AddHeader(ContentType = MimeTypes.Json)]\r\n   public object Any(Hello request)\r\n   {\r\n      return new HelloResponse { Result = &quot;Hello, {0}!&quot;.Fmt(request.Name) };\r\n   }\r\n}\r\n<\/pre>\n<p><strong>Creating our client<\/strong><\/p>\n<p>Fire up Visual Studio and create a console application (I&#8217;ve targeted .NET 4.6), then add Refit via NuGet.<\/p>\n<p>As we already have the HelloResponse class from our ServiceStack sample application, we can just copy that into our Refit application, or visit https:\/\/quicktype.io\/ and paste {&#8220;Result&#8221;:&#8221;Hello, World!&#8221;} into the JSON edit box on the left of the site and it&#8217;ll create the class for us. So using either method we need to add the JsonProperty so it now looks like this in our Refit app.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class HelloResponse\r\n{\r\n   &#x5B;JsonProperty(&quot;Result&quot;)]\r\n   public string Result { get; set; }\r\n}\r\n<\/pre>\n<p>Next up we need create an interface that calls our REST service. We know, having written\/generated the code for the service that there&#8217;s a Hello class that defines the route and response, but we won&#8217;t be copying this, instead Refit likes to use methods returning a Task (which makes sense being a web service call) so create (in your Refit app.) the following <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic interface IHelloService\r\n{\r\n   &#x5B;Get(&quot;\/hello\/{name}&quot;)]\r\n   Task&lt;HelloResponse&gt; SayHello(string name);\r\n}\r\n<\/pre>\n<p>Note we create an interface and that the route is within the Get attribute points to our Hello endpoint. Next we created a method to be called to contact our endpoint at the aforementioned route. This is the main area where we actually have to do any coding for this example to work. <\/p>\n<p>Let&#8217;s now create the client code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar svc = RestService.For&lt;IHelloService&gt;(&quot;http:\/\/127.0.0.1:8088&quot;);\r\nvar response = svc.SayHello(&quot;World&quot;);\r\n<\/pre>\n<p>and that&#8217;s it &#8211; obviously you&#8217;ll need to await or Result etc. to get the actually HelloResponse object, but that&#8217;s all there is to this (admittedly very simple example).<\/p>\n<p>As you can see the RestService.For method creates an instance of an IHelloService for us acting as a transparent proxy through to the service. <\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/paulcbetts\/refit\" target=\"_blank\">The automatic type-safe REST library for Xamarin and .NET<\/a><br \/>\n<a href=\"https:\/\/www.hanselman.com\/blog\/ExploringRefitAnAutomaticTypesafeRESTLibraryForNETStandard.aspx\" target=\"_blank\">Exploring refit, an automatic type-safe REST library for .NET Standard<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>REST is cool in that it&#8217;s lightweight and simple to use but lacks a lot of the tooling etc. that SOAP\/XML has. In .NET we have libraries such as ServiceStack (and others) to enable generation of REST services to be fairly painless, there are also a few client frameworks out there, but for this post [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[181],"tags":[],"class_list":["post-5299","post","type-post","status-publish","format-standard","hentry","category-refit"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5299"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5299\/revisions"}],"predecessor-version":[{"id":5351,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5299\/revisions\/5351"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}