Chapter 7. Incorporating API Data Access Using Microsoft Azure App Services

In the previous chapter, we learned about the Razor templating engine and how you can use it to create a hybrid mobile solution. You learned how to create and use models within your application, and how to save the information to a SQLite database and retrieve it later. Towards the end of the chapter, you learned how you can use JavaScript code using C# to execute method calls.

Up until this point, you have been building the TrackMyWalks app with static walk trail information that has been hard-coded within the TrackMyWalks app. However, in the real world, it is very rare that your app will depend purely on local static data, and you will need to source your information from a remote data source, typically using a RESTful API. In some cases, your app may even communicate with a third-party API, for example Facebook.

In this chapter, you'll learn how you can use Microsoft Azure App services to create your very first live, cloud-based backend HTTP web service to handle all the communication between the cloud and the app. You will also learn how to create a DataService API that will allow the app to consume the API so that we can retrieve, store, and delete walk trail information from the cloud all within the TrackMyWalks app.

This chapter will cover the following points:

  • Gain an understanding of what Microsoft Azure App services are
  • Setting up the TrackMyWalks app within the Microsoft Azure portal
  • Adding the HttpClient and JSON.Net NuGet packages to the solution
  • Creating the TrackMyWalks base HTTP service
  • Creating the TrackMyWalks API data service
  • Updating the TrackMyWalksViewModels to use the API data service
  • Running the TrackMyWalks app within the simulator

Setting up our TrackMyWalks app using Microsoft Azure

In this section, we will look at the steps required to set up the TrackMyWalks application within Microsoft Azure. Nearly all mobile applications that you will develop will require the ability to communicate with an API to store, retrieve, update, and delete information. This API can be an existing one that someone within your organization has already created, but sometimes you will need to create your own API for your application.

