Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning Azure Functions

You're reading from  Learning Azure Functions

Product type Book
Published in Sep 2017
Publisher Packt
ISBN-13 9781787122932
Pages 240 pages
Edition 1st Edition
Languages
Concepts

Chapter 6. The Real World - Functions to Build Consumable APIs

"Elegance is not a dispensable luxury but a factor that decides between success and failure"

– Edsger Dijkstra

In this chapter, we will show how the previous concepts of triggers and bindings work together to bring some real-world functionality.

In the previous chapter, we have learned about triggers and bindings and how they work. In this chapter, we will do some exercises, which will use all the previous concepts, and create real-life examples:

  • Outlining a project
  • Architecting the solution
  • Building a project
  • Using storage
  • Testing

Outlining a project


Let's start with the project's outline. We are going to create a User Detail Portal.

This portal will be used by the client for their users, who can enter their details, which will be stored in the database.

Once the data is stored in the database, the client can use that data anytime in the future.

The detailed process is where we will create one form for the user, which will take the ID, name, age, and address as parameters and save all the data in the database.

To create this, we first need to create APIs using the HTTP trigger of the Azure Function. After that, we will create a form using HTML, CSS, and JavaScript.

Architecting the solution


The following diagram shows the solution's architecture:

As we discussed at the start of the chapter, the user will fill in the form and submit the data. Once the data gets submitted, the HTTP trigger of the Azure Function processes the request and puts the data in the database.

In the preceding architecture diagram, the user will fill all the details in the form. Once the user submits the form, HttpRequest will be sent to Azure Function and HttpTrigger gets fired. HttpTrigger executes the code, which stores user details in Cosmos DB.

Now, in the next section, we will start our project.

Building the project


Now, we will start to build our project. First we will create the API for storing data in Cosmos DB. Once the API is created, we will then move on to design our user form using HTML and CSS.

For creating APIs, we need to create HttpTrigger in Azure Function.

Let's start with the creation of the API using Azure Function:

  1. First, login to the Azure portal. Navigate to the Function App, which we have created in the previous chapter.
  2. Click on the plus sign, select the language as JavaScript, and click on the HttpTrigger function, as shown in the following screenshot:
  1. Now, provide a name to the function and click on Create:
  1. Once the function is created, the default code will be opened:
  1. Now, we need to create the API, which takes an input from the request and stores the output in the database. So, click on Integrate to add output binding:
  1. Now, click on Outputs to add output binding as Cosmos DB:
  1. Select Azure Cosmos DB, and then click on the Select button:
  1. To add output binding as Cosmos...

Using storage


We have created an API and a user form, which uses Azure Cosmos DB to store user data.

Cosmos DB is a database service that is globally distributed. It allows us to manage our data even if we keep it in datacenters that are scattered throughout the world. It provides the tools we need to scale both global distribution patterns and computational resources, and these tools are provided by Microsoft Azure.

Here are some of the advantages:

  • It can support multiple data models using one backend. This means that it can be used for document, key value, relational, and graph models.
  • It is more or less a NoSQL database because it does not rely on any schemas.
  • It uses a query language similar to SQL and can easily support ACID transactions; some people classify it as a NewSQL type of database.

Test


Now, let's test our portal as follows:

  1. Run the project from NetBeans. It will open the portal in the default browser.
  2. Fill the form and click on the Submit button as shown in the following screenshot:
  1. Once the form is processed successfully, it will give you a Data is saved!! popup, as shown in the following screenshot:
  1. Now, to verify the entry in the database, navigate to Azure Cosmos DB, select the database, and check the entry. If the entry is there, then our portal is working fine. In our case, data is there in DB:

Summary


In this chapter, we learned about real-life examples of Azure Function. We have discussed the architecture of a project. We have created an API using HttpTrigger and Azure Cosmos DB as the output binding. We created a portal, where the user can enter their details and have it stored in Cosmos DB. After that, we discussed Cosmos DB as storage. In the last section of this chapter, we tested our portal and verified its entry in the database.

In the next chapter, we will see how to manage and deploy our code and we will learn how to configure Continuous Integration and Continuous Delivery.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning Azure Functions
Published in: Sep 2017 Publisher: Packt ISBN-13: 9781787122932
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}