Ejemplo n.º 1
0
        public virtual ISDataResults Execute(SDataParameters parms)
        {
            var request  = CreateRequest(parms, true);
            var response = request.GetResponse();

            TraceResponse(response);
            return(SDataResults.FromResponse(response));
        }
Ejemplo n.º 2
0
        Lazy <T>
#endif
        Add(HttpMethod method, object content)
        {
            Guard.ArgumentNotNull(content, "content");
            if (_state == 1)
            {
                throw new InvalidOperationException("Commit in progress");
            }
            if (_state == 2)
            {
                throw new InvalidOperationException("Batch has been committed");
            }

            var parms = new SDataParameters
            {
                Method     = method,
                Path       = _path,
                Content    = content,
                Include    = _options.Include,
                Select     = _options.Select,
                Precedence = _options.Precedence
            };

            if (method == HttpMethod.Put || method == HttpMethod.Delete)
            {
                parms.ETag = ContentHelper.GetProtocolValue <string>(content, SDataProtocolProperty.ETag);
            }

#if !NET_2_0 && !NET_3_5
            var index = _items.Count;
#endif
            _items.Add(parms);

#if !NET_2_0 && !NET_3_5
            return(new Lazy <T>(() =>
            {
                if (_state == 0)
                {
                    throw new InvalidOperationException("Value cannot be fetched before committing the batch");
                }
                if (_state == 1)
                {
                    throw new InvalidOperationException("Value cannot be fetched until the commit has completed");
                }
                return _results[index];
            }));
#endif
        }
        public override void Save(Stream stream)
        {
            if (stream == null)
            {
                _attachment = string.IsNullOrEmpty(_attachment.Key)
                    ? _client.Post(_attachment)
                    : _client.Put(_attachment);
                return;
            }

            var parms = new SDataParameters
                {
                    Files = {new AttachedFile(null, _attachment.FileName, stream)}
                };

            if (string.IsNullOrEmpty(_attachment.Key))
            {
                parms.Method = HttpMethod.Post;
                parms.Path = "attachments";
            }
            else
            {
                parms.Method = HttpMethod.Put;
                parms.Path = "attachments(" + SDataUri.FormatConstant(_attachment.Key) + ")";
                parms.ETag = _attachment.ETag;
            }

            if (_formMode)
            {
                foreach (var prop in typeof (Attachment).GetProperties())
                {
                    var name = _client.NamingScheme.GetName(prop);
                    if (!name.StartsWith("$", StringComparison.Ordinal) && !new[] {"physicalFileName", "fileExists", "fileSize", "createDate", "createUser", "modifyDate", "modifyUser"}.Contains(name, StringComparer.Ordinal))
                    {
                        var value = prop.GetValue(_attachment, null);
                        if (value != null)
                        {
                            parms.Form[name] = value.ToString();
                        }
                    }
                }

                parms.Path += "/file";
                var results = _client.Execute(parms);
                if (!string.IsNullOrEmpty(results.Location))
                {
                    var selector = new SDataUri(results.Location).GetPathSegment(4).Selector;
                    _attachment.Key = selector.Substring(1, selector.Length - 2);
                    _attachment.ETag = results.ETag;
                }
            }
            else
            {
                parms.Content = _attachment;
                _attachment = _client.Execute<Attachment>(parms).Content;
            }
        }
Ejemplo n.º 4
0
        private SDataRequest CreateRequest(SDataParameters parms, bool responseContentIgnored)
        {
            Guard.ArgumentNotNull(parms, "parms");

            var uri = new SDataUri(Uri)
            {
                StartIndex    = parms.StartIndex,
                Count         = parms.Count,
                Where         = parms.Where,
                OrderBy       = parms.OrderBy,
                Search        = parms.Search,
                Include       = parms.Include,
                Select        = parms.Select,
                Precedence    = parms.Precedence,
                IncludeSchema = parms.IncludeSchema,
                ReturnDelta   = parms.ReturnDelta,
                TrackingId    = parms.TrackingId,
                Format        = parms.Format,
                Language      = parms.Language,
                Version       = parms.Version
            };

            if (parms.Method != HttpMethod.Delete)
            {
                if (uri.Precedence == null && responseContentIgnored)
                {
                    uri.Precedence = 0;
                }
                if (uri.Format == null)
                {
                    uri.Format = Format;
                }
                if (uri.Language == null)
                {
                    uri.Language = Language;
                }
                if (uri.Version == null)
                {
                    uri.Version = Version;
                }
            }
            if (parms.Path != null)
            {
                uri.AppendPath(parms.Path);
            }
            foreach (var arg in parms.ExtensionArgs)
            {
                uri["_" + arg.Key] = arg.Value;
            }

            var content = parms.Content;

            if (parms.Method == HttpMethod.Put && DifferentialUpdate)
            {
                var tracking = content as IChangeTracking;
                if (tracking != null)
                {
                    content = tracking.GetChanges();
                    if (content == null)
                    {
                        throw new SDataClientException("Content doesn't have any changes");
                    }
                }
            }

            var request = CreateRequest(uri, parms.Method, content);

            request.ContentType = parms.ContentType ?? Format;
            request.ETag        = parms.ETag;
            foreach (var item in parms.Form)
            {
                request.Form.Add(item.Key, item.Value);
            }
            foreach (var file in parms.Files)
            {
                request.Files.Add(file);
            }
            request.Accept         = parms.Accept;
            request.AcceptLanguage = parms.Language ?? Language;
            TraceRequest(request);
            return(request);
        }
Ejemplo n.º 5
0
        public virtual Task <ISDataResults <T> > ExecuteAsync <T>(SDataParameters parms, CancellationToken cancel = default(CancellationToken))
        {
            var request = CreateRequest(parms, false);

            return(ExecuteAsync(request, CreateResults <T>, cancel));
        }
Ejemplo n.º 6
0
        public virtual Task <ISDataResults> ExecuteAsync(SDataParameters parms, CancellationToken cancel = default(CancellationToken))
        {
            var request = CreateRequest(parms, true);

            return(ExecuteAsync(request, SDataResults.FromResponse, cancel));
        }
Ejemplo n.º 7
0
        public virtual ISDataResults <T> Execute <T>(SDataParameters parms)
        {
            var request = CreateRequest(parms, false);

            return(CreateResults <T>(request.GetResponse()));
        }