Permalink
Browse files

attempting to fix up writes to the table

  • Loading branch information...
1 parent 4217ee4 commit cfa6244369fc029264dabc482563388d7e07203b @richorama committed Nov 17, 2013
@@ -17,7 +17,7 @@ namespace netmfazurestorage
{
public class Program
{
- private const string AccountName = "netmftest"; // please upload pictures of cats to this storage account.
+ private const string AccountName = "netmftest"; // please upload pictures of cats and other larger animals to this storage account.
private const string AccountKey = "gYU/Nf/ib97kHQrVkxhdD3y0lKz6ZOVaR1FjeDpESecGuqZOEq1TE+5+SXfZ/DBzKsXH3m0NDsLxTbTqQxL9yA==";
private static BlobClient _blobClient;
private static TableClient _tableClient;
@@ -45,7 +45,7 @@ public TableClient(CloudStorageAccount account)
additionalHeaders.Add("Content-Type", ContentType);
}
- public void CreateTable(string tableName)
+ public HttpStatusCode CreateTable(string tableName)
{
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" +
"<entry xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\" " +
@@ -62,7 +62,7 @@ public void CreateTable(string tableName)
int contentLength = 0;
byte[] payload = GetBodyBytesAndLength(xml, out contentLength);
string header = CreateAuthorizationHeader(payload, "/" + _account.AccountName + "/Tables()");
- AzureStorageHttpHelper.SendWebRequest(_account.UriEndpoints["Table"] + "/Tables()", header, DateHeader, VersionHeader, payload, contentLength, "POST", false, this.additionalHeaders);
+ return AzureStorageHttpHelper.SendWebRequest(_account.UriEndpoints["Table"] + "/Tables()", header, DateHeader, VersionHeader, payload, contentLength, "POST", false, this.additionalHeaders).StatusCode;
}
[Obsolete("Please use the InsertTableEntity method; this AddTableEntityForTemperature method will be removed in a future release.", false)]
@@ -90,7 +90,7 @@ public void AddTableEntityForTemperature(string tablename, string partitionKey,
AzureStorageHttpHelper.SendWebRequest(StringUtility.Format("{0}/{1}", _account.UriEndpoints["Table"], tablename), header, DateHeader, VersionHeader, payload, contentLength, "GET", false, this.additionalHeaders);
}
- public void InsertTableEntity(string tablename, string partitionKey, string rowKey, DateTime timeStamp, System.Collections.ArrayList tableEntityProperties)
+ public HttpStatusCode InsertTableEntity(string tablename, string partitionKey, string rowKey, DateTime timeStamp, System.Collections.ArrayList tableEntityProperties)
{
var timestamp = timeStamp.ToString("yyyy-MM-ddTHH:mm:ss.0000000Z");
@@ -110,10 +110,33 @@ public void InsertTableEntity(string tablename, string partitionKey, string rowK
int contentLength = 0;
byte[] payload = GetBodyBytesAndLength(xml, out contentLength);
string header = CreateAuthorizationHeader(payload, StringUtility.Format("/{0}/{1}", _account.AccountName, tablename));
- AzureStorageHttpHelper.SendWebRequest(StringUtility.Format("{0}/{1}", _account.UriEndpoints["Table"], tablename), header, DateHeader, VersionHeader, payload, contentLength, "POST", false, this.additionalHeaders);
+ return AzureStorageHttpHelper.SendWebRequest(StringUtility.Format("{0}/{1}", _account.UriEndpoints["Table"], tablename), header, DateHeader, VersionHeader, payload, contentLength, "POST", false, this.additionalHeaders).StatusCode;
}
- public void InsertTableEntity_Experimental(string tablename, string partitionKey, string rowKey, DateTime timeStamp, Hashtable tableEntityProperties)
+ public HttpStatusCode InsertTableEntity_Experimental(string tablename, string partitionKey, string rowKey, DateTime timeStamp, Hashtable tableEntityProperties)
+ {
+ var timestamp = timeStamp.ToString("yyyy-MM-ddTHH:mm:ss.0000000Z");
+
+ string xml =
+ StringUtility.Format("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><entry xml:base=\"http://{0}.table.core.windows.net/\" xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\" xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\" m:etag=\"W/&quot;datetime'2008-09-18T23%3A46%3A19.4277424Z'&quot;\" xmlns=\"http://www.w3.org/2005/Atom\">" +
+ "<id>{6}/{5}(PartitionKey='{2}',RowKey='{3}')</id>" +
+ "<title/><updated>{1}</updated><author><name /></author>" +
+ "<link />" +
+ "<category term=\"{0}.Tables\" scheme=\"http://schemas.microsoft.com/ado/2007/08/dataservices/scheme\" />" +
+ "<content type=\"application/xml\"><m:properties><d:PartitionKey>{2}</d:PartitionKey><d:RowKey>{3}</d:RowKey>" +
+ "<d:Timestamp m:type=\"Edm.DateTime\">{1}</d:Timestamp>" +
+ "{4}" +
+ "</m:properties>" +
+ "</content>" +
+ "</entry>", _account.AccountName, timestamp, partitionKey, rowKey, GetTableXml(tableEntityProperties), tablename, _account.UriEndpoints["Table"]);
+
+ int contentLength = 0;
+ byte[] payload = GetBodyBytesAndLength(xml, out contentLength);
+ string header = CreateAuthorizationHeader(payload, StringUtility.Format("/{0}/{1}", _account.AccountName, tablename));
+ return AzureStorageHttpHelper.SendWebRequest(StringUtility.Format("{0}/{1}", _account.UriEndpoints["Table"], tablename), header, DateHeader, VersionHeader, payload, contentLength, "POST", false, this.additionalHeaders).StatusCode;
+ }
+
+ public HttpStatusCode UpdateTableEntity(string tablename, string partitionKey, string rowKey, DateTime timeStamp, Hashtable tableEntityProperties)
{
var timestamp = timeStamp.ToString("yyyy-MM-ddTHH:mm:ss.0000000Z");
@@ -132,8 +155,8 @@ public void InsertTableEntity_Experimental(string tablename, string partitionKey
int contentLength = 0;
byte[] payload = GetBodyBytesAndLength(xml, out contentLength);
- string header = CreateAuthorizationHeader(payload, StringUtility.Format("/{0}/{1}", _account.AccountName, tablename));
- AzureStorageHttpHelper.SendWebRequest(StringUtility.Format("{0}/{1}", _account.UriEndpoints["Table"], tablename), header, DateHeader, VersionHeader, payload, contentLength, "POST", false, this.additionalHeaders);
+ string header = CreateAuthorizationHeader(payload, StringUtility.Format("/{0}/{1}(PartitionKey='{2}',RowKey='{3}')", _account.AccountName, tablename, partitionKey, rowKey));
+ return AzureStorageHttpHelper.SendWebRequest(StringUtility.Format("{0}/{1}(PartitionKey='{2}',RowKey='{3}')", _account.UriEndpoints["Table"], tablename, partitionKey, rowKey), header, DateHeader, VersionHeader, payload, contentLength, "PUT", false, this.additionalHeaders).StatusCode;
}
private string GetTableXml(ArrayList tableEntityProperties)
@@ -3,6 +3,7 @@
using netmfazurestorage.Table;
using System.Collections;
using Microsoft.SPOT;
+using System.Net;
namespace netmfazurestorage.Tests
{
@@ -23,6 +24,7 @@ public void Run()
this.TestInsertExperimental();
this.QuerySingleEntity();
this.QueryMultipleEntities();
+ this.UpdateTableEntity();
}
private void TestCreate()
@@ -47,7 +49,8 @@ public void TestInsert()
tep.Type = "Edm.String";
values.Add(tep);
- this.client.InsertTableEntity("netmftest", "1", Guid.NewGuid().ToString(), DateTime.Now, values);
+ var code = this.client.InsertTableEntity("netmftest", "1", Guid.NewGuid().ToString(), DateTime.Now, values);
+ Debug.Assert(HttpStatusCode.Created == code);
}
public void TestInsertDouble()
@@ -72,7 +75,8 @@ public void TestInsertDouble()
tep.Type = "Edm.Double";
values.Add(tep);
- this.client.InsertTableEntity("netmftest", "1", Guid.NewGuid().ToString(), DateTime.Now, values);
+ var code = this.client.InsertTableEntity("netmftest", "1", Guid.NewGuid().ToString(), DateTime.Now, values);
+ Debug.Assert(HttpStatusCode.Created == code);
}
@@ -86,7 +90,8 @@ public void TestInsertExperimental()
values.Add("doublefield", (double)123.22);
values.Add("int64field", (Int64)64);
values.Add("boolfield", true);
- this.client.InsertTableEntity_Experimental("netmftest", "2", Guid.NewGuid().ToString(), DateTime.Now, values);
+ var code = this.client.InsertTableEntity_Experimental("netmftest", "2", Guid.NewGuid().ToString(), DateTime.Now, values);
+ Debug.Assert(code == System.Net.HttpStatusCode.Created);
}
public void QuerySingleEntity()
@@ -102,6 +107,23 @@ public void QueryMultipleEntities()
Debug.Assert(output.Count > 0);
}
+ public void UpdateTableEntity()
+ {
+ var rowKey = Guid.NewGuid();
+ var values = new Hashtable();
+ values.Add("guidfield", Guid.NewGuid());
+ values.Add("int32field", 32);
+ values.Add("stringfield", "string");
+ //values.Add("datetimefield", DateTime.Now); // not sure why this is appearing as a null in the table
+ values.Add("doublefield", (double)123.22);
+ values.Add("int64field", (Int64)64);
+ values.Add("boolfield", true);
+ var code1 = this.client.InsertTableEntity_Experimental("netmftest", "3", rowKey.ToString(), DateTime.Now, values);
+
+ values["stringfield"] = "updated string";
+ var code2 = this.client.UpdateTableEntity("netmftest", "3", rowKey.ToString(), DateTime.Now, values);
+ }
+
}
}
Binary file not shown.

0 comments on commit cfa6244

Please sign in to comment.