Ejemplo n.º 1
0
        public async Task <Response <ConfigurationSetting> > SetAsync(ConfigurationSetting setting, CancellationToken cancellation = default)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            if (string.IsNullOrEmpty(setting.Key))
            {
                throw new ArgumentNullException($"{nameof(setting)}.{nameof(setting.Key)}");
            }

            Uri uri = BuildUrlForKvRoute(setting);

            using (PipelineCallContext context = Pipeline.CreateContext(_options, cancellation))
            {
                ReadOnlyMemory <byte> content = Serialize(setting);

                context.SetRequestLine(ServiceMethod.Put, uri);

                context.AddHeader("Host", uri.Host);
                context.AddHeader(MediaTypeKeyValueApplicationHeader);
                context.AddHeader(Header.Common.JsonContentType);
                context.AddHeader(Header.Common.CreateContentLength(content.Length));
                AddAuthenticationHeaders(context, uri, ServiceMethod.Put, content, _secret, _credential);

                context.SetContent(PipelineContent.Create(content));

                await Pipeline.ProcessAsync(context).ConfigureAwait(false);

                return(await CreateResponse(context));
            }
        }
Ejemplo n.º 2
0
        static void SetContentStream(HttpMessage message, string imagePath)
        {
            byte[] temp   = new byte[4096];
            var    stream = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true);

            message.SetContent(PipelineContent.Create(stream));
            message.AddHeader(HttpHeader.Common.CreateContentLength(stream.Length));
        }
Ejemplo n.º 3
0
        public async Task <Response> PutRangeAsync(long index, int length, Stream content, CancellationToken cancellation)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if (length < 0 || length > 4 * 1024 * 1024)
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            if (content.CanRead == false)
            {
                throw new ArgumentOutOfRangeException(nameof(content));
            }
            if (content.CanSeek == false)
            {
                throw new ArgumentOutOfRangeException(nameof(content));
            }

            PipelineCallContext context = null;

            try {
                context = Pipeline.CreateContext(_options, cancellation);
                context.SetRequestLine(ServiceMethod.Put, _baseUri);

                context.AddHeader(Header.Common.CreateContentLength(content.Length));
                context.AddHeader(Header.Common.OctetStreamContentType);
                context.SetContent(PipelineContent.Create(content));

                await Pipeline.ProcessAsync(context).ConfigureAwait(false);

                return(new Response(context.Response));
            }
            catch {
                if (context != null)
                {
                    context.Dispose();
                }
                throw;
            }
        }
        public async Task <Response <ConfigurationSetting> > AddAsync(ConfigurationSetting setting, RequestOptions options = null, CancellationToken cancellation = default)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            if (string.IsNullOrEmpty(setting.Key))
            {
                throw new ArgumentNullException($"{nameof(setting)}.{nameof(setting.Key)}");
            }
            if (!string.IsNullOrEmpty(setting.ETag))
            {
                throw new ArgumentException($"{nameof(setting)}.{nameof(setting.ETag)} has to be null");
            }

            Uri uri = BuildUriForKvRoute(setting);

            using (HttpMessage message = Pipeline.CreateMessage(_options, cancellation)) {
                ReadOnlyMemory <byte> content = Serialize(setting);

                message.SetRequestLine(PipelineMethod.Put, uri);

                message.AddHeader("Host", uri.Host);
                message.AddHeader(IfNoneMatch, "*");
                message.AddHeader(MediaTypeKeyValueApplicationHeader);
                message.AddHeader(HttpHeader.Common.JsonContentType);
                message.AddHeader(HttpHeader.Common.CreateContentLength(content.Length));
                AddOptionsHeaders(options, message);
                AddAuthenticationHeaders(message, uri, PipelineMethod.Put, content, _secret, _credential);

                message.SetContent(PipelineContent.Create(content));

                await Pipeline.ProcessAsync(message).ConfigureAwait(false);

                var response = message.Response;
                if (response.Status == 200)
                {
                    return(await CreateResponse(response, cancellation));
                }
                else
                {
                    throw new ResponseFailedException(response);
                }
            }
        }