Esempio n. 1
0
        public IDictionary <string, object> InsertEntry(string tableName, IDictionary <string, object> entryData, bool resultRequired)
        {
            var entryMembers = ParseEntryMembers(tableName, entryData);

            var entry = ODataHelper.CreateDataElement(entryMembers.Properties);

            foreach (var associatedData in entryMembers.AssociationsByValue)
            {
                CreateLinkElement(entry, tableName, associatedData);
            }

            var commandText = GetTableActualName(tableName);
            var command     = HttpCommand.Post(commandText, entryData, entry.ToString());

            _requestBuilder.AddCommandToRequest(command);
            var result = _requestRunner.InsertEntry(command, resultRequired);

            foreach (var associatedData in entryMembers.AssociationsByContentId)
            {
                var linkCommand = CreateLinkCommand(tableName, associatedData.Key,
                                                    ODataHelper.CreateLinkPath(command.ContentId),
                                                    ODataHelper.CreateLinkPath(associatedData.Value));
                _requestBuilder.AddCommandToRequest(linkCommand);
                _requestRunner.InsertEntry(linkCommand, resultRequired);
            }

            return(result);
        }
Esempio n. 2
0
        private int UpdateEntryPropertiesAndAssociations(string tableName, IDictionary <string, object> entryKey, IDictionary <string, object> entryData, EntryMembers entryMembers)
        {
            bool merge       = CheckMergeConditions(tableName, entryKey, entryData);
            var  commandText = FormatGetKeyCommand(tableName, entryKey);

            var entryElement = ODataHelper.CreateDataElement(entryMembers.Properties);

            foreach (var associatedData in entryMembers.AssociationsByValue)
            {
                CreateLinkElement(entryElement, tableName, associatedData);
            }

            var command = new HttpCommand(merge ? RestVerbs.MERGE : RestVerbs.PUT, commandText, entryData, entryElement.ToString());

            _requestBuilder.AddCommandToRequest(command);
            var result = _requestRunner.UpdateEntry(command);

            foreach (var associatedData in entryMembers.AssociationsByContentId)
            {
                var linkCommand = CreateLinkCommand(tableName, associatedData.Key,
                                                    ODataHelper.CreateLinkPath(command.ContentId),
                                                    ODataHelper.CreateLinkPath(associatedData.Value));
                _requestBuilder.AddCommandToRequest(linkCommand);
                _requestRunner.UpdateEntry(linkCommand);
            }

            return(result);
        }
Esempio n. 3
0
        public override IEnumerable <IDictionary <string, object> > FindEntries(HttpCommand command, bool scalarResult, bool setTotalCount, out int totalCount)
        {
            using (var response = TryRequest(command.Request))
            {
                totalCount = 0;
                IEnumerable <IDictionary <string, object> > result = null;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    result = Enumerable.Empty <IDictionary <string, object> >();
                }
                else
                {
                    var stream = response.GetResponseStream();
                    if (setTotalCount)
                    {
                        result = ODataHelper.GetData(stream, out totalCount);
                    }
                    else
                    {
                        result = ODataHelper.GetData(response.GetResponseStream(), scalarResult);
                    }
                }

                return(result);
            }
        }
        public override IEnumerable<IDictionary<string, object>> FindEntries(HttpCommand command, bool scalarResult, bool setTotalCount, out int totalCount)
        {
            totalCount = 0;
            try
            {
                using (var response = ExecuteRequest(command.Request))
                {
                    IEnumerable<IDictionary<string, object>> result = null;
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        result = Enumerable.Empty<IDictionary<string, object>>();
                    }
                    else
                    {
                        var stream = response.GetResponseStream();
                        if (setTotalCount)
                            result = _feedReader.GetData(stream, out totalCount);
                        else
                            result = _feedReader.GetData(response.GetResponseStream(), scalarResult);
                    }

                    return result;
                }
            }
            catch (WebRequestException ex)
            {
                if (_ignoreResourceNotFoundException && IsResourceNotFoundException(ex))
                    return new[] { (IDictionary<string, object>)null };
                else
                    throw;
            }
        }
        public override HttpRequest CreateRequest(HttpCommand command, bool returnContent = false)
        {
            var uri = CreateRequestUrl(command.CommandText);
            var request = CreateRequest(uri);
            request.Method = command.Method;
            request.ReturnContent = returnContent;

            // TODO: revise
            //if (method == "PUT" || method == "DELETE" || method == "MERGE")
            //{
            //    request.Headers.Add("If-Match", "*");
            //}

            if (command.FormattedContent != null)
            {
                request.ContentType = command.ContentType;
                request.Content = new StringContent(command.FormattedContent, Encoding.UTF8, command.ContentType);
            }

            if (request.Method == RestVerbs.GET && !command.ReturnsScalarResult || request.ReturnContent)
            {
                request.Accept = new[] { "application/text", "application/xml", "application/atom+xml" };
            }

            return request;
        }
