<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Bec Lyons</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/becstech/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/becstech/feed.xml</link>
    <description>Cloud Solution Architect</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:45:46 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Turn any API into a RESTful API with Azure API Management</title>
      <link>https://docs.microsoft.com/archive/blogs/becstech/turn-any-api-into-a-restful-api-with-azure-api-management</link>
      <pubDate>Fri, 02 Mar 2018 05:58:27 GMT</pubDate>
      <dc:creator><![CDATA[Bec Lyons]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/becstech/?p=55</guid>
      <description><![CDATA[With Azure API Management (API M) you can turn any API into a RESTful API. To demonstrate, this post...]]></description>
      <content:encoded><![CDATA[<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">With <a href="https://docs.microsoft.com/en-us/azure/api-management/api-management-key-concepts">Azure API Management</a> (API M) you can turn any API into a RESTful API. To demonstrate, this post will detail the steps to transform a simple web service into a RESTful API without any code change or refactoring.</p>
&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">To start I have a simple ASP.NET MVC web service that exposes one endpoint "api/action" with two possible actions "GetBook" and "CreateBook" passed in as JSON objects - nothing RESTful here. Rather than refactor my code into something RESTful I am going to simply use API M to wrap a RESTful interface around my existing API backend, which can be left as is with no code change necessary. This pattern works great for modernising existing APIs that may be too complex and time consuming to change.</p>
&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">To find the code for my simple web service view my GitHub <a href="https://github.com/Bec-Lyons/BookAPI">here</a>. Feel free to follow along with these steps, to do so you will need a:</p>

<ul type="disc" style="margin-left: .375in;direction: ltr;margin-top: 0in;margin-bottom: 0in">
 	<li style="margin-top: 0;margin-bottom: 0;vertical-align: middle"><span style="font-family: Calibri;font-size: 11.0pt">Azure Subscription </span></li>
 	<li style="margin-top: 0;margin-bottom: 0;vertical-align: middle"><span style="font-family: Calibri;font-size: 11.0pt">Azure API M instance </span></li>
 	<li style="margin-top: 0;margin-bottom: 0;vertical-align: middle"><span style="font-family: Calibri;font-size: 11.0pt">Hosting platform for your API (I am using Azure Web Apps for this as I can simply deploy from my visual studio environment)</span></li>
</ul>
&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">Here is what a call to my existing web service looks like on postman. My endpoint URL is '/api/action' and I am passing in the action I want to execute in a HTTP POST as JSON. My backend will then parse that, get the result and simply send a confirmation of the action triggered as seen here:</p>
<img src="https://msdnshared.blob.core.windows.net/media/2018/03/postman-1024x661.png" alt="" width="530" height="342" class="alignnone wp-image-95" />
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">The first step in creating a RESTful API is to define the contract of the actions, responses and requests in the OpenAPI specification. I have included mine on GitHub <a href="https://github.com/Bec-Lyons/BookAPI/blob/master/documents/openapispec.json">here</a>.</p>
&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">For details on how to write an OpenAPI Spec I followed <a href="https://apihandyman.io/writing-openapi-swagger-specification-tutorial-part-1-introduction/">API Handymans series</a>. It does a great job in going through the process step by step.<a href="https://apihandyman.io/writing-openapi-swagger-specification-tutorial-part-1-introduction/"></a></p>
&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">Once this specification has been defined I can upload this to API M and start playing around with it.</p>
&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">To upload your OpenAPI Spec to API M click on the API tab in the main blade of your API M instance (please note that the functionality of the publisher portal has been moved to the Azure portal).</p>
&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">Click the OpenAPI specification option to upload your spec as a file or URL.</p>
<img src="https://msdnshared.blob.core.windows.net/media/2018/03/API-blade-1024x416.png" alt="" width="590" height="240" class="alignnone wp-image-135" />
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">Here are the options that I filled in:</p>
<img src="https://msdnshared.blob.core.windows.net/media/2018/03/Untitled-1024x771.png" alt="" width="553" height="416" class="alignnone wp-image-125" />
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">Once uploaded we can start to tweak our API in the portal. To allow our RESTful calls to hit our backend service in the right format we need to do some URL translation on the fly. To allow my backend to understand the incoming requests I need to change the HTTP methods from GET to POST, changing the URL from /books to /api/action and add in a JSON object in the body message.</p>
&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">To implement these changes I have added some policies to my inbound requests as seen here.</p>
<img src="https://msdnshared.blob.core.windows.net/media/2018/03/edit-inbound-1024x460.png" alt="" width="614" height="276" class="alignnone wp-image-65" />

&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">To add and configure these select edit.</p>
<img src="https://msdnshared.blob.core.windows.net/media/2018/03/form-inbound-1024x626.png" alt="" width="626" height="383" class="alignnone wp-image-75" />
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">Here I have changed the HTTP method from GET to POST, rewritten the incoming URL from /books to /api/action and added in a body as JSON with an input parameter of "GetBook". Now this request is in the right format to be executed by my backend service. This would be a similar process for the "CreateBook" operation.</p>
&nbsp;
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">To test this is working as expected I can go ahead and test the API from API M itself under the Test tab. Select the GET books operation and try it out.</p>
<img src="https://msdnshared.blob.core.windows.net/media/2018/03/new-URL-821x1024.png" alt="" width="427" height="533" class="alignnone wp-image-85" />
<p style="margin: 0in;font-family: Calibri;font-size: 11.0pt">As you can see we have successfully turned an old non-REST API into RESTful with no code change, just simple tweaks in Azure API M.</p>]]></content:encoded>
    </item>
  </channel>
</rss>