Exemplo n.º 1
0
        internal void Init(
            CosmosClientConfiguration configuration,
            DocumentClient documentClient)
        {
            this.Configuration        = configuration;
            this.DocumentClient       = documentClient;
            this.CosmosJsonSerializer = new CosmosJsonSerializerWrapper(this.Configuration.CosmosJsonSerializer);

            //Request pipeline
            ClientPipelineBuilder clientPipelineBuilder = new ClientPipelineBuilder(
                this,
                this.DocumentClient.ResetSessionTokenRetryPolicy,
                this.Configuration.CustomHandlers
                );

            // DocumentClient is not initialized with any consistency overrides so default is backend consistency
            this.AccountConsistencyLevel = (ConsistencyLevel)this.DocumentClient.ConsistencyLevel;

            this.RequestHandler = clientPipelineBuilder.Build();

            CosmosClientContext clientContext = new CosmosClientContextCore(
                this,
                this.Configuration,
                this.CosmosJsonSerializer,
                this.ResponseFactory,
                this.RequestHandler,
                this.DocumentClient,
                new DocumentQueryClient(this.DocumentClient));

            this.Databases = new CosmosDatabasesCore(clientContext);
            this.offerSet  = new Lazy <CosmosOffers>(() => new CosmosOffers(this.DocumentClient), LazyThreadSafetyMode.PublicationOnly);
        }
 internal CosmosResponseFactory(
     CosmosJsonSerializer defaultJsonSerializer,
     CosmosJsonSerializer userJsonSerializer)
 {
     this.settingsSerializer = defaultJsonSerializer;
     this.cosmosSerializer   = userJsonSerializer;
 }
 /// <summary>
 /// Create the cosmos item response.
 /// Creates the response object, deserializes the
 /// HTTP content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos JSON serializer</param>
 internal static CosmosItemResponse <CustomResponseType> CreateResponse <CustomResponseType>(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer)
 {
     return(CosmosResponse <CosmosContainerSettings>
            .InitResponse <CosmosItemResponse <CustomResponseType>, CustomResponseType>(
                (httpResponse) => new CosmosItemResponse <CustomResponseType>(cosmosResponseMessage),
                jsonSerializer,
                cosmosResponseMessage));
 }
 /// <summary>
 /// Create the cosmos stored procedure response.
 /// Creates the response object, deserializes the
 /// http content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos json serializer</param>
 /// <param name="storedProcedure">The cosmos stored procedure</param>
 internal static CosmosStoredProcedureResponse CreateResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer,
     CosmosStoredProcedure storedProcedure)
 {
     return(CosmosResponse <CosmosStoredProcedureSettings>
            .InitResponse <CosmosStoredProcedureResponse, CosmosStoredProcedureSettings>(
                (httpResponse) => new CosmosStoredProcedureResponse(httpResponse, storedProcedure),
                jsonSerializer,
                cosmosResponseMessage));
 }
