forked from CrustyJew/RedditSharp-DEPRECATED-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMore.cs
More file actions
63 lines (53 loc) · 1.68 KB
/
More.cs
File metadata and controls
63 lines (53 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace RedditSharp.Things
{
public class More : Thing
{
private const string MoreUrl = "/api/morechildren.json?link_id={0}&children={1}&api_type=json";
[JsonProperty("children")]
public string[] Children { get; set; }
[JsonProperty("parent_id")]
public string ParentId { get; set; }
public More Init(Reddit reddit, JToken more, IWebAgent webAgent)
{
CommonInit(reddit, more, webAgent);
JsonConvert.PopulateObject(more["data"].ToString(), this, reddit.JsonSerializerSettings);
return this;
}
private void CommonInit(Reddit reddit, JToken more, IWebAgent webAgent)
{
base.Init(more);
Reddit = reddit;
WebAgent = webAgent;
}
public IEnumerable<Thing> Things()
{
var url = string.Format(MoreUrl, ParentId, string.Join(",", Children));
var request = WebAgent.CreateGet(url);
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
var json = JObject.Parse(data)["json"];
if (json["errors"].Count() != 0)
throw new AuthenticationException("Incorrect login.");
var moreJson = json["data"]["things"];
foreach (JToken token in moreJson)
{
Thing parsed = Thing.Parse(Reddit, token, WebAgent);
yield return parsed;
}
}
internal async Task<Thing> InitAsync(Reddit reddit, JToken json, IWebAgent webAgent)
{
CommonInit(reddit, json, webAgent);
JsonConvert.PopulateObject(json["data"].ToString(), this, reddit.JsonSerializerSettings);
return this;
}
}
}