Exemplo n.º 1
0
        internal static void FillPartitionKeyRangeId(CosmosRequestMessage request, string partitionKeyRangeId)
        {
            Debug.Assert(request != null);

            if (!string.IsNullOrEmpty(partitionKeyRangeId))
            {
                request.PartitionKeyRangeId = partitionKeyRangeId;
            }
        }
Exemplo n.º 2
0
        internal static void FillMaxItemCount(CosmosRequestMessage request, int?maxItemCount)
        {
            Debug.Assert(request != null);

            if (maxItemCount.HasValue)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.PageSize, maxItemCount.Value.ToString(CultureInfo.InvariantCulture));
            }
        }
Exemplo n.º 3
0
        public void CosmosChangeFeedRequestOptions_AddsPartitionKeyRangeId()
        {
            CosmosRequestMessage           request        = new CosmosRequestMessage();
            CosmosChangeFeedRequestOptions requestOptions = new CosmosChangeFeedRequestOptions();

            CosmosChangeFeedRequestOptions.FillPartitionKeyRangeId(request, "randomPK");

            Assert.AreEqual("randomPK", request.PartitionKeyRangeId);
        }
 internal static void FillContinuationToken(
     CosmosRequestMessage request,
     string continuationToken)
 {
     if (!string.IsNullOrWhiteSpace(continuationToken))
     {
         request.Headers.Add(HttpConstants.HttpHeaders.Continuation, continuationToken);
     }
 }
        internal static void FillContinuationToken(CosmosRequestMessage request, string continuationToken)
        {
            Debug.Assert(request != null);

            if (!string.IsNullOrWhiteSpace(continuationToken))
            {
                // On REST level, change feed is using IfNoneMatch/ETag instead of continuation
                request.Headers.IfNoneMatch = continuationToken;
            }
        }
        /// <summary>
        /// Processes the current <see cref="CosmosRequestMessage"/> in the current handler and sends the current <see cref="CosmosRequestMessage"/> to the next handler in the chain.
        /// </summary>
        /// <param name="request"><see cref="CosmosRequestMessage"/> received by the handler.</param>
        /// <param name="cancellationToken"><see cref="CancellationToken"/> received by the handler.</param>
        /// <returns>An instance of <see cref="CosmosResponseMessage"/>.</returns>
        public virtual Task <CosmosResponseMessage> SendAsync(
            CosmosRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (this.InnerHandler == null)
            {
                throw new ArgumentNullException(nameof(this.InnerHandler));
            }

            return(this.InnerHandler.SendAsync(request, cancellationToken));
        }
Exemplo n.º 7
0
        public void StatelessTest()
        {
            QueryRequestOptions requestOption = new QueryRequestOptions();

            requestOption.RequestContinuation = "SomeToken";

            CosmosRequestMessage testMessage = new CosmosRequestMessage();

            requestOption.FillRequestOptions(testMessage);

            Assert.IsNull(testMessage.Headers.Continuation);
        }
Exemplo n.º 8
0
        public void ChangeFeedRequestOptions_DefaultValues()
        {
            CosmosRequestMessage     request        = new CosmosRequestMessage();
            ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions()
            {
            };

            requestOptions.FillRequestOptions(request);

            Assert.AreEqual(ChangeFeedRequestOptions.IfNoneMatchAllHeaderValue, request.Headers.IfNoneMatch);
            Assert.IsNull(request.Headers[Documents.HttpConstants.HttpHeaders.IfModifiedSince]);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Fill the CosmosRequestMessage headers with the set properties
        /// </summary>
        /// <param name="request">The <see cref="CosmosRequestMessage"/></param>
        public override void FillRequestOptions(CosmosRequestMessage request)
        {
            if (this.EnableScriptLogging)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.EnableLogging, bool.TrueString);
            }

            CosmosRequestOptions.SetSessionToken(request, this.SessionToken);
            CosmosRequestOptions.SetConsistencyLevel(request, this.ConsistencyLevel);

            base.FillRequestOptions(request);
        }
Exemplo n.º 10
0
        public void ChangeFeedRequestOptions_ContinuationIsSet()
        {
            CosmosRequestMessage     request        = new CosmosRequestMessage();
            ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions()
            {
            };

            ChangeFeedRequestOptions.FillContinuationToken(request, "something");
            requestOptions.FillRequestOptions(request);

            Assert.AreEqual("something", request.Headers.IfNoneMatch);
            Assert.IsNull(request.Headers[Documents.HttpConstants.HttpHeaders.IfModifiedSince]);
        }
