ImgurNet.ApiEndpoints.CommentEndpoint.CreateCommentAsync C# (CSharp) Method

CreateCommentAsync() public method

Creates a new comment
public CreateCommentAsync ( string caption, string imageId, int parentId = null ) : Task>
caption string The body of the comment
imageId string The image to post the comment on
parentId int [optional] The id of the comment this is a reply to, (if this is a reply)
return Task>
		public async Task<ImgurResponse<Comment>> CreateCommentAsync(string caption, string imageId, int? parentId = null)
		{
			if (ImgurClient.Authentication == null)
				throw new InvalidAuthenticationException("Authentication can not be null. Set it in the main Imgur class.");

			if (!(ImgurClient.Authentication is OAuth2Authentication))
				throw new InvalidAuthenticationException("You need to use OAuth2Authentication to call this Endpoint.");

			var keyPairs = new List<KeyValuePair<string, string>>
			{
				new KeyValuePair<string, string>("image_id", imageId),
				new KeyValuePair<string, string>("comment", caption)
			};
			if (parentId != null) keyPairs.Add(new KeyValuePair<string, string>("parent_id", parentId.ToString()));
			var multi = new FormUrlEncodedContent(keyPairs.ToArray());

			return
				await
					Request.SubmitImgurRequestAsync<Comment>(Request.HttpMethod.Post, CommentCreateUrl,
						ImgurClient.Authentication, content: multi);
		}

Usage Example

		public async Task TestCreateComment()
		{
			var imgurClient = await AuthenticationHelpers.CreateOAuth2AuthenticatedImgurClient();
			var commentEndpoint = new CommentEndpoint(imgurClient);
			var comment = await commentEndpoint.CreateCommentAsync("test reply", "161n8BB", 193421419);

			// Assert the Reponse
			Assert.IsNotNull(comment.Data);
			Assert.AreEqual(comment.Success, true);
			Assert.AreEqual(comment.Status, HttpStatusCode.OK);
		}