Exemplo n.º 1
0
        private List <AzureTable> ListTables()
        {
            return(Retry <List <AzureTable> >(delegate()
            {
                HttpWebResponse response;
                List <AzureTable> tables = new List <AzureTable>();

                tables = new List <AzureTable>();

                try
                {
                    response = CreateRESTRequest("GET", "Tables", String.Empty, null, String.Empty, String.Empty).GetResponse() as HttpWebResponse;

                    if ((int)response.StatusCode == 200)
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            string result = reader.ReadToEnd();

                            XNamespace ns = "http://www.w3.org/2005/Atom";
                            XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";

                            XElement x = XElement.Parse(result, LoadOptions.SetBaseUri);

                            foreach (XElement table in x.Descendants(ns + "entry"))
                            {
                                AzureTable tableOutput = new AzureTable();
                                tableOutput.TableId = new Uri(table.Descendants(ns + "id").First().Value);
                                tableOutput.TableName = table.Descendants(d + "TableName").First().Value;
                                tableOutput.Updated = (DateTime)LanguagePrimitives.ConvertTo((table.Descendants(ns + "updated").First().Value), DateTime.Now.GetType());
                                tables.Add(tableOutput);
                            }
                        }
                    }

                    response.Close();

                    return tables;
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError &&
                        ex.Response != null &&
                        (int)(ex.Response as HttpWebResponse).StatusCode == 404)
                    {
                        return null;
                    }

                    throw;
                }
            }));
        }
        private List<AzureTable> ListTables()
        {
            return Retry<List<AzureTable>>(delegate()
            {
                HttpWebResponse response;
                List<AzureTable> tables = new List<AzureTable>();

                tables = new List<AzureTable>();

                try
                {
                    response = CreateRESTRequest("GET", "Tables", String.Empty, null, String.Empty, String.Empty).GetResponse() as HttpWebResponse;

                    if ((int)response.StatusCode == 200)
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            string result = reader.ReadToEnd();

                            XNamespace ns = "http://www.w3.org/2005/Atom";
                            XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";

                            XElement x = XElement.Parse(result, LoadOptions.SetBaseUri);

                            foreach (XElement table in x.Descendants(ns + "entry"))
                            {
                                AzureTable tableOutput = new AzureTable();
                                tableOutput.TableId = new Uri(table.Descendants(ns + "id").First().Value);
                                tableOutput.TableName = table.Descendants(d + "TableName").First().Value;
                                tableOutput.Updated = (DateTime)LanguagePrimitives.ConvertTo((table.Descendants(ns + "updated").First().Value), DateTime.Now.GetType());
                                tables.Add(tableOutput);
                            }
                        }
                    }

                    response.Close();

                    return tables;
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError &&
                        ex.Response != null &&
                        (int)(ex.Response as HttpWebResponse).StatusCode == 404)
                        return null;

                    throw;
                }
            });
        }