Exemplo n.º 11
0
        public void ChangeFeedRequestOptions_AddsStartTime()
        {
            CosmosRequestMessage     request        = new CosmosRequestMessage();
            ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions()
            {
                StartTime = new DateTime(1985, 1, 1, 0, 0, 0, DateTimeKind.Utc)
            };

            requestOptions.FillRequestOptions(request);

            Assert.AreEqual("Tue, 01 Jan 1985 00:00:00 GMT", request.Headers[Documents.HttpConstants.HttpHeaders.IfModifiedSince]);
            Assert.IsNull(request.Headers.IfNoneMatch);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Create a <see cref="CosmosResponseMessage"/>
 /// </summary>
 /// <param name="statusCode">The HttpStatusCode of the response</param>
 /// <param name="requestMessage">The <see cref="CosmosRequestMessage"/> object</param>
 /// <param name="errorMessage">The reason for failures if any.</param>
 /// <param name="error">The inner error object</param>
 /// <param name="headers">The headers for the response.</param>
 internal CosmosResponseMessage(
     HttpStatusCode statusCode,
     CosmosRequestMessage requestMessage,
     string errorMessage,
     Error error,
     CosmosResponseMessageHeaders headers)
 {
     this.StatusCode     = statusCode;
     this.RequestMessage = requestMessage;
     this.ErrorMessage   = errorMessage;
     this.Error          = error;
     this.Headers        = headers;
 }
Exemplo n.º 13
0
        public void ChangeFeedRequestOptions_MaxItemSizeIsSet()
        {
            CosmosRequestMessage     request        = new CosmosRequestMessage();
            ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions()
            {
            };

            ChangeFeedRequestOptions.FillMaxItemCount(request, 10);
            requestOptions.FillRequestOptions(request);

            Assert.AreEqual("10", request.Headers[Documents.HttpConstants.HttpHeaders.PageSize]);
            Assert.AreEqual(ChangeFeedRequestOptions.IfNoneMatchAllHeaderValue, request.Headers.IfNoneMatch);
            Assert.IsNull(request.Headers[Documents.HttpConstants.HttpHeaders.IfModifiedSince]);
        }
        /// <summary>
        /// Create a <see cref="CosmosResponseMessage"/>
        /// </summary>
        /// <param name="statusCode">The HttpStatusCode of the response</param>
        /// <param name="requestMessage">The <see cref="CosmosRequestMessage"/> object</param>
        /// <param name="errorMessage">The reason for failures if any.</param>
        public CosmosResponseMessage(
            HttpStatusCode statusCode,
            CosmosRequestMessage requestMessage = null,
            string errorMessage = null)
        {
            if (((int)statusCode < 0) || ((int)statusCode > 999))
            {
                throw new ArgumentOutOfRangeException(nameof(statusCode));
            }

            this.StatusCode     = statusCode;
            this.RequestMessage = requestMessage;
            this.ErrorMessage   = errorMessage;
        }
Exemplo n.º 15
0
        /// <summary>
        /// Fill the CosmosRequestMessage headers with the set properties
        /// </summary>
        /// <param name="request">The <see cref="CosmosRequestMessage"/></param>
        public override void FillRequestOptions(CosmosRequestMessage request)
        {
            if (request.OperationType == OperationType.SqlQuery)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.IsQuery, bool.TrueString);
                request.Headers.Add(HttpConstants.HttpHeaders.EnableCrossPartitionQuery, this.EnableCrossPartitionQuery ? bool.TrueString : bool.FalseString);
                request.Headers.Add(HttpConstants.HttpHeaders.ContentType, MediaTypes.QueryJson);
            }

            CosmosRequestOptions.SetSessionToken(request, this.SessionToken);
            CosmosRequestOptions.SetConsistencyLevel(request, this.ConsistencyLevel);

            base.FillRequestOptions(request);
        }
Exemplo n.º 16
0
        public void CosmosChangeFeedRequestOptions_ContinuationBeatsStartTime()
        {
            CosmosRequestMessage           request        = new CosmosRequestMessage();
            CosmosChangeFeedRequestOptions requestOptions = new CosmosChangeFeedRequestOptions()
            {
                StartTime = new DateTime(1985, 1, 1)
            };

            CosmosChangeFeedRequestOptions.FillContinuationToken(request, "something");
            requestOptions.FillRequestOptions(request);

            Assert.AreEqual("something", request.Headers.IfNoneMatch);
            Assert.IsNull(request.Headers[Documents.HttpConstants.HttpHeaders.IfModifiedSince]);
        }
