Activity: Improving Your Function

In this activity, we will add a JSON payload to the request and write code to parse that message into a C# object.

Prerequisites

You will require a function created from the HTTP trigger with the parameters template.

Scenario

You are creating the start of a personal finance application that allows users to add their own transactions, integrate with other applications and perhaps allow their credit card to directly log transactions. It will be able to scale elastically to any number of users, saving us money when we don't have any users.

Aim

Parse a JSON payload into a C# object, starting your RESTful API.

Steps for Completion

  1. Change the Route to transactions.
  2. Remove the get verb. Remove the String parameter called name:
  1. Add the Newtonsoft.json package, if it isn't already present. You can do this by right-clicking the Solution | Manage NuGet packages | Browse | Newtonsoft.Json.

Version 10.0.3 is used for this book.
  1. Right-click on the project and add a folder called Models, then add a C# class called Transaction. Add two properties to this class: a DateTime property called ExecutionTime, and a Decimal property called Amount:
Refer to the complete code placed at Code/Serverless-Architectures-with-Azure/Lesson 1/BeginningAzureServerlessArchitecture/Models/Transaction.cs.

Go to https://goo.gl/H6cu2M to access the code.

  1. Use JsonConvert.DeserializeObject<Transaction>(message).Result() to deserialize the HttpRequestMessage into an instantiation of this class. To do that, you need to import the Models namespace and Newtonsoft.Json. This will parse the JSON payload and use the Amount property to file the corresponding property on the Transaction object:
Refer to the complete code placed at Code/Serverless-Architectures-with-Azure/Lesson 1/BeginningAzureServerlessArchitecture/PostTransactionsExC.cs.

Go to https://goo.gl/ttyvpT to access the code.

  1. Change the return message to use a property of the new Transaction object, for example, You entered a transaction of £47.32! Go to Postman and open the Body tab and select raw.
  1. Enter in the following JSON object:
{
Amount: 47.32,
ExecutionTime: "2018-01-01T09:00:00Z"
}
  1. Run locally to test. Make sure you change the endpoint to /transactions in Postman.

Outcome

You have learned how to access the HttpRequestMessage, and you will have a function that can read a JSON message and turn it into a C# object.

During this subtopic, you debugged an Azure Function. Visual Studio only allows this through downloading azure-functions-core-tools. Unfortunately, it doesn't make it available on the general command line—only through command windows started in Visual Studio. If you want to use it independently, then you have to download it using npm. If you need to download azure-functions-core-tools separately, you can use npm to get it—npm install -g azure-functions-core-tools for version 1 (fully supported) and npm install -g azure-functions-core-tools@core for version 2 (beta). We will go into the differences in versions later in this chapter. You can then use the debug setup to set Visual Studio to call an external program with the command func host start when you click on the Debug button.

This package is a lot more than just a debug environment, however; it actually has a CLI for everything you could possibly need in Azure Function development. Open up a command window (in Visual Studio, if you haven't downloaded it independently) and type func help; you should see a full list of everything the CLI can do. Notable commands are func host start, which starts the local debug environment, and func azure {functionappname} fetch-app-settings, which lets you download the app settings of a function deployed to Azure so that you can test integration locally, as well. These need to be run in the same folder as the host.json file.

Developing in Visual Studio is much easier for Azure Functions, especially if it is your usual IDE, but it's perfectly possible to use a simple text editor and the CLI to develop. If you are developing for version 2 on a Mac or Linux machine, this setup is basically the only way to develop at the moment, although Visual Studio Code has a good Azure Functions extension.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.236.70