public async Task <Response <ConfigurationSetting> > AddAsync(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(IfNoneMatchWildcard); 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)); } }
public async Task <Response <FaceDetectResult> > DetectAsync(CancellationToken cancellation, Uri image, FaceDetectOptions options) { if (options == null) { options = new FaceDetectOptions(); } Uri uri = BuildUri(options); PipelineCallContext context = null; try { context = _client.CreateContext(_options, cancellation); context.SetRequestLine(ServiceMethod.Post, uri); context.AddHeader(_keyHeader); context.AddHeader(Header.Common.JsonContentType); context.SetContent(new FaceContent(image, context)); await _client.ProcessAsync(context).ConfigureAwait(false); ServiceResponse response = context.Response; if (!response.TryGetHeader(s_contentLength, out long contentLength)) { throw new Exception("bad response: no content length header"); } var buffer = new byte[contentLength]; var read = await response.ContentStream.ReadAsync(buffer, cancellation); Func <ServiceResponse, FaceDetectResult> contentParser = null; if (response.Status == 200) { contentParser = (rsp) => { return(FaceDetectResult.Parse(new ReadOnlySequence <byte>(buffer, 0, read))); }; } return(new Response <FaceDetectResult>(response, contentParser)); } catch { if (context != null) { context.Dispose(); } throw; } }
public async Task <Response> CreateAsync(CancellationToken cancellation) { PipelineCallContext context = null; try { context = Pipeline.CreateContext(_options, cancellation); context.SetRequestLine(ServiceMethod.Put, _baseUri); await Pipeline.ProcessAsync(context).ConfigureAwait(false); return(new Response(context.Response)); } catch { if (context != null) { context.Dispose(); } throw; } }