Page 50 - MSDN Magazine, December 15, 2017
P. 50
matching object and passing along all other objects. If you want to remove an object entirely, you need to overwrite it with a blank entry. Replace the Mock API with a Real API Now that the partner team has been unblocked, it’s time to add a real API into the solu- tion. To start off, the real API will be the httpbin.org/headers endpoint. This testing endpoint returns a dictionary of HTTP headers, so it’ll be perfect to replace the /mobile-user POST end- point. However, the httpbin.org/headers endpoint only accepts GET requests, so use a proxy request override to send the back-end
service the correct request.
Replace the MobileUserPOST proxy with the following rule:
"MobileUserPOST": { "matchCondition": {
Paste the following proxy into the advanced editor, to create a rule that proxies /mobile-user/name to the back-end function, to ultimately return “hello name,” like so:
"MobileUserHello": { "matchCondition": {
"route": "/mobile-user/{user}" },
"backendUri": "%backendfunction%", "requestOverrides": {
"backend.request.querystring.name": "{user}"
Tip: Use application settings to store secrets outside of your proxies, so that the proxies.json can be checked into source control, shared widely or simply updated dynamically. Use %appsettingname% to reference an application setting.
When it’s all said and done, your finished API topology will look like Figure 5.
Advanced API Topologies with APIM
For many developers, Azure Functions Proxies just scratches the surface of its API integration needs; instead, it makes sense to step up to API Management. When compositing multiple APIs together, Azure API Management (APIM) provides a much more expansive set of transformation rules, as well as policies for rate limiting and security. APIM also allows APIs to be monetized or restricted using a developer onboarding portal. In large APIs the topology is usually a single APIM endpoint resolving to multiple Azure Functions Proxies endpoints, which then have functions and Software-as-a-Service services under them.
Adding Extra Functionality
Azure Functions Proxies enables you to add a whole suite of func- tionality to any existing API. You can expose an OpenAPI definition, layer on authentication/authorization to provide a secured API endpoint, or measure traffic on an API using App Insights.
Host a Static Page Using Azure Functions Proxies: Azure Functions Proxies isn’t just useful for API compositing; it can also be used for general HTTP redirection. You can host static HTML on your function app by either proxying the back-end URL of a service hosting HTML, or creating a proxy that returns HTTP 302 to redirect the user to a Web page. This combo can be used to host single-page applications, and is especially useful if the API endpoints
used by the single-page application is in the same proxy.
Monitor a Legacy API with Application (App) Insights: Azure Functions Proxies can be used to add lots of Platform-as-a-Service-like functionality to an existing API, such as monitoring with App Insights. App Insights is a strong monitoring suite that can calculate complex metrics on API traffic, send alerts on certain conditions, and provide a real-time analytics stream.
To enable App Insights monitoring, simply create a new function app and toggle App Insights to on at the creation blade. Then add a proxy, as simple as one route with a back-end URL. Send some requests to your proxy to have data to measure; then navigate to the App Insights resource with the same name as your function app in the portal. You’ll be able to see the latency, request count, and
Functions
"route": "/mobile-user", "methods": [
"POST"
"backendUri": "https://httpbin.org/headers", "requestOverrides": {
"backend.request.method": "get" }
Now resend the Post request to the /mobile-user endpoint. You should receive a complete list of headers, along with the name header you sent. Your response should somewhat match Figure 4.
Composite an Additional API Part of the strength of Azure Functions Proxies is the ability to combine multiple separate API endpoints into one outward-facing API endpoint. For the next example, we’ll add a function-hosted API into the /mobile-user API.
Start by creating a new function app and creating a new HTTP triggered function inside of the new app. We’ll utilize the built-in HTTP sample code that takes in a name as a query parameter and returns “hello name.”
When you proxy the function, you’re not going to want to hard- code the back-end function URL and API key into the proxy rule, so we’ll use an application setting to store that value, and retrieve that value in the proxy.
In the function editor of the back-end function, copy the func- tion URL—API key and all—to the clipboard for later. Now navigate back to the function hosting the proxies, and add an application set- ting with the function URL you just copied. The app settings page is under functionname | Platform features | Application settings | + Add new setting. Call the new setting “backendfunction” and hit save.
}
] },
Function App A
Httpbin.org/Post
} }
/mobile-user GET
/mobile-user POST
/mobile-user/{name}
Key:
HTTP Endpoints API Proxy Endpoints
Figure 5 Final Proxy API Topology 46 msdn magazine
/api/httptrigger CSharp1
Function App B
Function2