public async Task EditText(string newText) { if (Reddit.User == null) { throw new Exception("No user logged in."); } if (!this.IsSelfPost) { throw new Exception("Submission to edit is not a self-post."); } HttpClient client = Reddit.CreateClient(); var content = Reddit.StringForPost(new { api_type = "json", text = newText, thing_id = FullName, uh = Reddit.User.Modhash }); var response = await client.PostAsync(EditUserTextUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); JToken json = JToken.Parse(responseContent); if (json["json"].ToString().Contains("\"errors\": []")) // errors element is empty { this.SelfText = newText; } else { throw new Exception("Error editing text."); } }
/// <summary> /// Submits a link post in the current subreddit using the logged-in user /// </summary> /// <param name="title">The title of the submission</param> /// <param name="url">The url of the submission link</param> public async Task <Post> SubmitPost(string title, string url) { if (Reddit.User == null) { throw new Exception("No user logged in."); } HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { api_type = "json", extension = "json", kind = "link", sr = Title, title = title, uh = Reddit.User.Modhash, url = url }); var response = await client.PostAsync(SubmitLinkUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); var json = JToken.Parse(responseContent); return(new Post(Reddit, json["json"])); // TODO: Error }
public async Task <SubredditStyle> GetStylesheet() { HttpClient client = Reddit.CreateClient(); var response = await client.GetAsync(string.Format(StylesheetUrl, Name)); var responseContent = await response.Content.ReadAsStringAsync(); var json = JToken.Parse(responseContent); return(new SubredditStyle(Reddit, this, json)); }
/// <summary> /// Resets the subreddit's header image to the Reddit logo /// </summary> public async Task ResetHeaderImage() { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { uh = Reddit.User.Modhash, r = Subreddit.Name }); var response = await client.PostAsync(DeleteHeaderImageUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); }
public async Task SetAsRead() { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { id = this.FullName, uh = Reddit.User.Modhash, api_type = "json" }); var response = await client.PostAsync(SetAsReadUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); }
public async Task AcceptModeratorInvite() { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { api_type = "json", uh = Reddit.User.Modhash, r = Name }); var response = await client.PostAsync(AcceptModeratorInviteUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); }
public async Task ClearVote() { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { dir = 0, id = FullName, uh = Reddit.User.Modhash }); var response = await client.PostAsync(VoteUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); }
public async Task ClearFlairTemplates(FlairType flairType) { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { flair_type = flairType == FlairType.Link ? "LINK_FLAIR" : "USER_FLAIR", uh = Reddit.User.Modhash, r = Name }); var response = await client.PostAsync(ClearFlairTemplatesUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); }
public async Task Unsave() { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { id = FullName, uh = Reddit.User.Modhash }); var response = await client.PostAsync(UnsaveUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); Saved = false; }
public async Task RemoveModerator(string id) { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { api_type = "json", uh = Reddit.User.Modhash, r = Name, type = "moderator", id }); var response = await client.PostAsync(LeaveModerationUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); }
public async Task SetUserFlair(string user, string cssClass, string text) { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { css_class = cssClass, text = text, uh = Reddit.User.Modhash, r = Name, name = user }); var response = await client.PostAsync(SetUserFlairUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); }
public async Task Vote(VoteType type) { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { dir = (int)type, id = FullName, uh = Reddit.User.Modhash }); var response = await client.PostAsync(VoteUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); Liked = null; }
public async Task Delete() { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { img_name = Name, uh = Reddit.User.Modhash, r = SubredditStyle.Subreddit.Name }); var response = await client.PostAsync(DeleteImageUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); SubredditStyle.Images.Remove(this); }
// an attempt at using Task and System.Net.Http public async Task <Comment[]> GetComments() { var comments = new List <Comment>(); // create a new HttpClient HttpClient client = Reddit.CreateClient(); string body = await client.GetStringAsync(string.Format(GetCommentsUrl, Id)); var json = JArray.Parse(body); var postJson = json.Last()["data"]["children"]; foreach (var comment in postJson) { comments.Add(new Comment(Reddit, comment)); } return(comments.ToArray()); }
public async Task Downvote() { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { dir = -1, id = FullName, uh = Reddit.User.Modhash }); var response = await client.PostAsync(VoteUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); // TODO: check the data for success or failure Liked = false; }
public async Task UpdateCss() { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { op = "save", stylesheet_content = CSS, uh = Reddit.User.Modhash, api_type = "json", r = Subreddit.Name }); var response = await client.PostAsync(UpdateCssUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); var json = JToken.Parse(responseContent); }
public async Task Reply(string message) { if (Reddit.User == null) { throw new Exception("No user logged in."); } HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { text = message, thing_id = FullName, uh = Reddit.User.Modhash }); var response = await client.PostAsync(CommentUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); var json = JObject.Parse(responseContent); }
public async Task Unsubscribe() { if (Reddit.User == null) { throw new Exception("No user logged in."); } HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { action = "unsub", sr = FullName, uh = Reddit.User.Modhash }); var response = await client.PostAsync(SubscribeUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); // Do something with the results or discard them }
public async Task AddFlairTemplate(string cssClass, FlairType flairType, string text, bool userEditable) { HttpClient client = Reddit.CreateClient(); StringContent content = Reddit.StringForPost(new { css_class = cssClass, flair_type = flairType == FlairType.Link ? "LINK_FLAIR" : "USER_FLAIR", text = text, text_editable = userEditable, uh = Reddit.User.Modhash, r = Name, api_type = "json" }); var response = await client.PostAsync(FlairTemplateUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); var json = JToken.Parse(responseContent); }
public async Task UpdateSettings() { HttpClient client = Reddit.CreateClient(); string link_type; string type; string wikimode; switch (ContentOptions) { case RedditWP.ContentOptions.All: link_type = "any"; break; case RedditWP.ContentOptions.LinkOnly: link_type = "link"; break; default: link_type = "self"; break; } switch (SubredditType) { case SubredditType.Public: type = "public"; break; case SubredditType.Private: type = "private"; break; default: type = "restricted"; break; } switch (WikiEditMode) { case WikiEditMode.All: wikimode = "anyone"; break; case WikiEditMode.Moderators: wikimode = "modonly"; break; default: wikimode = "disabled"; break; } StringContent content = Reddit.StringForPost(new { allow_top = AllowAsDefault, description = Sidebar, domain = Domain, lang = Language, link_type, over_18 = NSFW, public_description = PublicDescription, show_media = ShowThumbnails, sr = Subreddit.FullName, title = Title, type, uh = Reddit.User.Modhash, wiki_edit_age = WikiEditAge, wiki_edit_karma = WikiEditKarma, wikimode, api_type = "json" }, "header-title", HeaderHoverText); var response = await client.PostAsync(SiteAdminUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); }