public override async Task StartBatchAsync()
        {
            _requestMessage = new ODataRequestMessage()
            {
                Url = new Uri(_session.Settings.UrlBase)
            };
            _messageWriter = new ODataMessageWriter(_requestMessage);
            _batchWriter   = await _messageWriter.CreateODataBatchWriterAsync();

            await _batchWriter.WriteStartBatchAsync();
        }
Exemple #2
0
        protected override async Task <Stream> WriteStreamContentAsync(Stream stream, bool writeAsText)
        {
            var message = new ODataRequestMessage();

            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(ODataFormat.RawValue), _model))
            {
                var value = writeAsText ? (object)Utils.StreamToString(stream) : Utils.StreamToByteArray(stream);
                await messageWriter.WriteValueAsync(value);

                return(await message.GetStreamAsync());
            }
        }
        public override async Task StartBatchAsync()
        {
            _requestMessage = new ODataRequestMessage()
            {
                Url = _session.Settings.BaseUri
            };
            _messageWriter = new ODataMessageWriter(_requestMessage);
            _batchWriter   = await _messageWriter.CreateODataBatchWriterAsync();

            await _batchWriter.WriteStartBatchAsync();

            this.HasOperations = true;
        }
        protected override async Task <Stream> WriteLinkContentAsync(string linkIdent)
        {
            var message = new ODataRequestMessage();

            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
            {
                var link = new ODataEntityReferenceLink {
                    Url = Utils.CreateAbsoluteUri(_session.Settings.UrlBase, linkIdent)
                };
                await messageWriter.WriteEntityReferenceLinkAsync(link);

                return(await message.GetStreamAsync());
            }
        }
        protected override async Task <Stream> WriteActionContentAsync(string actionName, IDictionary <string, object> parameters)
        {
            var message = new ODataRequestMessage();

            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
            {
                var action = _model.SchemaElements.BestMatch(
                    x => x.SchemaElementKind == EdmSchemaElementKind.Action,
                    x => x.Name, actionName, _session.Pluralizer);
                var parameterWriter = await messageWriter.CreateODataParameterWriterAsync(action as IEdmAction);

                await parameterWriter.WriteStartAsync();

                foreach (var parameter in parameters)
                {
                    if (!(parameter.Value is string) && parameter.Value is IEnumerable)
                    {
                        var collectionWriter = await parameterWriter.CreateCollectionWriterAsync(parameter.Key);

                        await collectionWriter.WriteStartAsync(new ODataCollectionStart());

                        foreach (var item in parameter.Value as IEnumerable)
                        {
                            await collectionWriter.WriteItemAsync(item);
                        }
                        await collectionWriter.WriteEndAsync();
                    }
                    else
                    {
                        await parameterWriter.WriteValueAsync(parameter.Key, parameter.Value);
                    }
                }

                await parameterWriter.WriteEndAsync();

                return(await message.GetStreamAsync());
            }
        }
        protected override async Task <Stream> WriteActionContentAsync(string actionName, IDictionary <string, object> parameters)
        {
            var message = new ODataRequestMessage();

            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model))
            {
                var action = _model.SchemaElements.BestMatch(
                    x => x.SchemaElementKind == EdmSchemaElementKind.Action,
                    x => x.Name, actionName, _session.Pluralizer) as IEdmAction;
                var parameterWriter = await messageWriter.CreateODataParameterWriterAsync(action);

                await parameterWriter.WriteStartAsync();

                foreach (var parameter in parameters)
                {
                    var actionParameter = action.Parameters.BestMatch(x => x.Name, parameter.Key, _session.Pluralizer);
                    if (actionParameter == null)
                    {
                        throw new UnresolvableObjectException(parameter.Key, string.Format("Parameter [{0}] not found for action [{1}]", parameter.Key, actionName));
                    }

                    if (actionParameter.Type.Definition.TypeKind == EdmTypeKind.Collection)
                    {
                        var collectionType = actionParameter.Type.Definition as IEdmCollectionType;
                        var elementType    = collectionType.ElementType;
                        if (elementType.Definition.TypeKind == EdmTypeKind.Entity)
                        {
                            var feedWriter = await parameterWriter.CreateFeedWriterAsync(parameter.Key);

                            var feed = new ODataFeed();
                            await feedWriter.WriteStartAsync(feed);

                            foreach (var item in parameter.Value as IEnumerable)
                            {
                                var entry = CreateODataEntry(elementType.Definition.FullTypeName(), item.ToDictionary());

                                await feedWriter.WriteStartAsync(entry);

                                await feedWriter.WriteEndAsync();
                            }
                            await feedWriter.WriteEndAsync();
                        }
                        else
                        {
                            var collectionWriter = await parameterWriter.CreateCollectionWriterAsync(parameter.Key);

                            await collectionWriter.WriteStartAsync(new ODataCollectionStart());

                            foreach (var item in parameter.Value as IEnumerable)
                            {
                                await collectionWriter.WriteItemAsync(item);
                            }
                            await collectionWriter.WriteEndAsync();
                        }
                    }
                    else
                    {
                        await parameterWriter.WriteValueAsync(parameter.Key, parameter.Value);
                    }
                }

                await parameterWriter.WriteEndAsync();

                return(await message.GetStreamAsync());
            }
        }