Exemplo n.º 17
0
        private static async Task <CosmosRequestMessage> GenerateCosmosRequestMessage(
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            CosmosRequestOptions requestOptions,
            CosmosContainerCore cosmosContainerCore,
            Object partitionKey,
            Stream streamPayload,
            Action <CosmosRequestMessage> requestEnricher)
        {
            HttpMethod method = ExecUtils.GetHttpMethod(operationType);

            CosmosRequestMessage request = new CosmosRequestMessage(method, resourceUri);

            request.OperationType  = operationType;
            request.ResourceType   = resourceType;
            request.RequestOptions = requestOptions;
            request.Content        = streamPayload;

            if (partitionKey != null)
            {
                if (cosmosContainerCore == null && partitionKey.Equals(PartitionKey.None))
                {
                    throw new ArgumentException($"{nameof(cosmosContainerCore)} can not be null with partition key as PartitionKey.None");
                }
                else if (partitionKey.Equals(PartitionKey.None))
                {
                    PartitionKeyInternal partitionKeyInternal = await cosmosContainerCore.GetNonePartitionKeyValue();

                    request.Headers.PartitionKey = partitionKeyInternal.ToJsonString();
                }
                else
                {
                    PartitionKey pk = new PartitionKey(partitionKey);
                    request.Headers.PartitionKey = pk.InternalKey.ToJsonString();
                }
            }

            if (operationType == OperationType.Upsert)
            {
                request.Headers.IsUpsert = bool.TrueString;
            }

            requestEnricher?.Invoke(request);

            return(request);
        }
        /// <summary>
        /// Dispose of the response message content
        /// </summary>
        /// <param name="disposing">True to dispose of content</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !_disposed)
            {
                _disposed = true;
                if (this._content != null)
                {
                    this._content.Dispose();
                    this._content = null;
                }

                if (this.RequestMessage != null)
                {
                    this.RequestMessage.Dispose();
                    this.RequestMessage = null;
                }
            }
        }
        /// <summary>
        /// Fill the CosmosRequestMessage headers with the set properties
        /// </summary>
        /// <param name="request">The <see cref="CosmosRequestMessage"/></param>
        public virtual void FillRequestOptions(CosmosRequestMessage request)
        {
            if (this.Properties != null)
            {
                foreach (KeyValuePair <string, object> property in this.Properties)
                {
                    request.Properties[property.Key] = property.Value;
                }
            }

            if (this.AccessCondition != null)
            {
                string accessConditionHeaderName = (this.AccessCondition.Type == AccessConditionType.IfMatch) ?
                                                   HttpConstants.HttpHeaders.IfMatch : HttpConstants.HttpHeaders.IfNoneMatch;

                request.Headers.Add(accessConditionHeaderName, this.AccessCondition.Condition);
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Used internally by friends ensrue robust argument and
        /// exception-less handling
        /// </summary>
        internal static Task <T> ProcessResourceOperationAsync <T>(
            CosmosClient client,
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            CosmosRequestOptions requestOptions,
            Object partitionKey,
            Stream streamPayload,
            Action <CosmosRequestMessage> requestEnricher,
            Func <CosmosResponseMessage, T> responseCreator,
            CancellationToken cancellationToken)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            if (client.RequestHandler == null)
            {
                throw new ArgumentException(nameof(client));
            }

            if (resourceUri == null)
            {
                throw new ArgumentNullException(nameof(resourceUri));
            }

            if (responseCreator == null)
            {
                throw new ArgumentNullException(nameof(responseCreator));
            }

            CosmosRequestMessage request = ExecUtils.GenerateCosmosRequestMessage(
                resourceUri,
                resourceType,
                operationType,
                requestOptions,
                partitionKey,
                streamPayload,
                requestEnricher);

            return(client.RequestHandler.SendAsync(request, cancellationToken)
                   .ContinueWith(task => responseCreator(task.Result), cancellationToken));
        }
        /// <summary>
        /// Fill the CosmosRequestMessage headers with the set properties
        /// </summary>
        /// <param name="request">The <see cref="CosmosRequestMessage"/></param>
        public override void FillRequestOptions(CosmosRequestMessage request)
        {
            // Check if no Continuation Token is present
            if (string.IsNullOrEmpty(request.Headers.IfNoneMatch))
            {
                if (this.StartTime == null)
                {
                    request.Headers.IfNoneMatch = ChangeFeedRequestOptions.IfNoneMatchAllHeaderValue;
                }
                else if (this.StartTime != null)
                {
                    request.Headers.Add(HttpConstants.HttpHeaders.IfModifiedSince, this.StartTime.Value.ToUniversalTime().ToString("r", CultureInfo.InvariantCulture));
                }
            }

            request.Headers.Add(HttpConstants.HttpHeaders.A_IM, HttpConstants.A_IMHeaderValues.IncrementalFeed);

            base.FillRequestOptions(request);
        }
        /// <summary>
        /// Fill the CosmosRequestMessage headers with the set properties
        /// </summary>
        /// <param name="request">The <see cref="CosmosRequestMessage"/></param>
        public override void FillRequestOptions(CosmosRequestMessage request)
        {
            request.Headers.Add(HttpConstants.HttpHeaders.ContentType, MediaTypes.QueryJson);
            request.Headers.Add(HttpConstants.HttpHeaders.IsQuery, bool.TrueString);
            request.Headers.Add(HttpConstants.HttpHeaders.EnableCrossPartitionQuery, this.EnableCrossPartitionQuery ? bool.TrueString : bool.FalseString);

            CosmosRequestOptions.SetSessionToken(request, this.SessionToken);
            CosmosRequestOptions.SetConsistencyLevel(request, this.ConsistencyLevel);

            // Flow the pageSize only when we are not doing client eval
            if (this.MaxItemCount.HasValue)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.PageSize, this.MaxItemCount.ToString());
            }

            if (this.MaxConcurrency.HasValue && this.MaxConcurrency > 0)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.ParallelizeCrossPartitionQuery, bool.TrueString);
            }

            if (this.EnableScanInQuery.HasValue && this.EnableScanInQuery.Value)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.EnableScanInQuery, bool.TrueString);
            }

            if (this.EnableLowPrecisionOrderBy != null)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.EnableLowPrecisionOrderBy, this.EnableLowPrecisionOrderBy.ToString());
            }

            if (this.ResponseContinuationTokenLimitInKb != null)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.ResponseContinuationTokenLimitInKB, this.ResponseContinuationTokenLimitInKb.ToString());
            }

            if (this.CosmosSerializationOptions != null)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.ContentSerializationFormat, this.CosmosSerializationOptions.ContentSerializationFormat);
            }

            base.FillRequestOptions(request);
        }