Esempio n. 6
0
        public IEnumerable <IDictionary <string, object> > FindEntries(string commandText, bool scalarResult, bool setTotalCount, out int totalCount)
        {
            var command = HttpCommand.Get(commandText);

            _requestBuilder.AddCommandToRequest(command);
            return(_requestRunner.FindEntries(command, scalarResult, setTotalCount, out totalCount));
        }
        public override void AddCommandToRequest(HttpCommand command)
        {
            _contentBuilder.AppendLine(string.Format("--changeset_{0}", _changesetId));
            _contentBuilder.AppendLine("Content-Type: application/http");
            _contentBuilder.AppendLine("Content-Transfer-Encoding:binary");
            _contentBuilder.AppendLine();

            _contentBuilder.AppendLine(string.Format("{0} {1} HTTP/{2}", command.Method, command.IsLink ? command.CommandText : CreateRequestUrl(command.CommandText), "1.1"));

            if (command.FormattedContent != null)
            {
                _contentBuilder.AppendLine(string.Format("Content-ID: {0}", ++_contentId));
                _contentBuilder.AppendLine(string.Format("Content-Type: {0}", command.ContentType));
                _contentBuilder.AppendLine(string.Format("Content-Length: {0}", (command.FormattedContent ?? string.Empty).Length));
                _contentBuilder.AppendLine();
                _contentBuilder.Append(command.FormattedContent);
            }

            _contentBuilder.AppendLine();

            command.Request = this.Request;
            command.ContentId = _contentId;

            if (command.OriginalContent != null)
            {
                command.OriginalContent.Add("$Batch-ID", _batchId);
                command.OriginalContent.Add("$Content-ID", command.ContentId);
            }
        }
        public override void AddCommandToRequest(HttpCommand command)
        {
            _contentBuilder.AppendLine(string.Format("--changeset_{0}", _changesetId));
            _contentBuilder.AppendLine("Content-Type: application/http");
            _contentBuilder.AppendLine("Content-Transfer-Encoding:binary");
            _contentBuilder.AppendLine();

            _contentBuilder.AppendLine(string.Format("{0} {1} HTTP/{2}", command.Method, command.IsLink ? command.CommandText : CreateRequestUrl(command.CommandText), "1.1"));

            if (command.FormattedContent != null)
            {
                _contentBuilder.AppendLine(string.Format("Content-ID: {0}", ++_contentId));
                _contentBuilder.AppendLine(string.Format("Content-Type: {0}", command.ContentType));
                _contentBuilder.AppendLine(string.Format("Content-Length: {0}", (command.FormattedContent ?? string.Empty).Length));
                _contentBuilder.AppendLine();
                _contentBuilder.Append(command.FormattedContent);
            }

            _contentBuilder.AppendLine();

            command.Request   = this.Request;
            command.ContentId = _contentId;

            if (command.OriginalContent != null)
            {
                command.OriginalContent.Add("$Batch-ID", _batchId);
                command.OriginalContent.Add("$Content-ID", command.ContentId);
            }
        }
Esempio n. 9
0
        public int DeleteEntry(string tableName, IDictionary <string, object> entryKey)
        {
            var commandText = FormatGetKeyCommand(tableName, entryKey);
            var command     = HttpCommand.Delete(commandText);

            _requestBuilder.AddCommandToRequest(command);
            return(_requestRunner.DeleteEntry(command));
        }
Esempio n. 10
0
 public override int DeleteEntry(HttpCommand command)
 {
     using (var response = TryRequest(command.Request))
     {
         // TODO: check response code
         return(response.StatusCode == HttpStatusCode.OK ? 1 : 0);
     }
 }
 public override int UpdateEntry(HttpCommand command)
 {
     using (var response = ExecuteRequest(command.Request))
     {
         // TODO
         return response.StatusCode == HttpStatusCode.OK ? 1 : 0;
     }
 }