Exemplo n.º 5
0
 /// <summary>
 /// Create the cosmos user defined function response.
 /// Creates the response object, deserializes the
 /// HTTP content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos json serializer</param>
 /// <param name="userDefinedFunction">The cosmos user defined function</param>
 internal static CosmosUserDefinedFunctionResponse CreateResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer,
     CosmosUserDefinedFunction userDefinedFunction)
 {
     return(CosmosResponse <CosmosUserDefinedFunctionSettings>
            .InitResponse <CosmosUserDefinedFunctionResponse, CosmosUserDefinedFunctionSettings>(
                (httpResponse) => new CosmosUserDefinedFunctionResponse(cosmosResponseMessage, userDefinedFunction),
                jsonSerializer,
                cosmosResponseMessage));
 }
 /// <summary>
 /// Create the cosmos database response.
 /// Creates the response object, deserializes the
 /// http content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos json serializer</param>
 /// <param name="database">The cosmos database</param>
 internal static CosmosDatabaseResponse CreateResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer,
     CosmosDatabase database)
 {
     return(CosmosResponse <CosmosDatabaseSettings>
            .InitResponse <CosmosDatabaseResponse, CosmosDatabaseSettings>(
                (httpResponse) => new CosmosDatabaseResponse(cosmosResponseMessage, database),
                jsonSerializer,
                cosmosResponseMessage));
 }
 /// <summary>
 /// Create the cosmos trigger response.
 /// Creates the response object, deserializes the
 /// http content stream, and disposes of the HttpResponseMessage
 /// </summary>
 /// <param name="cosmosResponseMessage"><see cref="CosmosResponseMessage"/> from the Cosmos DB service</param>
 /// <param name="jsonSerializer">The cosmos json serializer</param>
 /// <param name="trigger">The cosmos trigger</param>
 internal static CosmosTriggerResponse CreateResponse(
     CosmosResponseMessage cosmosResponseMessage,
     CosmosJsonSerializer jsonSerializer,
     CosmosTrigger trigger)
 {
     return(CosmosResponse <CosmosTriggerSettings>
            .InitResponse <CosmosTriggerResponse, CosmosTriggerSettings>(
                (httpResponse) => new CosmosTriggerResponse(cosmosResponseMessage, trigger),
                jsonSerializer,
                cosmosResponseMessage));
 }
        /// <summary>
        /// DEVNOTE: Need to refactor to use CosmosJsonSerializer
        /// </summary>
        public static FeedResponse <T> ConvertCosmosElementFeed <T>(
            FeedResponse <CosmosElement> dynamicFeed,
            ResourceType resourceType,
            CosmosJsonSerializer jsonSerializer)
        {
            if (dynamicFeed.Count == 0)
            {
                return(new FeedResponse <T>(
                           new List <T>(),
                           dynamicFeed.Count,
                           dynamicFeed.Headers,
                           dynamicFeed.UseETagAsContinuation,
                           dynamicFeed.QueryMetrics,
                           dynamicFeed.RequestStatistics,
                           dynamicFeed.DisallowContinuationTokenMessage,
                           dynamicFeed.ResponseLengthBytes));
            }

            IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Text);

            jsonWriter.WriteArrayStart();

            foreach (CosmosElement cosmosElement in dynamicFeed)
            {
                cosmosElement.WriteTo(jsonWriter);
            }

            jsonWriter.WriteArrayEnd();
            MemoryStream    stream = new MemoryStream(jsonWriter.GetResult());
            IEnumerable <T> typedResults;

            // If the resource type is an offer and the requested type is either a Offer or OfferV2 or dynamic
            // create a OfferV2 object and cast it to T. This is a temporary fix until offers is moved to v3 API.
            if (resourceType == ResourceType.Offer &&
                (typeof(T).IsSubclassOf(typeof(Resource)) || typeof(T) == typeof(object)))
            {
                typedResults = jsonSerializer.FromStream <List <OfferV2> >(stream).Cast <T>();
            }
            else
            {
                typedResults = jsonSerializer.FromStream <List <T> >(stream);
            }

            return(new FeedResponse <T>(
                       typedResults,
                       dynamicFeed.Count,
                       dynamicFeed.Headers,
                       dynamicFeed.UseETagAsContinuation,
                       dynamicFeed.QueryMetrics,
                       dynamicFeed.RequestStatistics,
                       dynamicFeed.DisallowContinuationTokenMessage,
                       dynamicFeed.ResponseLengthBytes));
        }
 /// <summary>
 /// Create the cosmos response using the custom JSON parser.
 /// This factory exist to ensure that the HttpResponseMessage is
 /// correctly disposed of, and to handle the async logic required to
 /// read the stream and to convert it to an object.
 /// </summary>
 internal static CosmosResponseType InitResponse <CosmosResponseType, CosmosSettingsType>(
     Func <CosmosResponseMessage, CosmosResponseType> createCosmosResponse,
     CosmosJsonSerializer jsonSerializer,
     CosmosResponseMessage cosmosResponseMessage,
     CosmosIdentifier cosmosSetResource = null)
     where CosmosResponseType : CosmosResponse <CosmosSettingsType>
 {
     using (cosmosResponseMessage)
     {
         CosmosResponseType cosmosResponse = createCosmosResponse(cosmosResponseMessage);
         cosmosResponse.InitializeResource(cosmosResponseMessage, jsonSerializer);
         return(cosmosResponse);
     }
 }
 internal CosmosClientContextCore(
     CosmosClient client,
     CosmosClientConfiguration clientConfiguration,
     CosmosJsonSerializer cosmosJsonSerializer,
     CosmosResponseFactory cosmosResponseFactory,
     CosmosRequestHandler requestHandler,
     DocumentClient documentClient,
     IDocumentQueryClient documentQueryClient)
 {
     this.Client = client;
     this.ClientConfiguration = clientConfiguration;
     this.JsonSerializer      = cosmosJsonSerializer;
     this.ResponseFactory     = cosmosResponseFactory;
     this.RequestHandler      = requestHandler;
     this.DocumentClient      = documentClient;
     this.DocumentQueryClient = documentQueryClient;
 }
