Пример #1
0
        internal static async Task CreateItemInNonPartitionedContainer(
            ContainerCore container,
            string itemId)
        {
            (string endpoint, string authKey)accountInfo = TestCommon.GetAccountInfo();
            //Creating non partition Container item.
            HttpClient client       = new System.Net.Http.HttpClient();
            Uri        baseUri      = new Uri(accountInfo.endpoint);
            string     dateTimeUtc  = NonPartitionedContainerHelper.GetUtcDateTime();
            string     verb         = "POST";
            string     resourceType = "docs";
            string     resourceLink = string.Format("dbs/{0}/colls/{1}/docs", container.Database.Id, container.Id);
            string     authHeader   = NonPartitionedContainerHelper.GenerateMasterKeyAuthorizationSignature(
                verb, container.LinkUri.OriginalString,
                resourceType,
                accountInfo.authKey,
                "master",
                "1.0",
                dateTimeUtc);

            client.DefaultRequestHeaders.Add("x-ms-date", dateTimeUtc);
            client.DefaultRequestHeaders.Add("x-ms-version", NonPartitionedContainerHelper.PreNonPartitionedMigrationApiVersion);
            client.DefaultRequestHeaders.Add("authorization", authHeader);

            string itemDefinition = JsonConvert.SerializeObject(ToDoActivity.CreateRandomToDoActivity(id: itemId));
            {
                StringContent       itemContent = new StringContent(itemDefinition);
                Uri                 requestUri  = new Uri(baseUri, resourceLink);
                HttpResponseMessage response    = await client.PostAsync(requestUri.ToString(), itemContent);

                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, response.ToString());
            }
        }
Пример #2
0
        internal static async Task <DocumentCollection> CreateNonPartitionedContainer(
            Cosmos.Database database,
            DocumentCollection documentCollection)
        {
            (string endpoint, string authKey)accountInfo = TestCommon.GetAccountInfo();

            //Creating non partition Container, rest api used instead of .NET SDK api as it is not supported anymore.
            HttpClient client      = new System.Net.Http.HttpClient();
            Uri        baseUri     = new Uri(accountInfo.endpoint);
            string     dateTimeUtc = NonPartitionedContainerHelper.GetUtcDateTime();

            string verb         = "POST";
            string resourceType = "colls";
            string resourceId   = string.Format("dbs/{0}", database.Id);
            string resourceLink = string.Format("dbs/{0}/colls", database.Id);

            client.DefaultRequestHeaders.Add("x-ms-date", dateTimeUtc);
            client.DefaultRequestHeaders.Add("x-ms-version", NonPartitionedContainerHelper.PreNonPartitionedMigrationApiVersion);

            string authHeader = NonPartitionedContainerHelper.GenerateMasterKeyAuthorizationSignature(
                verb,
                resourceId,
                resourceType,
                accountInfo.authKey,
                "master",
                "1.0",
                dateTimeUtc);

            client.DefaultRequestHeaders.Add("authorization", authHeader);
            string        containerDefinition = documentCollection.ToString();
            StringContent containerContent    = new StringContent(containerDefinition);
            Uri           requestUri          = new Uri(baseUri, resourceLink);

            DocumentCollection responseCollection = null;

            using (HttpResponseMessage response = await client.PostAsync(requestUri.ToString(), containerContent))
            {
                response.EnsureSuccessStatusCode();
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, response.ToString());
                responseCollection = await response.Content.ToResourceAsync <DocumentCollection>();
            }

            return(responseCollection);
        }
Пример #3
0
        internal static async Task CreateUndefinedPartitionItem(
            ContainerCore container,
            string itemId)
        {
            (string endpoint, string authKey)accountInfo = TestCommon.GetAccountInfo();
            //Creating undefined partition key  item, rest api used instead of .NET SDK api as it is not supported anymore.
            HttpClient client      = new System.Net.Http.HttpClient();
            Uri        baseUri     = new Uri(accountInfo.endpoint);
            string     dateTimeUtc = NonPartitionedContainerHelper.GetUtcDateTime();

            client.DefaultRequestHeaders.Add("x-ms-date", dateTimeUtc);
            client.DefaultRequestHeaders.Add("x-ms-version", NonPartitionedContainerHelper.PreNonPartitionedMigrationApiVersion);
            client.DefaultRequestHeaders.Add("x-ms-documentdb-partitionkey", "[{}]");

            //Creating undefined partition Container item.
            string verb         = "POST";
            string resourceType = "docs";
            string resourceId   = container.LinkUri.OriginalString;
            string resourceLink = string.Format("dbs/{0}/colls/{1}/docs", container.Database.Id, container.Id);
            string authHeader   = NonPartitionedContainerHelper.GenerateMasterKeyAuthorizationSignature(
                verb,
                resourceId,
                resourceType,
                accountInfo.authKey,
                "master",
                "1.0",
                dateTimeUtc);

            client.DefaultRequestHeaders.Remove("authorization");
            client.DefaultRequestHeaders.Add("authorization", authHeader);

            var           payload        = new { id = itemId, user = itemId };
            string        itemDefinition = JsonConvert.SerializeObject(payload);
            StringContent itemContent    = new StringContent(itemDefinition);
            Uri           requestUri     = new Uri(baseUri, resourceLink);
            await client.PostAsync(requestUri.ToString(), itemContent);
        }