Esempio n. 12
0
        public IEnumerable <IEnumerable <IEnumerable <KeyValuePair <string, object> > > > ExecuteFunction(string functionName, IDictionary <string, object> parameters)
        {
            var function            = _schema.FindFunction(functionName);
            var formattedParameters = new ValueFormatter().Format(parameters, "&");
            var commandText         = function.ActualName + "?" + formattedParameters;
            var command             = new HttpCommand(function.HttpMethod.ToUpper(), commandText.ToString());

            _requestBuilder.AddCommandToRequest(command);
            return(_requestRunner.ExecuteFunction(command));
        }
 public override IDictionary<string, object> InsertEntry(HttpCommand command, bool resultRequired = true)
 {
     var text = ExecuteRequestAndGetResponse(command.Request);
     if (resultRequired)
     {
         return _feedReader.GetData(text).First();
     }
     else
     {
         return null;
     }
 }
Esempio n. 14
0
        public override IDictionary <string, object> InsertEntry(HttpCommand command, bool resultRequired)
        {
            var text = Request(command.Request);

            if (resultRequired)
            {
                return(ODataHelper.GetData(text).First());
            }
            else
            {
                return(null);
            }
        }
        private int UpdateEntryPropertiesAndAssociations(
            string collection,
            IDictionary<string, object> entryKey,
            IDictionary<string, object> entryData,
            EntryMembers entryMembers)
        {
            bool hasPropertiesToUpdate = entryMembers.Properties.Count > 0;
            bool merge = !hasPropertiesToUpdate || CheckMergeConditions(collection, entryKey, entryData);
            var commandText = GetFluentClient()
                .For(_schema.FindBaseTable(collection).ActualName)
                .Key(entryKey)
                .CommandText;

            var feedWriter = new ODataFeedWriter();
            var table = _schema.FindConcreteTable(collection);
            var entryElement = feedWriter.CreateDataElement(_schema.TypesNamespace, table.EntityType.Name, entryMembers.Properties);
            var unlinkAssociationNames = new List<string>();
            foreach (var associatedData in entryMembers.AssociationsByValue)
            {
                var association = table.FindAssociation(associatedData.Key);
                if (associatedData.Value != null)
                {
                    CreateLinkElement(entryElement, collection, associatedData);
                }
                else
                {
                    unlinkAssociationNames.Add(association.ActualName);
                }
            }

            var command = new HttpCommand(merge ? RestVerbs.MERGE : RestVerbs.PUT, commandText, entryData, entryElement.ToString());
            _requestBuilder.AddCommandToRequest(command);
            var result = _requestRunner.UpdateEntry(command);

            foreach (var associatedData in entryMembers.AssociationsByContentId)
            {
                var linkCommand = CreateLinkCommand(collection, associatedData.Key,
                    feedWriter.CreateLinkPath(command.ContentId),
                    feedWriter.CreateLinkPath(associatedData.Value));
                _requestBuilder.AddCommandToRequest(linkCommand);
                _requestRunner.UpdateEntry(linkCommand);
            }

            foreach (var associationName in unlinkAssociationNames)
            {
                UnlinkEntry(collection, entryKey, associationName);
            }

            return result;
        }
 public override IDictionary<string, object> GetEntry(HttpCommand command)
 {
     try
     {
         var text = ExecuteRequestAndGetResponse(command.Request);
         return _feedReader.GetData(text).First();
     }
     catch (WebRequestException ex)
     {
         if (_ignoreResourceNotFoundException && IsResourceNotFoundException(ex))
             return null;
         else
             throw;
     }
 }
Esempio n. 17
0
        private EdmSchema RequestMetadata()
        {
            var requestBuilder = new CommandRequestBuilder(_urlBase);
            var command        = HttpCommand.Get("$metadata");

            requestBuilder.AddCommandToRequest(command);
            using (var response = new CommandRequestRunner().TryRequest(command.Request))
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(ODataHelper.GetSchema(response.GetResponseStream()));
                }
            }
            // TODO
            return(null);
        }
        public override void AddCommandToRequest(HttpCommand command)
        {
            var uri = CreateRequestUrl(command.CommandText);
            var request = CreateWebRequest(uri);
            request.Method = command.Method;

            // TODO: revise
            //if (method == "PUT" || method == "DELETE" || method == "MERGE")
            //{
            //    request.Headers.Add("If-Match", "*");
            //}

            if (command.FormattedContent != null)
            {
                request.ContentType = command.ContentType;
                request.SetContent(command.FormattedContent);
            }

            command.Request = request;
        }