Exemplo n.º 11
0
        internal static CosmosQueryResponse <TInput> CreateResponse <TInput>(
            Stream stream,
            CosmosJsonSerializer jsonSerializer,
            string continuationToken,
            bool hasMoreResults)
        {
            using (stream)
            {
                CosmosQueryResponse <TInput> queryResponse = new CosmosQueryResponse <TInput>()
                {
                    ContinuationToken = continuationToken,
                    HasMoreResults    = hasMoreResults
                };

                queryResponse.InitializeResource(stream, jsonSerializer);
                return(queryResponse);
            }
        }
        internal static ReadFeedResponse <TInput> CreateResponse <TInput>(
            CosmosResponseMessageHeaders responseMessageHeaders,
            Stream stream,
            CosmosJsonSerializer jsonSerializer,
            bool hasMoreResults)
        {
            using (stream)
            {
                CosmosFeedResponseUtil <TInput> response         = jsonSerializer.FromStream <CosmosFeedResponseUtil <TInput> >(stream);
                IEnumerable <TInput>            resources        = response.Data;
                ReadFeedResponse <TInput>       readFeedResponse = new ReadFeedResponse <TInput>(
                    resource: resources,
                    responseMessageHeaders: responseMessageHeaders,
                    hasMoreResults: hasMoreResults);

                return(readFeedResponse);
            }
        }
Exemplo n.º 13
0
        internal static CosmosQueryResponse <T> CreateCosmosQueryResponse(
            CosmosResponseMessage cosmosResponseMessage,
            CosmosJsonSerializer jsonSerializer)
        {
            using (cosmosResponseMessage)
            {
                // Throw the exception if the query failed.
                cosmosResponseMessage.EnsureSuccessStatusCode();

                string continuationToken = CosmosFeedResultSetIteratorCore.GetContinuationToken(cosmosResponseMessage);
                bool   hasMoreResults    = CosmosFeedResultSetIteratorCore.GetHasMoreResults(continuationToken, cosmosResponseMessage.StatusCode);

                return(CosmosQueryResponse <T> .CreateResponse <T>(
                           stream : cosmosResponseMessage.Content,
                           jsonSerializer : jsonSerializer,
                           continuationToken : continuationToken,
                           hasMoreResults : hasMoreResults));
            }
        }
 internal CosmosClientContextCore(
     CosmosClient client,
     CosmosClientOptions clientOptions,
     CosmosJsonSerializer userJsonSerializer,
     CosmosJsonSerializer defaultJsonSerializer,
     CosmosResponseFactory cosmosResponseFactory,
     RequestInvokerHandler requestHandler,
     DocumentClient documentClient,
     IDocumentQueryClient documentQueryClient)
 {
     this.Client              = client;
     this.ClientOptions       = clientOptions;
     this.CosmosSerializer    = userJsonSerializer;
     this.SettingsSerializer  = defaultJsonSerializer;
     this.ResponseFactory     = cosmosResponseFactory;
     this.RequestHandler      = requestHandler;
     this.DocumentClient      = documentClient;
     this.DocumentQueryClient = documentQueryClient;
 }
        internal T ToObjectInternal <T>(CosmosResponseMessage cosmosResponseMessage, CosmosJsonSerializer jsonSerializer)
        {
            // Not finding something is part of a normal work-flow and should not be an exception.
            // This prevents the unnecessary overhead of an exception
            if (cosmosResponseMessage.StatusCode == HttpStatusCode.NotFound)
            {
                return(default(T));
            }

            //Throw the exception
            cosmosResponseMessage.EnsureSuccessStatusCode();

            if (cosmosResponseMessage.Content == null)
            {
                return(default(T));
            }

            return(jsonSerializer.FromStream <T>(cosmosResponseMessage.Content));
        }
        internal static CosmosQueryResponse <TInput> CreateCosmosQueryFeedResponse <TInput>(
            CosmosResponseMessage cosmosResponseMessage,
            CosmosJsonSerializer jsonSerializer)
        {
            using (cosmosResponseMessage)
            {
                // Throw the exception if the query failed: EnsureSuccessStatusCode only
                // validates 200-299. 304 is valid for Changefeed so do not throw on that.
                if (cosmosResponseMessage.StatusCode != HttpStatusCode.NotModified)
                {
                    cosmosResponseMessage.EnsureSuccessStatusCode();
                }

                string continuationToken = cosmosResponseMessage.Headers.ETag;
                bool   hasMoreResults    = ChangeFeedResultSetStreamIterator.GetHasMoreResults(continuationToken, cosmosResponseMessage.StatusCode);

                return(CosmosQueryResponse <TInput> .CreateResponse <TInput>(
                           stream : cosmosResponseMessage.Content,
                           jsonSerializer : jsonSerializer,
                           continuationToken : continuationToken,
                           hasMoreResults : hasMoreResults));
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Gets the content of the Conflict resource in the Azure Cosmos DB service.
        /// </summary>
        /// <typeparam name="T">The type to use to deserialize the content.</typeparam>
        /// <param name="cosmosJsonSerializer">(Optional) <see cref="CosmosJsonSerializer"/> to use while parsing the content.</param>
        /// <returns>An instance of T</returns>
        public virtual T GetResource <T>(CosmosJsonSerializer cosmosJsonSerializer = null)
        {
            if (!string.IsNullOrEmpty(this.Content))
            {
                if (cosmosJsonSerializer == null)
                {
                    cosmosJsonSerializer = new CosmosJsonSerializerCore();
                }

                using (MemoryStream stream = new MemoryStream())
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.Write(this.Content);
                        writer.Flush();
                        stream.Position = 0;
                        return(cosmosJsonSerializer.FromStream <T>(stream));
                    }
                }
            }

            return(default(T));
        }
