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"
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
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.
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:
- First, login to the Azure portal. Navigate to the Function App, which we have created in the previous chapter.
- Click on the plus sign, select the language as
JavaScript
, and click on the HttpTrigger
function, as shown in the following screenshot:
- Now, provide a name to the function and click on
Create
:
- Once the function is created, the default code will be opened:
- 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:
- Now, click on
Outputs
to add output binding as Cosmos DB:
- Select
Azure Cosmos DB
, and then click on the Select
button:
- To add output binding as Cosmos...
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.
Now, let's test our portal as follows:
- Run the project from NetBeans. It will open the portal in the default browser.
- Fill the form and click on the
Submit
button as shown in the following screenshot:
- Once the form is processed successfully, it will give you a
Data is saved!!
popup, as shown in the following screenshot:
- 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:
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.