Esempio n. 19
0
        public override void AddCommandToRequest(HttpCommand command)
        {
            var uri = CreateRequestUrl(command.CommandText);
            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = command.Method;
            request.ContentLength = (command.FormattedContent ?? string.Empty).Length;

            // TODO: revise
            //if (method == "PUT" || method == "DELETE" || method == "MERGE")
            //{
            //    request.Headers.Add("If-Match", "*");
            //}

            if (command.FormattedContent != null)
            {
                request.ContentType = "application/atom+xml";
                request.SetContent(command.FormattedContent);
            }

            command.Request = request;
        }
        public override void AddCommandToRequest(HttpCommand command)
        {
            var uri     = CreateRequestUrl(command.CommandText);
            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method        = command.Method;
            request.ContentLength = (command.FormattedContent ?? string.Empty).Length;

            // TODO: revise
            //if (method == "PUT" || method == "DELETE" || method == "MERGE")
            //{
            //    request.Headers.Add("If-Match", "*");
            //}

            if (command.FormattedContent != null)
            {
                request.ContentType = command.ContentType;
                request.SetContent(command.FormattedContent);
            }

            command.Request = request;
        }
        public override HttpRequest CreateRequest(HttpCommand command, bool returnContent = false)
        {
            var request = new CommandRequestBuilder(this.UrlBase, this.Credentials).CreateRequest(command);
            var content = new StringContent(FormatBatchItem(command));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/http");
            content.Headers.Add("Content-Transfer-Encoding", "binary");

            var requestMessage = new HttpRequestMessage(new HttpMethod(request.Method), request.Uri);
            requestMessage.Content = content;
            if (requestMessage.Content != null)
            {
                _content.Add(requestMessage.Content);
            }

            request.EntryData = command.EntryData;
            if (request.EntryData != null)
            {
                request.EntryData.Add("$Batch-ID", _batchId);
                request.EntryData.Add("$Content-ID", _contentId);
            }
            command.ContentId = _contentId;

            return request;
        }
Esempio n. 22
0
 public abstract IEnumerable<IDictionary<string, object>> FindEntries(HttpCommand command, bool scalarResult, bool setTotalCount, out int totalCount);
Esempio n. 23
0
 public override int DeleteEntry(HttpCommand command)
 {
     return(0);
 }
        public override IEnumerable<IDictionary<string, object>> ExecuteFunction(HttpCommand command)
        {
            using (var response = ExecuteRequest(command.Request))
            {
                IEnumerable<IDictionary<string, object>> result = null;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    result = Enumerable.Empty<IDictionary<string, object>>();
                }
                else
                {
                    result = _feedReader.GetFunctionResult(response.GetResponseStream());
                }

                return result;
            }
        }
 public override IEnumerable<IEnumerable<IEnumerable<KeyValuePair<string, object>>>> ExecuteFunction(HttpCommand command)
 {
     return null;
 }
 public override IEnumerable<IDictionary<string, object>> ExecuteFunction(HttpCommand command)
 {
     return null;
 }
 public override IDictionary<string, object> GetEntry(HttpCommand command)
 {
     return null;
 }
 public override int UpdateEntry(HttpCommand command)
 {
     return 0;
 }
Esempio n. 29
0
 public override IEnumerable <IDictionary <string, object> > FindEntries(HttpCommand command, bool scalarResult, bool setTotalCount, out int totalCount)
 {
     totalCount = 0;
     return(null);
 }
Esempio n. 30
0
        public int UpdateEntry(string tableName, IDictionary<string, object> entryKey, IDictionary<string, object> entryData)
        {
            bool merge = CheckMergeConditions(tableName, entryKey, entryData);
            var commandText = FormatGetKeyCommand(tableName, entryKey);

            var entryMembers = ParseEntryMembers(tableName, entryData);
            var entryElement = ODataHelper.CreateDataElement(entryMembers.Properties);
            foreach (var association in entryMembers.AssociationsByValue)
            {
                CreateLinkElement(entryElement, tableName, association);
            }

            var command = new HttpCommand(merge ? RestVerbs.MERGE : RestVerbs.PUT, commandText, entryData, entryElement.ToString());
            _requestBuilder.AddCommandToRequest(command);
            var result = _requestRunner.UpdateEntry(command);

            foreach (var association in entryMembers.AssociationsByContentId)
            {
                var linkCommand = CreateLinkCommand(tableName, association.Key, command.ContentId, association.Value);
                _requestBuilder.AddCommandToRequest(linkCommand);
                _requestRunner.UpdateEntry(linkCommand);
            }

            return result;
        }
Esempio n. 31
0
 public abstract IDictionary<string, object> InsertEntry(HttpCommand command, bool resultRequired);
Esempio n. 32
0
 public abstract int DeleteEntry(HttpCommand command);
Esempio n. 33
0
        private HttpCommand CreateLinkCommand(string tableName, string associationName, string entryPath)
        {
            var commandText = string.Format("{0}/$links/{1}", entryPath, associationName);

            return(HttpCommand.Delete(commandText));
        }
