public async Task <Response <Stream> > DetectLazyAsync(CancellationToken cancellation, Uri image, FaceDetectOptions options = default) { if (options == null) { options = new FaceDetectOptions(); } Uri uri = BuildUri(options); HttpMessage message = null; try { message = _client.CreateMessage(_options, cancellation); message.SetRequestLine(PipelineMethod.Post, uri); message.AddHeader(_keyHeader); message.AddHeader(HttpHeader.Common.JsonContentType); message.SetContent(new FaceContent(image, message)); await _client.ProcessAsync(message).ConfigureAwait(false); return(new Response <Stream>(message.Response, message.Response.ContentStream)); } catch { if (message != null) { message.Dispose(); } throw; } }
public void DisposeNoopsForNullResponse() { var requestMock = new Mock <Request>(); HttpMessage message = new HttpMessage(requestMock.Object, new ResponseClassifier()); message.Dispose(); requestMock.Verify(r => r.Dispose(), Times.Once); }
public void MultipleDispose() { var buffer = new ArraySegment <byte>(new byte[0]); using (var message = new HttpMessage(buffer, 0)) { message.Dispose(); } }
public void DisposingMessageDisposesTheRequestAndResponse() { var requestMock = new Mock <Request>(); var responseMock = new Mock <Response>(); HttpMessage message = new HttpMessage(requestMock.Object, new ResponseClassifier()); message.Response = responseMock.Object; message.Dispose(); requestMock.Verify(r => r.Dispose(), Times.Once); responseMock.Verify(r => r.Dispose(), Times.Once); }
public async Task <Response <FaceDetectResult> > DetectAsync(CancellationToken cancellation, string imagePath, FaceDetectOptions options) { if (options == null) { options = new FaceDetectOptions(); } Uri uri = BuildUri(options); HttpMessage message = null; try { message = _client.CreateMessage(_options, cancellation); message.SetRequestLine(PipelineMethod.Post, uri); message.AddHeader(_keyHeader); message.AddHeader(HttpHeader.Common.OctetStreamContentType); SetContentStream(message, imagePath); await _client.ProcessAsync(message).ConfigureAwait(false); Response response = message.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 <Response, 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 (message != null) { message.Dispose(); } throw; } }
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)); } HttpMessage message = null; try { message = Pipeline.CreateMessage(_options, cancellation); message.SetRequestLine(PipelineMethod.Put, _baseUri); message.AddHeader(HttpHeader.Common.CreateContentLength(content.Length)); message.AddHeader(HttpHeader.Common.OctetStreamContentType); message.SetContent(PipelineContent.Create(content)); await Pipeline.ProcessAsync(message).ConfigureAwait(false); return(message.Response); } catch { if (message != null) { message.Dispose(); } throw; } }
public async Task <Response> CreateAsync(CancellationToken cancellation) { HttpMessage message = null; try { message = Pipeline.CreateMessage(_options, cancellation); message.SetRequestLine(PipelineMethod.Put, _baseUri); await Pipeline.ProcessAsync(message).ConfigureAwait(false); return(message.Response); } catch { if (message != null) { message.Dispose(); } throw; } }
public void Initialise(IJsonSerializer serializer) { HttpMessage?.Dispose(); var extractedUrl = FormatUrl(_url, out var guildId, out var channelId, out var webhookId); var builtQueryString = BuildQueryString(_queryStringParameters); var message = new HttpRequestMessage { Method = _method, RequestUri = new Uri(string.Concat(extractedUrl, builtQueryString), UriKind.Relative), Content = _content?.Prepare(serializer, Options) }; message.Headers.Add("X-Ratelimit-Precision", "millisecond"); var reason = Options?.Reason; if (reason != null) { message.Headers.Add("X-Audit-Log-Reason", Uri.EscapeDataString(reason)); } HttpMessage = message; Identifier = RateLimitBucket.GenerateIdentifier(_method, BucketsMethod, _url.Format, guildId, channelId, webhookId); }
public void Dispose() { _httpMessage.Dispose(); }
public void Dispose() { HttpMessage.Dispose(); _tcs = null; }
public void Dispose() { _message.Dispose(); _socket.Dispose(); }