Microsoft Azure, or ("Azure" as it's best known for), is essentially a cloud-based platform that was created by Microsoft back in February 2010. Azure was designed for building, deploying, and managing several applications and their associated services, such as SaaS, PaaS, and IaaS.

Each of the Microsoft Azure specific associated services are explained in the following table:

Azure service

Description

SaaS

Software as a Service provides software licensing and delivery models where software is licensed on a subscription basis and is centrally hosted.

PaaS

Platform as a Service provides customers with a platform to develop, run, and manage applications without the complexities of maintaining the infrastructure when developing and launching an app.

IaaS

Infrastructure as a Service provides virtualized computing resources over the Internet.

One of the main benefits of using Microsoft Azure Mobile Apps is that they provide you with a very quick and easy way to get a fully functional backend service up and running within a matter of minutes. Before we can proceed with setting up and creating our TrackMyWalks database within the cloud, you will need to have a Microsoft Azure account. If you don't already have one, you can create one for free at https://azure.microsoft.com/en-us/pricing/free-trial/ .

Once you have created your Microsoft Azure account, you will need to log into the Microsoft Azure portal using your web browser. Let's look at how to do this, by performing the following steps:

  1. Launch your web browser with the following URL https://portal.azure.com/ and log in to the Microsoft Azure portal using your credentials.
  2. Next, from the main Microsoft Azure portal dashboard, click the + button in the top-left hand corner from the New section, and select the Web + Mobile option, and then choose the Mobile App option as shown in the following screenshot:

    Setting up our TrackMyWalks app using Microsoft Azure

  3. Then, enter in TrackMyWalks to use as the name for our app for the App name field.
  4. Next, either choose your Subscription type, or leave the default of Free Trial.
  5. Then, provide a name for your Resource Group, either by creating a new one, or choosing from an existing one.
  6. Next, ensure that the Pin to dashboard option has been selected, so that you can have your Mobile App displayed on the Microsoft Azure Dashboard. This is particularly useful, and provides easy access.

Now that we have successfully created our Mobile App within Microsoft Azure, our next step is to begin setting up the database that will allow our app to store walk entry information. Let's look at how we can achieve this with the following steps:

  1. From the Dashboard, click on the TrackMyWalks service, as shown in the following screenshot:

    Setting up our TrackMyWalks app using Microsoft Azure

  2. Next, choose the Data connections option from the MOBILE section under the TrackMyWalks App Service section, as shown in the following screenshot:

    Setting up our TrackMyWalks app using Microsoft Azure

  3. Then, within the TrackMyWalks - Data connections screen, click on the + Add button, to display the Add data connection screen, as shown in the following screenshot:

    Setting up our TrackMyWalks app using Microsoft Azure

  4. Next, ensure that you have selected SQL Database from the Type dropdown, and proceed to configure your SQL Database.
  5. Then, click on the OK button to save your changes and create the new data connection for our TrackMyWalks SQL server database.

Once you have created your TrackMyWalks mobile app and SQL database within Microsoft Azure, by default, your database won't contain any database tables or data. Before we can start communicating with and consuming the API within our TrackMyWalks app, we need to create a new table that will store our walk trail entries.

Let's look at the following steps to achieve this:

  1. From the Dashboard, click on the TrackMyWalks service, then choose the Easy tables option from the MOBILE section under the TrackMyWalks App Service section.
  2. Next, within the TrackMyWalks - Easy tables screen, click on the + Add button, to display the Add a table screen, as shown in the following screenshot:

    Setting up our TrackMyWalks app using Microsoft Azure

  3. Then, enter in WalkEntries to use as the name for our table for the Name field.
  4. Next, leave the default permissions that have been set for our Insert permission, Update permission, Delete permission, Read permission, and Undelete permission dropdown entries:

    Setting up our TrackMyWalks app using Microsoft Azure

  5. Then, click on the OK button to save your changes, and your new WalkEntries table will be added to the list of Easy Tables entries.

    Note

    Whenever you choose the Allow anonymous access permission during the creation of your table, you are essentially making the API available without providing any specific authentication headers as part of the HTTP request.

    Before we can start making calls to our API and consuming this within our TrackMyWalks app, we'll run a quick check to see if our API endpoint is working correctly. This is achieved by issuing a GET HTTP request using the command line, or if you'd prefer, you can use a REST console client.

  6. Open your terminal window, and type in the following statement from the command line as follows:
    Last login: Sun Nov 6 10:48:41 on console
    GENIESOFT-MAC-Mini:~ stevendaniel$ curl
            https://trackmywalks.azurewebsites.net/tables/
            walkentries --header "ZUMO-API-VERSION:2.0.0"
    

    If you have set everything up correctly within the Microsoft Azure portal, you should receive back a 200 (Success) status code, along with an empty collection in the response body as follows:

    Last login: Sun Nov 6 10:48:41 on console
    GENIESOFT-MAC-Mini:~ stevendaniel$ curl 
            https://trackmywalks.azurewebsites.net/tables/
            walkentries --header "ZUMO-API-VERSION:2.0.0"
    [] GENIESOFT-MAC-Mini:~ stevendaniel$ 
    

    Note

    There are several REST console clients that exist for you to choose from, if you don't already have one installed. I tend to use Postman for handling REST APIs, which you can download from http://www.getpostman.com/.

Now that we have successfully created our TrackMyWalks API and WalkEntries data table within the service, we can begin making calls to our API and receiving those response messages directly back from the API. In the next section, we will begin to add the Json.Net and HttpClient .NET Framework libraries that will be responsible for handling the REST API requests to save and retrieve our walk entry details.

Adding the Json.Net NuGet package to the TrackMyWalks app

Now that you have set up and created the TrackMyWalks database within the Microsoft Azure platform, our next step is to add the Json.Net NuGet package to our TrackMyWalks Portable Class Library solution. The Json.Net package is a high-performance JSON framework for the .NET platform that allows you to serialize and deserialize any type of .NET object with help of the JSON serializer.

When we start to incorporate this framework within our TrackMyWalks solution, we will have the ability of performing LINQ to JSON capabilities that will enable us to create, parse, query, and modify the JSON structure that we receive back from our Microsoft Azure TrackMyWalks database table.

Let's look at how to add the Json.Net NuGet package to our TrackMyWalks Portable Class Library , by performing the following steps:

  1. Right-click on the Packages folder that is contained within the TrackMyWalks Portable Class Library solution, and choose Add Packages... menu option, as shown in the following screenshot:

    Adding the Json.Net NuGet package to the TrackMyWalks app

  2. This will display the Add Packages dialog, enter in Json.Net within the search dialog, and select the Json.Net option within the list, as shown in the following screenshot:

    Adding the Json.Net NuGet package to the TrackMyWalks app

  3. Finally, click on the Add Package button to add the NuGet package to the Packages folder, contained within the TrackMyWalks Portable Class Library solution.

Now that you have added the Json.Net NuGet package, our next step is to add the HttpClient framework to our TrackMyWalks Portable Class Library, which we will be covering in the next section.

Adding the HttpClient NuGet package to the TrackMyWalks app

In the previous section, we added the Json.Net NuGet package to our TrackMyWalks solution. Our next step is to add the HTTP library to our TrackMyWalks solution to enable it to communicate with an API over HTTP.

Since we are using both .NET and C# to build our Xamarin.Forms application, we can leverage a library within the .NET Framework called System.Net.Http.HttpClient. This HttpClient framework provides us with a mechanism of sending and receiving data using standard HTTP methods, such as GET and POST.

Let's look at how to add the HttpClient NuGet package to our TrackMyWalks Portable Class Library, by performing the following steps:

  1. Right-click on the Packages folder that is contained within the TrackMyWalks Portable Class Library solution, and choose the Add Packages... menu option. If you can't remember how to do this, you can refer to the section entitled Adding the Json.Net NuGet package to the TrackMyWalks app located within this chapter.
  2. This will display the Add Packages dialog. Here, enter in Http within the search dialog, and select the System.Net.Http option, as shown in the following screenshot:

    Adding the HttpClient NuGet package to the TrackMyWalks app

  3. Finally, click on the Add Package button to add the NuGet package to the Packages folder, contained within the TrackMyWalks Portable Class Library solution.

Now that you have added both the Json.Net and System.Net.HttpNuGet packages to our solution, we can begin utilizing these framework libraries as we progress throughout this chapter.

Updating the WalkEntries model to use the Json.Net framework

In this section, we will begin by updating the WalkEntries data model to take advantage of our backend service calls, when we create these, and then the WalkDataService.cs and WalkWebService.cs files will communicate and interact with our Microsoft Azure TrackMyWalks database to store, delete, and retrieve walk entry information.

Let's now start to modify and implement the code required for our WalkEntries class model, by performing the following steps:

Ensure that the WalkEntries.cs file is displayed within the code editor, and enter in the following highlighted code sections:

        // 
        //  WalkEntries.cs 
        //  TrackMyWalks 
        // 
        //  Created by Steven F. Daniel on 04/08/2016. 
        //  Copyright © 2016 GENIESOFT STUDIOS. All rights reserved. 
        // 
         using System; 
         using Newtonsoft.Json;
        namespace TrackMyWalks.Models 
        { 
           public class WalkEntries 
          { 
            [JsonProperty("id")]

            public string Id { get; set; } 
            public string Title { get; set; } 
            public string Notes { get; set; } 
            public double Latitude { get; set; } 
            public double Longitude { get; set; } 
            public double Kilometers { get; set; } 
            public string Difficulty { get; set; } 
            public double Distance { get; set; } 
            public Uri ImageUrl { get; set; } 
          } 
        } 

In the preceding code snippet, we have successfully modified the database model that will be used to store walk entry information within our Microsoft Azure database. You will notice that we have defined a [JsonProperty("id")] item, as well as a string property named Id that will serve as a unique primary key for each record that we store within the database. We have also updated our ImageUrl property to include the Uri type that will be used to convert the URL entered within the walk entry page, so that it is stored correctly within the database.

Note

If you are interested in finding out more information about the JsonProperty and the Newtonsoft.Json classes, please refer to the Json.NET documentation located at http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_JsonProperty.htm .

..................Content has been hidden....................

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