forked from CrustyJew/RedditSharp-DEPRECATED-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMulti.cs
More file actions
386 lines (364 loc) · 15.8 KB
/
Multi.cs
File metadata and controls
386 lines (364 loc) · 15.8 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
namespace RedditSharp.Multi
{
public class Multi
{
private Reddit Reddit { get; set; }
private IWebAgent WebAgent { get; set; }
#region Constant API URLs
private const string GetCurrentUserMultiUrl = "/api/multi/mine";
private const string GetPublicUserMultiUrl = "/api/multi/user/{0}";
private const string GetMultiPathUrl = "/api/multi/{0}";
private const string GetMultiDescriptionPathUrl = "/api/multi/{0}/description";
private const string GetMultiSubUrl = "/api/multi/{0}/r/{1}";
private const string PostMultiRenameUrl = "/api/multi/rename";
private const string PutSubMultiUrl = "/api/multi/{0}/r/{1}";
private const string CopyMultiUrl = "/api/multi/copy";
#endregion
public Multi(Reddit reddit, IWebAgent webAgent)
{
Reddit = reddit;
WebAgent = webAgent;
}
/// <summary>
/// Retrieve a list of the Multis belonging to the currently authenticated user
/// </summary>
/// <returns>A List of MultiData containing the authenticated user's Multis</returns>
public List<MultiData> GetCurrentUsersMultis()
{
var request = WebAgent.CreateGet(GetCurrentUserMultiUrl);
var response = request.GetResponse();
var json = JToken.Parse(WebAgent.GetResponseString(response.GetResponseStream()));
List<MultiData> results = new List<MultiData>();
foreach (var r in json)
{
results.Add(new MultiData(Reddit,r,WebAgent));
}
return results;
}
/// <summary>
/// Retrieve a list of public Multis belonging to a given user
/// </summary>
/// <param name="username">Username to search</param>
/// <returns>A list of MultiData containing the public Multis of the searched user</returns>
public List<MultiData> GetPublicUserMultis(string username)
{
var request = WebAgent.CreateGet(string.Format(GetPublicUserMultiUrl,username));
var response = request.GetResponse();
var json = JToken.Parse(WebAgent.GetResponseString(response.GetResponseStream()));
List<MultiData> results = new List<MultiData>();
foreach (var r in json)
{
results.Add(new MultiData(Reddit, r, WebAgent));
}
return results;
}
/// <summary>
/// Retrieve the information of a Multi based on the URL path given
/// </summary>
/// <param name="path">URL path to use</param>
/// <returns>A MultiData containing the information for the found Multi</returns>
public MultiData GetMultiByPath(string path)
{
var request = WebAgent.CreateGet(string.Format(GetMultiPathUrl, path));
var response = request.GetResponse();
var json = JToken.Parse(WebAgent.GetResponseString(response.GetResponseStream()));
var result = new MultiData(Reddit, json, WebAgent);
return result;
}
/// <summary>
/// Retrieve the description for the Multi based on the URL path given
/// </summary>
/// <param name="path">URL path to use</param>
/// <returns>A MultiData containing the description for the found Multi</returns>
public MultiData GetMultiDescription(string path)
{
var request = WebAgent.CreateGet(string.Format(GetMultiDescriptionPathUrl, path));
var response = request.GetResponse();
var json = JToken.Parse(WebAgent.GetResponseString(response.GetResponseStream()));
var result = new MultiData(Reddit, json, WebAgent, false);
return result;
}
/// <summary>
/// Retrieve the information for a given subreddit in a Multi
/// </summary>
/// <param name="path">URL path to use</param>
/// <param name="subreddit">Subreddit name to get information for</param>
/// <returns>A MultiSubs element containing the information for the searched subreddit</returns>
public MultiSubs GetSubInformation(string path, string subreddit)
{
var request = WebAgent.CreateGet(string.Format(GetMultiSubUrl, path,subreddit));
var response = request.GetResponse();
var json = JToken.Parse(WebAgent.GetResponseString(response.GetResponseStream()));
var result = new MultiSubs(Reddit, json, WebAgent);
return result;
}
/// <summary>
/// Rename a Multi based on the given information
/// </summary>
/// <param name="displayName">New Display name for the Multi</param>
/// <param name="pathFrom">Original URL path of the Multi</param>
/// <param name="pathTo">New URL path of the Multi</param>
/// <returns>A String containing the new Multi information</returns>
public string RenameMulti(string displayName, string pathFrom, string pathTo)
{
if (Reddit.User == null)
{
throw new AuthenticationException("No user logged in.");
}
var request = WebAgent.CreatePost(PostMultiRenameUrl);
var stream = request.GetRequestStream();
WebAgent.WritePostBody(stream, new
{
display_name = displayName,
from = pathFrom,
to = pathTo,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
return data;
}
/// <summary>
/// Adds a Subreddit to the given Multi
/// </summary>
/// <param name="path">URL Path of the Multi to update</param>
/// <param name="subName">Name of the subreddit to add</param>
/// <returns>A String containing the information of the updated Multi</returns>
public string PutSubMulti(string path, string subName)
{
if (Reddit.User == null)
{
throw new AuthenticationException("No user logged in.");
}
var request = WebAgent.CreatePut(string.Format(PutSubMultiUrl,path,subName));
var stream = request.GetRequestStream();
JObject modelData = new JObject
{
{ "name", subName }
};
WebAgent.WritePostBody(stream, new
{
model = modelData,
multipath = path,
srname = subName,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
return data;
}
/// <summary>
/// Updates the description for a given Multi
/// </summary>
/// <param name="path">URL path of the Multi to update</param>
/// <param name="description">New description for the Multi</param>
/// <returns>A string containing the updated information of the Multi</returns>
public string PutMultiDescription(string path, string description)
{
if (Reddit.User == null)
{
throw new AuthenticationException("No user logged in.");
}
var request = WebAgent.CreatePut(string.Format(GetMultiDescriptionPathUrl, path));
var stream = request.GetRequestStream();
JObject modelData = new JObject
{
{ "body_md", description }
};
WebAgent.WritePostBody(stream, new
{
model = modelData,
multipath = path,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
return data;
}
/// <summary>
/// Makes a copy of a Multi
/// </summary>
/// <param name="displayName">Display name for the new Multi</param>
/// <param name="pathFrom">URL path to copy from</param>
/// <param name="pathTo">URL path to copy to</param>
/// <returns>A string containing the information of the new Multi</returns>
public string CopyMulti(string displayName, string pathFrom, string pathTo)
{
if (Reddit.User == null)
{
throw new AuthenticationException("No user logged in.");
}
var request = WebAgent.CreatePost(CopyMultiUrl);
var stream = request.GetRequestStream();
WebAgent.WritePostBody(stream, new
{
display_name = displayName,
from = pathFrom,
to = pathTo,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
return data;
}
/// <summary>
/// Remove a Subreddit from the given Multi
/// </summary>
/// <param name="path">URL path of the Multi to edit</param>
/// <param name="subname">Subreddit name to be removed</param>
/// <returns>A string containing the updated information of the given Multi.</returns>
public string DeleteSub(string path, string subname)
{
if (Reddit.User == null)
{
throw new AuthenticationException("No user logged in.");
}
var request = WebAgent.CreateDelete(string.Format(GetMultiSubUrl, path, subname));
var stream = request.GetRequestStream();
WebAgent.WritePostBody(stream, new
{
multipath = path,
srname = subname,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
return data;
}
/// <summary>
/// Deletes a Multi
/// </summary>
/// <param name="path">URL path of the Multi to Delete</param>
/// <returns>A string containing the success code for deletion.</returns>
public string DeleteMulti(string path)
{
if (Reddit.User == null)
{
throw new AuthenticationException("No user logged in.");
}
var request = WebAgent.CreateDelete(string.Format(GetMultiPathUrl, path));
var stream = request.GetRequestStream();
WebAgent.WritePostBody(stream, new
{
multipath = path,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
return data;
}
/// <summary>
/// Create a new Multi for the authenticated user
/// </summary>
/// <param name="description">Multi Description</param>
/// <param name="displayname">Multi Display Name</param>
/// <param name="iconname">Icon Name (must be one of the default values)</param>
/// <param name="keycolor">Hex Code for the desired color</param>
/// <param name="subreddits">Array of Subreddit names to add</param>
/// <param name="visibility">Visibility state for the Multi</param>
/// <param name="weightingscheme">Weighting Scheme for the Multi</param>
/// <param name="path">Desired URL path for the Multi</param>
/// <returns>A string containing the information for the newly created Multi or a status of (409) if the Multi already exists</returns>
public string PostMulti(MData m, string path)
{
if(Reddit.User == null)
{
throw new AuthenticationException("No user logged in");
}
var request = WebAgent.CreatePost(string.Format(GetMultiPathUrl,path));
var stream = request.GetRequestStream();
JObject modelData = new JObject
{
{ "description_md", m.DescriptionMD },
{ "display_name", m.DisplayName },
{ "icon_name", m.IconName },
{ "key_color", m.KeyColor }
};
JArray subData = new JArray();
foreach(var s in m.Subreddits)
{
JObject sub = new JObject
{
{ "name", s.Name }
};
subData.Add(sub);
}
modelData.Add("subreddits",subData);
modelData.Add("visibility",m.Visibility);
modelData.Add("weighting_scheme",m.WeightingScheme);
WebAgent.WritePostBody(stream, new
{
model = modelData,
multipath = path,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
return data;
}
/// <summary>
/// Create or update a Multi for the authenticated user
/// </summary>
/// <param name="description">Multi Description</param>
/// <param name="displayname">Multi Display Name</param>
/// <param name="iconname">Icon Name (must be one of the default values)</param>
/// <param name="keycolor">Hex Code for the desired color</param>
/// <param name="subreddits">Array of Subreddit names to add</param>
/// <param name="visibility">Visibility state for the Multi</param>
/// <param name="weightingscheme">Weighting Scheme for the Multi</param>
/// <param name="path">Desired URL path for the Multi</param>
/// <returns>A string containing the information for the newly created or updated Multi or a status of (409) if the Multi already exists</returns>
public string PutMulti(MData m, string path)
{
if (Reddit.User == null)
{
throw new AuthenticationException("No user logged in");
}
var request = WebAgent.CreatePut(string.Format(GetMultiPathUrl, path));
var stream = request.GetRequestStream();
JObject modelData = new JObject
{
{ "description_md", m.DescriptionMD },
{ "display_name", m.DisplayName },
{ "icon_name", m.IconName },
{ "key_color", m.KeyColor }
};
JArray subData = new JArray();
foreach (var s in m.Subreddits)
{
JObject sub = new JObject
{
{ "name", s.Name }
};
subData.Add(sub);
}
modelData.Add("subreddits", subData);
modelData.Add("visibility", m.Visibility);
modelData.Add("weighting_scheme", m.WeightingScheme);
WebAgent.WritePostBody(stream, new
{
model = modelData,
multipath = path,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
return data;
}
}
}