Exemplo n.º 23
0
        internal static async Task <T> ProcessResourceOperationAsync <T>(
            CosmosRequestHandler requestHandler,
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            CosmosRequestOptions requestOptions,
            CosmosContainerCore cosmosContainerCore,
            Object partitionKey,
            Stream streamPayload,
            Action <CosmosRequestMessage> requestEnricher,
            Func <CosmosResponseMessage, T> responseCreator,
            CancellationToken cancellationToken)
        {
            if (requestHandler == null)
            {
                throw new ArgumentException(nameof(requestHandler));
            }

            if (resourceUri == null)
            {
                throw new ArgumentNullException(nameof(resourceUri));
            }

            if (responseCreator == null)
            {
                throw new ArgumentNullException(nameof(responseCreator));
            }

            CosmosRequestMessage request = await ExecUtils.GenerateCosmosRequestMessage(
                resourceUri,
                resourceType,
                operationType,
                requestOptions,
                cosmosContainerCore,
                partitionKey,
                streamPayload,
                requestEnricher);

            CosmosResponseMessage response = await requestHandler.SendAsync(request, cancellationToken);

            return(responseCreator(response));
        }
Exemplo n.º 24
0
        /// <summary>
        /// Fill the CosmosRequestMessage headers with the set properties
        /// </summary>
        /// <param name="request">The <see cref="CosmosRequestMessage"/></param>
        public virtual void FillRequestOptions(CosmosRequestMessage request)
        {
            if (this.Properties != null)
            {
                foreach (KeyValuePair <string, object> property in this.Properties)
                {
                    request.Properties[property.Key] = property.Value;
                }
            }

            if (this.IfMatchEtag != null)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.IfMatch, this.IfMatchEtag);
            }

            if (this.IfNoneMatchEtag != null)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.IfNoneMatch, this.IfNoneMatchEtag);
            }
        }
 private QueryResponse(
     IEnumerable <CosmosElement> result,
     int count,
     long responseLengthBytes,
     CosmosQueryResponseMessageHeaders responseHeaders,
     HttpStatusCode statusCode,
     CosmosRequestMessage requestMessage,
     string errorMessage,
     Error error)
     : base(
         statusCode: statusCode,
         requestMessage: requestMessage,
         errorMessage: errorMessage,
         error: error,
         headers: responseHeaders)
 {
     this.CosmosElements      = result;
     this.Count               = count;
     this.ResponseLengthBytes = responseLengthBytes;
 }
