using azure table service for NAR RESO odata


 

Some notes on what we did to call the azure table services entity endpoints, and $metadata.

First we built a code app for table storage management:

image

image

The tool needs a webapi server – i.e. the cloud table service. So we created ourselves an “instance” of the webapi:

image

Server and data repository created, we used the tool to post and update of an entity.

Since this tool does not actually bother to read (or update) metadata for the entities it posts, we found ourselves using other samples to make a simple command line tool that simply issues canned queries (with suitable security headers attached). This at least now reads the metadata (EDMX)

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static HttpWebRequest GenerateODataWebRequestForAzureStorage(string url, string accountName, string accountKey)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.Headers.Add("x-ms-date", DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture));
            var resource = request.RequestUri.AbsolutePath;
            string stringToSign = string.Format("{0}\n/{1}{2}", request.Headers["x-ms-date"], accountName,
            resource
            );
            var hasher = new HMACSHA256(Convert.FromBase64String(accountKey));
            string signedSignature = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
            string authorizationHeader = string.Format("{0} {1}:{2}", "SharedKeyLite", accountName, signedSignature);
            request.Headers.Add("Authorization", authorizationHeader);
            return request;
        }

        static void QueryTableItemsWithRawODataHttp()
        {
            var accountName = "reso";
            var accountKey = "ai8cqXvEv6hE17qsgnFw2TK0rEkMA84EU8xQ/dIrH4KAHfTaB27HFFOowM9LVQroClukysG223KcUPb6md2PRA==";
            var queryUrl = string.Format("http://{0}.table.core.windows.net/Property?$top=5", accountName);
            var queryUrl1 = string.Format("http://{0}.table.core.windows.net/$metadata", accountName);
            var queryUrl2 = string.Format("http://{0}.table.core.windows.net/Tables()", accountName);

            var request1 = GenerateODataWebRequestForAzureStorage(queryUrl1, accountName, accountKey);
            var response1 = request1.GetResponse();
            using (var sr = new StreamReader(response1.GetResponseStream()))
            {
                var doc = XElement.Load(sr);
                Console.WriteLine(doc);
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            var request2 = GenerateODataWebRequestForAzureStorage(queryUrl2, accountName, accountKey);
            var response2 = request2.GetResponse();
            using (var sr = new StreamReader(response2.GetResponseStream()))
            {
                var doc = XElement.Load(sr);
                Console.WriteLine(doc);
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            var request = GenerateODataWebRequestForAzureStorage(queryUrl, accountName, accountKey);
            var response = request.GetResponse();
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                var doc = XElement.Load(sr);
                var nsMetadata = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
                var nsSchema = "http://schemas.microsoft.com/ado/2007/08/dataservices";
                foreach (var elmItem in doc.Descendants(XName.Get("properties", nsMetadata)))
                {
                    var name = elmItem.Descendants(XName.Get("ListingRID", nsSchema)).First().Value;
                    var partitionKey = elmItem.Descendants(XName.Get("PartitionKey", nsSchema)).First().Value;
                    Console.WriteLine("ListingRID:{0}, RowKey:{1}", name, partitionKey);
                }
            }
        }


        static void Main(string[] args)
        {

            QueryTableItemsWithRawODataHttp();
        }
    }
}

This allows us to see in fiddler what is passing on the wire (in XML format):

image

Advertisements

About home_pw@msn.com

Computer Programmer who often does network administration with focus on security servers. Very strong in Microsoft Azure cloud!
This entry was posted in coding theory. Bookmark the permalink.