Exemplo n.º 3
0
        private List <AzureTable> CreateTable(string tableName)
        {
            return(Retry <List <AzureTable> >(delegate()
            {
                HttpWebResponse response;
                List <AzureTable> tables = new List <AzureTable>();

                try
                {
                    string now = DateTime.UtcNow.ToString("o");

                    string requestBody = String.Format("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" +
                                                       "<entry xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"" +
                                                       "       xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\"" +
                                                       "       xmlns=\"http://www.w3.org/2005/Atom\"> " +
                                                       "  <title /> " +
                                                       "  <updated>" + now + "</updated> " +
                                                       "  <author>" +
                                                       "    <name/> " +
                                                       "  </author> " +
                                                       "  <id/> " +
                                                       "  <content type=\"application/xml\">" +
                                                       "    <m:properties>" +
                                                       "      <d:TableName>{0}</d:TableName>" +
                                                       "    </m:properties>" +
                                                       "  </content> " +
                                                       "</entry>",
                                                       tableName);

                    if (!String.IsNullOrEmpty(Author))
                    {
                        if (!String.IsNullOrEmpty(Email))
                        {
                            requestBody.Replace("<name/>", ("<name>" + SecurityElement.Escape(Author) + "</name><email>" + SecurityElement.Escape(Email) + "</email>"));
                        }
                        else
                        {
                            requestBody.Replace("<name/>", ("<name>" + SecurityElement.Escape(Author) + "</name>"));
                        }
                    }
                    response = CreateRESTRequest("POST", "Tables", requestBody, null, null, null).GetResponse() as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.Created)
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            string result = reader.ReadToEnd();

                            XNamespace ns = "http://www.w3.org/2005/Atom";
                            XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";

                            XElement x = XElement.Parse(result, LoadOptions.SetBaseUri);
                            foreach (XElement table in x.Descendants(ns + "entry"))
                            {
                                AzureTable tableOutput = new AzureTable();
                                tableOutput.TableId = new Uri(table.Descendants(ns + "id").First().Value);
                                tableOutput.TableName = table.Descendants(d + "TableName").First().Value;
                                tableOutput.Updated = (DateTime)LanguagePrimitives.ConvertTo((table.Descendants(ns + "updated").First().Value), DateTime.Now.GetType());
                                tables.Add(tableOutput);
                            }
                        }
                    }
                    response.Close();

                    return tables;
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError &&
                        ex.Response != null &&
                        (int)(ex.Response as HttpWebResponse).StatusCode == 409)
                    {
                        return null;
                    }

                    throw;
                }
            }));
        }
        private List<AzureTable> CreateTable(string tableName)
        {
            return Retry<List<AzureTable>>(delegate()
            {
                HttpWebResponse response;
                List<AzureTable> tables = new List<AzureTable>();

                try
                {
                    string now = DateTime.UtcNow.ToString("o");

                    string requestBody = String.Format("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" +
                                          "<entry xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"" +
                                          "       xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\"" +
                                          "       xmlns=\"http://www.w3.org/2005/Atom\"> " +
                                          "  <title /> " +
                                          "  <updated>" + now + "</updated> " +
                                          "  <author>" +
                                          "    <name/> " +
                                          "  </author> " +
                                          "  <id/> " +
                                          "  <content type=\"application/xml\">" +
                                          "    <m:properties>" +
                                          "      <d:TableName>{0}</d:TableName>" +
                                          "    </m:properties>" +
                                          "  </content> " +
                                          "</entry>",
                                          tableName);

                    if (! String.IsNullOrEmpty(Author)) {
                        if (!String.IsNullOrEmpty(Email)) {
                            requestBody.Replace("<name/>", ("<name>" + SecurityElement.Escape(Author) + "</name><email>" + SecurityElement.Escape(Email) +  "</email>"));
                        } else {
                            requestBody.Replace("<name/>", ("<name>" + SecurityElement.Escape(Author) + "</name>"));
                        }

                    }
                    response = CreateRESTRequest("POST", "Tables", requestBody, null, null, null).GetResponse() as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.Created)
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            string result = reader.ReadToEnd();

                            XNamespace ns = "http://www.w3.org/2005/Atom";
                            XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";

                            XElement x = XElement.Parse(result, LoadOptions.SetBaseUri);
                            foreach (XElement table in x.Descendants(ns + "entry"))
                            {
                                AzureTable tableOutput = new AzureTable();
                                tableOutput.TableId = new Uri(table.Descendants(ns + "id").First().Value);
                                tableOutput.TableName = table.Descendants(d + "TableName").First().Value;
                                tableOutput.Updated = (DateTime)LanguagePrimitives.ConvertTo((table.Descendants(ns + "updated").First().Value), DateTime.Now.GetType());
                                tables.Add(tableOutput);
                            }
                        }
                    }
                    response.Close();

                    return tables;
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError &&
                        ex.Response != null &&
                        (int)(ex.Response as HttpWebResponse).StatusCode == 409)
                        return null;

                    throw;
                }
            });
        }