private async Task <IImage> UploadImageInternalAsync(Stream image, string album = null, string name = null, string title = null, string description = null, IProgress <int> progress = null, int?bufferSize = 4096, CancellationToken cancellationToken = default) { const string url = "upload"; using (var request = ImageRequestBuilder.UploadImageStreamRequest(url, image, album, name, title, description, progress, bufferSize)) { var response = await SendRequestAsync <Image>(request, cancellationToken).ConfigureAwait(false); return(response); } }
public void UploadStreamBinaryRequest_WithUrlNull_ThrowsArgumentNullException() { var requestBuilder = new ImageRequestBuilder(); using (var fs = new FileStream("banana.gif", FileMode.Open)) { requestBuilder.UploadImageStreamRequest(null, fs); } }
public void UploadStreamBinaryRequest_WithImageNull_ThrowsArgumentNullException() { var client = new ImgurClient("123", "1234"); var requestBuilder = new ImageRequestBuilder(); var url = $"{client.EndpointUrl}image"; using (var fs = new FileStream("banana.gif", FileMode.Open)) { requestBuilder.UploadImageStreamRequest(url, null); } }
public void UploadImageStreamRequest_WithUrlNull_ThrowsArgumentNullException() { using var ms = new MemoryStream(new byte[9]); var exception = Record.Exception(() => ImageRequestBuilder.UploadImageStreamRequest(null, ms)); Assert.NotNull(exception); Assert.IsType <ArgumentNullException>(exception); var argNullException = (ArgumentNullException)exception; Assert.Equal("url", argNullException.ParamName); }
/// <summary> /// Upload a new image using a stream. /// </summary> /// <param name="image">A stream.</param> /// <param name="albumId"> /// The id of the album you want to add the image to. For anonymous albums, {albumId} should be the /// deletehash that is returned at creation. /// </param> /// <param name="name">The name of the file.</param> /// <param name="title">The title of the image.</param> /// <param name="description">The description of the image.</param> /// <param name="progressBytes">A provider for progress updates.</param> /// <param name="progressBufferSize">The amount of bytes that should be uploaded while performing a progress upload.</param> /// <exception cref="ArgumentNullException"> /// Thrown when a null reference is passed to a method that does not accept it as a /// valid argument. /// </exception> /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception> /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception> /// <returns></returns> private async Task <IImage> UploadImageStreamInternalAsync(Stream image, string albumId = null, string name = null, string title = null, string description = null, IProgress <int> progressBytes = null, int progressBufferSize = 4096) { const string url = nameof(image); using (var request = ImageRequestBuilder.UploadImageStreamRequest(url, image, albumId, name, title, description, progressBytes, progressBufferSize)) { var returnImage = await SendRequestAsync <Image>(request).ConfigureAwait(false); return(returnImage); } }
public void UploadImageStreamRequest_WithImageNull_ThrowsArgumentNullException() { var apiClient = new ApiClient("123"); var mockUrl = $"{apiClient.BaseAddress}image"; var exception = Record.Exception(() => ImageRequestBuilder.UploadImageStreamRequest(mockUrl, null)); Assert.NotNull(exception); Assert.IsType <ArgumentNullException>(exception); var argNullException = (ArgumentNullException)exception; Assert.Equal("image", argNullException.ParamName); }
public void UploadStreamBinaryRequest_WithImageNull_ThrowsArgumentNullException() { var client = new ImgurClient("123", "1234"); var requestBuilder = new ImageRequestBuilder(); var mockUrl = $"{client.EndpointUrl}image"; var exception = Record.Exception(() => ImageRequestBuilder.UploadImageStreamRequest(mockUrl, null)); Assert.NotNull(exception); Assert.IsType <ArgumentNullException>(exception); var argNullException = (ArgumentNullException)exception; Assert.Equal(argNullException.ParamName, "image"); }
public async Task UploadImageProgressStreamRequest_Equal() { var apiClient = new ApiClient("123"); var mockUrl = $"{apiClient.BaseAddress}image"; using var ms = new MemoryStream(new byte[9]); var imageLength = ms.Length; var currentProgress = 0; int report(int progress) => currentProgress = progress; var progress = new Progress <int>(percent => report(percent)); using var request = ImageRequestBuilder.UploadImageStreamRequest(mockUrl, ms, "TheAlbum", "TheName", "TheTitle", "TheDescription", progress, 9999); Assert.NotNull(request); Assert.Equal("https://api.imgur.com/3/image", request.RequestUri.ToString()); Assert.Equal(HttpMethod.Post, request.Method); var content = (MultipartFormDataContent)request.Content; var streamContent = (ProgressStreamContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "image"); var type = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "type"); var name = streamContent.Headers.ContentDisposition.FileName; var album = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "album"); var title = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "title"); var description = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "description"); Assert.NotNull(streamContent); Assert.Equal(9999, streamContent._bufferSize); Assert.NotNull(type); Assert.NotNull(name); Assert.NotNull(album); Assert.NotNull(title); Assert.NotNull(description); var image = await streamContent.ReadAsByteArrayAsync(); Assert.Equal(imageLength, image.Length); Assert.Equal("file", await type.ReadAsStringAsync()); Assert.Equal("TheName", name); Assert.Equal("TheAlbum", await album.ReadAsStringAsync()); Assert.Equal("TheTitle", await title.ReadAsStringAsync()); Assert.Equal("TheDescription", await description.ReadAsStringAsync()); }
/// <summary> /// Upload a new image using a stream. /// </summary> /// <param name="image">A stream.</param> /// <param name="albumId"> /// The id of the album you want to add the image to. For anonymous albums, {albumId} should be the /// deletehash that is returned at creation. /// </param> /// <param name="name">The name of the file.</param> /// <param name="title">The title of the image.</param> /// <param name="description">The description of the image.</param> /// <param name="progressBytes">A provider for progress updates.</param> /// <param name="progressBufferSize">The amount of bytes that should be uploaded while performing a progress upload.</param> /// <exception cref="ArgumentNullException"> /// Thrown when a null reference is passed to a method that does not accept it as a /// valid argument. /// </exception> /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception> /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception> /// <returns></returns> public Basic <Image> UploadImageStream(Stream image, string albumId = null, string name = null, string title = null, string description = null) { if (image == null) { throw new ArgumentNullException(nameof(image)); } const string url = nameof(image); using (var request = ImageRequestBuilder.UploadImageStreamRequest(url, image, albumId, name, title, description)) { var httpResponse = HttpClient.SendAsync(request).Result; var jsonString = httpResponse.Content.ReadAsStringAsync().Result; var output = Newtonsoft.Json.JsonConvert.DeserializeObject <Basic <Image> >(httpResponse.Content.ReadAsStringAsync().Result.ToString()); return(output); } }
/// <summary> /// Upload a new image using a stream. /// </summary> /// <param name="image">A stream.</param> /// <param name="albumId"> /// The id of the album you want to add the image to. For anonymous albums, {albumId} should be the /// deletehash that is returned at creation. /// </param> /// <param name="name">The name of the file.</param> /// <param name="title">The title of the image.</param> /// <param name="description">The description of the image.</param> /// <param name="progressBytes">A provider for progress updates.</param> /// <param name="progressBufferSize">The amount of bytes that should be uploaded while performing a progress upload.</param> /// <exception cref="ArgumentNullException"> /// Thrown when a null reference is passed to a method that does not accept it as a /// valid argument. /// </exception> /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception> /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception> /// <returns></returns> public async Task <IImage> UploadImageStreamAsync(Stream image, string albumId = null, string name = null, string title = null, string description = null, IProgress <int> progressBytes = null, int progressBufferSize = 4096) { if (image == null) { throw new ArgumentNullException(nameof(image)); } const string url = nameof(image); using (var request = ImageRequestBuilder.UploadImageStreamRequest(url, image, albumId, name, title, description, progressBytes, progressBufferSize)) { var returnImage = await SendRequestAsync <Image>(request).ConfigureAwait(false); return(returnImage); } }
public async Task UploadStreamBinaryRequest_Equal() { var client = new ImgurClient("123", "1234"); var requestBuilder = new ImageRequestBuilder(); var mockUrl = $"{client.EndpointUrl}image"; using (var ms = new MemoryStream(new byte[9])) { var imageLength = ms.Length; var request = ImageRequestBuilder.UploadImageStreamRequest(mockUrl, ms, "TheAlbum", "TheName", "TheTitle", "TheDescription"); Assert.NotNull(request); Assert.Equal("https://api.imgur.com/3/image", request.RequestUri.ToString()); Assert.Equal(HttpMethod.Post, request.Method); var content = (MultipartFormDataContent)request.Content; var imageContent = (StreamContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "image"); var album = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "album"); var type = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "type"); var name = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "name"); var title = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "title"); var description = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "description"); Assert.NotNull(imageContent); Assert.NotNull(type); Assert.NotNull(album); Assert.NotNull(name); Assert.NotNull(title); Assert.NotNull(description); var image = await imageContent.ReadAsByteArrayAsync().ConfigureAwait(false); Assert.Equal(imageLength, image.Length); Assert.Equal("file", await type.ReadAsStringAsync().ConfigureAwait(false)); Assert.Equal("TheAlbum", await album.ReadAsStringAsync().ConfigureAwait(false)); Assert.Equal("TheName", await name.ReadAsStringAsync().ConfigureAwait(false)); Assert.Equal("TheTitle", await title.ReadAsStringAsync().ConfigureAwait(false)); Assert.Equal("TheDescription", await description.ReadAsStringAsync().ConfigureAwait(false)); } }
public async Task UploadStreamBinaryRequest_AreEqual() { var client = new ImgurClient("123", "1234"); var requestBuilder = new ImageRequestBuilder(); var url = $"{client.EndpointUrl}image"; using (var fs = new FileStream("banana.gif", FileMode.Open)) { var imageLength = fs.Length; var request = requestBuilder.UploadImageStreamRequest(url, fs, "TheAlbum", "TheTitle", "TheDescription"); Assert.IsNotNull(request); Assert.AreEqual("https://api.imgur.com/3/image", request.RequestUri.ToString()); Assert.AreEqual(HttpMethod.Post, request.Method); var content = (MultipartFormDataContent)request.Content; var imageContent = (StreamContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "image"); var album = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "album"); var type = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "type"); var title = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "title"); var description = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "description"); Assert.IsNotNull(imageContent); Assert.IsNotNull(type); Assert.IsNotNull(album); Assert.IsNotNull(title); Assert.IsNotNull(description); var image = await imageContent.ReadAsByteArrayAsync(); Assert.AreEqual(imageLength, image.Length); Assert.AreEqual("file", await type.ReadAsStringAsync()); Assert.AreEqual("TheAlbum", await album.ReadAsStringAsync()); Assert.AreEqual("TheTitle", await title.ReadAsStringAsync()); Assert.AreEqual("TheDescription", await description.ReadAsStringAsync()); } }