Developing the Web API

The Web API consumes the Cosmos DB data that we just created and exposes the REST API be consumed by other applications. Here are the steps to be followed to build the service.

  1. Open Visual Studio and create web application, by using context menu File| New Project| Web | ASP.NET Web Application (.NET Framework).
  2. From the New ASP.NET Web Application dialog window, select Azure API App option.
Azure API App selection
  1. Add Data Models for Product and Product List as follows by creating two different Class Product.cs and ProductList.cs.
public class Product
{
public string Name { get; set; }
public Uri AssetURL { get; set; }
public string Price { get; set; }
public string AssetName { get; set; }
}
public class ProductList
{

public string Id { get; set; }
public List<Product> Products { get; set; }
}

 

  1. Within Project, add a new class, say ProductListCreator.cs , which is responsible for connecting with SQL-DocumentDB and reading product details
  2. Within ProductListCreator class, add following constant strings for connecting with SQL-DocumentDB.
private const string EndpointUri = "https://[endpointname].documents.azure.com:443/";
private const string PrimaryKey = "[primary key]";
private const string DatabaseName = "[database name]";
private const string CollectionName = "[collection name]";
  1. Now within ProductListCreator class, add a new method for fetching data from SQL-DocumentDB.
public ProductList GetProductList(string id)
{
this.client = new DocumentClient(new
Uri(EndpointUri), PrimaryKey);
FeedOptions queryOptions = new FeedOptions {
MaxItemCount = -1 };
IQueryable<ProductList> prodQuery = this.client.
CreateDocumentQuery<ProductList>(

UriFactory.CreateDocument
CollectionUri(DatabaseName,
CollectionName), queryOptions)
.Where(f => f.Id == id);
for each (ProductList product in
prodQuery)
{
return product;
}
return null;
}

 

  1. Add a new controller by right clicking on the Controllers folder within the newly created project, select Add | Controller from the context menu, and name it AssetsController.
  2. Within this controller class, add following method which returns the ProductList when GetAsset() is invoked.
public IHttpActionResult GetAssets()
{
ProductList planlist = new ProductListCreator().LoadPlanList("ProductCatalog"); if (planlist == null)
{
return NotFound();
}
return (Ok(planlist));
}
  1. Add another controller, by right clicking on the Controllers folder within the newly created project, select Add | Controller from the context menu, and name it PurchaseController.
  2. Add the following Post method with in the Purchase Controller.
    [HttpPost]
public IHttpActionResult AddToCart()
{
return Ok("Added To Cart");
}
  1. Build the solution and deploy it to Azure.

Once your service is hosted and deployed on Azure ( We have dicussed the deployment process of Azure Services in the Chapter 6, Remote Monitoring of Smart Building(s) Using HoloLens - Developing Application and Deploying on Device) , you can hit the end points (like - http://<servicename>>.azurewebsites.net/api/assets) with any of the Rest API Client Tools,you should be able to view the JSON data in a format like the following:

"Products": [
{
"Name": "TV",
"AssetURL":
"https://ch8assets.blob.core.windows.net/3dassets/tv",
"Price": null,
"AssetName": "TV"
},

Once the service is deployed and end points return the preceding JSON data, our backend solution is ready. Now we will move to the next steps of the solution development we will start building up the Holographic Solution using Unity3D and then integrate with our backend services set up so far.

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

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