Exemplo n.º 18
0
 /// <summary>
 /// Set a custom JSON serializer.
 /// </summary>
 /// <param name="cosmosJsonSerializer">The custom class that implements <see cref="CosmosJsonSerializer"/> </param>
 /// <returns>The <see cref="CosmosClientBuilder"/> object</returns>
 /// <seealso cref="CosmosJsonSerializer"/>
 /// <seealso cref="CosmosClientConfiguration.CosmosJsonSerializer"/>
 public virtual CosmosClientBuilder UseCustomJsonSerializer(
     CosmosJsonSerializer cosmosJsonSerializer)
 {
     this.cosmosClientConfiguration.CosmosJsonSerializer = cosmosJsonSerializer;
     return(this);
 }
 internal CosmosResponseFactory(CosmosJsonSerializer cosmosJsonSerializer)
 {
     this.jsonSerializer = cosmosJsonSerializer;
 }
Exemplo n.º 20
0
 public CosmosJsonSerializerWrapper(CosmosJsonSerializer cosmosJsonSerializer)
 {
     this.internalJsonSerializer = cosmosJsonSerializer ?? throw new ArgumentNullException(nameof(cosmosJsonSerializer));
 }
Exemplo n.º 21
0
 private void InitializeResource(
     Stream stream,
     CosmosJsonSerializer jsonSerializer)
 {
     this.Resources = jsonSerializer.FromStream <CosmosFeedResponse <T> >(stream).Data;
 }
Exemplo n.º 22
0
 /// <summary>
 /// Gets the stream body and converts it to the typed passed in.
 /// This can throw JSON serialization exceptions
 /// </summary>
 /// <param name="cosmosResponseMessage">The response from the Cosmos service</param>
 /// <param name="cosmosJsonSerializer">The JSON serializer</param>
 private void InitializeResource(CosmosResponseMessage cosmosResponseMessage, CosmosJsonSerializer cosmosJsonSerializer)
 {
     this.Resource = ToObjectInternal(cosmosResponseMessage, cosmosJsonSerializer);
 }