Exemplo n.º 26
0
            public override async Task <CosmosResponseMessage> SendAsync(
                CosmosRequestMessage request,
                CancellationToken cancellationToken)
            {
                this.ProcessMessagesAsyncThrew = false;
                this.SendAsyncCalls++;
                try
                {
                    using (new ActivityScope(Guid.NewGuid()))
                    {
                        DocumentServiceResponse response = await base.ProcessMessageAsync(request, cancellationToken);

                        return(response.ToCosmosResponseMessage(request));
                    }
                }
                catch (DocumentClientException)
                {
                    this.ProcessMessagesAsyncThrew = true;
                    throw;
                }
            }
Exemplo n.º 27
0
        internal static Task <CosmosResponseMessage> ProcessResourceOperationStreamAsync(
            CosmosClient client,
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            CosmosRequestOptions requestOptions,
            Object partitionKey,
            Stream streamPayload,
            Action <CosmosRequestMessage> requestEnricher,
            CancellationToken cancellationToken)
        {
            CosmosRequestMessage request = ExecUtils.GenerateCosmosRequestMessage(
                resourceUri,
                resourceType,
                operationType,
                requestOptions,
                partitionKey,
                streamPayload,
                requestEnricher);

            return(client.RequestHandler.SendAsync(request, cancellationToken));
        }
Exemplo n.º 28
0
        private static CosmosRequestMessage GenerateCosmosRequestMessage(
            CosmosClient client,
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            CosmosRequestOptions requestOptions,
            Object partitionKey,
            Stream streamPayload,
            Action <CosmosRequestMessage> requestEnricher)
        {
            HttpMethod method = ExecUtils.GetHttpMethod(operationType);

            CosmosRequestMessage request = new CosmosRequestMessage(method, resourceUri);

            request.OperationType  = operationType;
            request.ResourceType   = resourceType;
            request.RequestOptions = requestOptions;
            request.Content        = streamPayload;

            if (partitionKey != null)
            {
                PartitionKey pk = new PartitionKey(partitionKey);
                request.Headers.PartitionKey = pk.InternalKey.ToJsonString();
            }

            if (client.DocumentClient.UseMultipleWriteLocations)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.AllowTentativeWrites, bool.TrueString);
            }

            if (operationType == OperationType.Upsert)
            {
                request.Headers.IsUpsert = bool.TrueString;
            }

            requestEnricher?.Invoke(request);

            return(request);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Fill the CosmosRequestMessage headers with the set properties
        /// </summary>
        /// <param name="request">The <see cref="CosmosRequestMessage"/></param>
        public override void FillRequestOptions(CosmosRequestMessage request)
        {
            if (PreTriggers != null && PreTriggers.Any())
            {
                request.Headers.Add(HttpConstants.HttpHeaders.PreTriggerInclude, this.PreTriggers);
            }

            if (PostTriggers != null && PostTriggers.Any())
            {
                request.Headers.Add(HttpConstants.HttpHeaders.PostTriggerInclude, this.PostTriggers);
            }

            if (this.IndexingDirective != null && this.IndexingDirective.HasValue)
            {
                request.Headers.Add(HttpConstants.HttpHeaders.IndexingDirective, this.IndexingDirective.Value.ToString());
            }

            CosmosRequestOptions.SetSessionToken(request, this.SessionToken);
            CosmosRequestOptions.SetConsistencyLevel(request, this.ConsistencyLevel);

            base.FillRequestOptions(request);
        }
Exemplo n.º 30
0
 public override Task <CosmosResponseMessage> SendAsync(
     CosmosRequestMessage request, CancellationToken cancellationToken)
 {
     return(_handlerFunc(request, cancellationToken));
 }