Ejemplo n.º 1
0
            public void Write(EGTRequestStream requestStream, CyanRest restClient, string contentId)
            {
                byte[] contentBytes = null;
                if (entity != null)
                {
                    var content = entity.Serialize();
                    contentBytes = Encoding.UTF8.GetBytes(content.ToString());
                }

                var finalHeaders = new List<Tuple<string, string>>();
                finalHeaders.Add(Tuple.Create("Content-ID", contentId));
                if (contentBytes != null && contentBytes.Length > 0)
                {
                    finalHeaders.Add(Tuple.Create("Content-Type", "application/atom+xml;type=entry"));
                    finalHeaders.Add(Tuple.Create("Content-Length", contentBytes.Length.ToString()));
                }

                if (headers != null)
                    finalHeaders.AddRange(headers);

                // write status line
                requestStream.WriteLine("{0} {1} {2}", method, restClient.FormatUrl(resource), "HTTP/1.1");

                // write headers
                foreach (var header in finalHeaders)
                    requestStream.WriteHeader(header.Item1, header.Item2);
                requestStream.WriteLine();

                // write content
                if (contentBytes != null)
                {
                    requestStream.Write(contentBytes, 0, contentBytes.Length);
                    requestStream.WriteLine();
                }
            }
Ejemplo n.º 2
0
        byte[] EncodeBatchRequestBody(string batchBoundary)
        {
            var changesetBoundary = string.Format("changeset_{0}", Guid.NewGuid());

            byte[] contentBytes = null;

            using (var contentStream = new EGTRequestStream())
            {
                // write batch boundary
                contentStream.WriteBoundary(batchBoundary);
                // write batch Content-Type header
                contentStream.WriteHeader("Content-Type", string.Format("multipart/mixed; boundary={0}", changesetBoundary));
                // blank line after headers
                contentStream.WriteLine();

                int index = 0;
                foreach (var operation in operations)
                {
                    // each changeset
                    // write changeset begin boundary
                    contentStream.WriteBoundary(changesetBoundary);

                    // required headers
                    contentStream.WriteHeader("Content-Type", "application/http");
                    contentStream.WriteHeader("Content-Transfer-Encoding", "binary");
                    contentStream.WriteLine();

                    // write changeset payload
                    operation.Write(contentStream, table.restClient, index++.ToString());
                }

                // write changeset and batch end boundaries
                contentStream.WriteEndBoundary(changesetBoundary);
                contentStream.WriteEndBoundary(batchBoundary);

                contentBytes = contentStream.ToArray();
            }

            //var debug = Encoding.UTF8.GetString(contentBytes);

            return contentBytes;
        }