Esempio n. 34
0
        public override IDictionary <string, object> GetEntry(HttpCommand command)
        {
            var text = Request(command.Request);

            return(ODataHelper.GetData(text).First());
        }
Esempio n. 35
0
        public override IEnumerable <IEnumerable <IEnumerable <KeyValuePair <string, object> > > > ExecuteFunction(HttpCommand command)
        {
            using (var response = TryRequest(command.Request))
            {
                IEnumerable <IEnumerable <IEnumerable <KeyValuePair <string, object> > > > result = null;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    result = Enumerable.Empty <IEnumerable <IDictionary <string, object> > >();
                }
                else
                {
                    var stream = response.GetResponseStream();
                    result = new[] { ODataHelper.GetData(response.GetResponseStream(), false) };
                }

                return(result);
            }
        }
Esempio n. 36
0
 public abstract void AddCommandToRequest(HttpCommand command);
Esempio n. 37
0
 public IEnumerable<IEnumerable<IEnumerable<KeyValuePair<string, object>>>> ExecuteFunction(string functionName, IDictionary<string, object> parameters)
 {
     var function = _schema.FindFunction(functionName);
     var formattedParameters = new ValueFormatter().Format(parameters, "&");
     var commandText = function.ActualName + "?" + formattedParameters;
     var command = new HttpCommand(function.HttpMethod.ToUpper(), commandText.ToString());
     _requestBuilder.AddCommandToRequest(command);
     return _requestRunner.ExecuteFunction(command);
 }
 public override IEnumerable<IDictionary<string, object>> FindEntries(HttpCommand command, bool scalarResult, bool setTotalCount, out int totalCount)
 {
     totalCount = 0;
     return null;
 }
Esempio n. 39
0
 public abstract IEnumerable <IEnumerable <IEnumerable <KeyValuePair <string, object> > > > ExecuteFunction(HttpCommand command);
 public override IDictionary<string, object> InsertEntry(HttpCommand command, bool resultRequired = true)
 {
     return command.OriginalContent;
 }
Esempio n. 41
0
 public abstract int DeleteEntry(HttpCommand command);
 public override int DeleteEntry(HttpCommand command)
 {
     return 0;
 }
Esempio n. 43
0
 public abstract int UpdateEntry(HttpCommand command);
Esempio n. 44
0
 public abstract IEnumerable <IDictionary <string, object> > FindEntries(HttpCommand command, bool scalarResult, bool setTotalCount, out int totalCount);
Esempio n. 45
0
 public abstract IDictionary <string, object> InsertEntry(HttpCommand command, bool resultRequired);
Esempio n. 46
0
 public override IEnumerable <IEnumerable <IEnumerable <KeyValuePair <string, object> > > > ExecuteFunction(HttpCommand command)
 {
     return(null);
 }
Esempio n. 47
0
 public abstract IEnumerable<IEnumerable<IEnumerable<KeyValuePair<string, object>>>> ExecuteFunction(HttpCommand command);
Esempio n. 48
0
 public override IDictionary <string, object> InsertEntry(HttpCommand command, bool resultRequired)
 {
     return(command.OriginalContent);
 }
Esempio n. 49
0
 public abstract IDictionary <string, object> GetEntry(HttpCommand command);
Esempio n. 50
0
 public override IDictionary <string, object> GetEntry(HttpCommand command)
 {
     return(null);
 }
Esempio n. 51
0
 public abstract void AddCommandToRequest(HttpCommand command);
Esempio n. 52
0
 public abstract IDictionary<string, object> GetEntry(HttpCommand command);
 public abstract HttpRequest CreateRequest(HttpCommand command, bool returnContent = false);
Esempio n. 54
0
 public abstract int UpdateEntry(HttpCommand command);
Esempio n. 55
0
        private HttpCommand CreateUnlinkCommand(string tableName, string associationName, string entryPath)
        {
            var commandText = ODataHelper.CreateLinkCommand(entryPath, associationName);

            return(HttpCommand.Delete(commandText));
        }
Esempio n. 56
0
 public abstract IEnumerable<IDictionary<string, object>> ExecuteFunction(HttpCommand command);
 public IEnumerable<IEnumerable<IEnumerable<KeyValuePair<string, object>>>> ExecuteFunction(string functionName, IDictionary<string, object> parameters)
 {
     var function = _schema.FindFunction(functionName);
     var command = new HttpCommand(function.HttpMethod.ToUpper(), new ODataClientWithCommand(this, _schema).Function(functionName).Parameters(parameters).CommandText);
     _requestBuilder.AddCommandToRequest(command);
     return _requestRunner.ExecuteFunction(command);
 }