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
269 lines (249 loc) · 11.5 KB
/
Multi.cs
File metadata and controls
269 lines (249 loc) · 11.5 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
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Threading.Tasks;
namespace RedditSharp.Multi
{
/// <summary>
/// A Multireddit.
/// </summary>
public class Multi : RedditObject
{
#region Constant API URLs
private const string GetCurrentUserMultiUrl = "/api/multi/mine";
private string GetPublicUserMultiUrl(string user) => $"/api/multi/user/{user}";
private string GetMultiPathUrl(string path) => $"/api/multi/{path}";
private string GetMultiDescriptionPathUrl(string path) => $"/api/multi/{path}/description";
private string GetMultiSubUrl(string path, string subredddit) => $"/api/multi/{path}/r/{1}";
private const string PostMultiRenameUrl = "/api/multi/rename";
private string PutSubMultiUrl(string path, string subreddit) => $"/api/multi/{path}/r/{subreddit}";
private const string CopyMultiUrl = "/api/multi/copy";
#endregion
/// <summary>
/// Create Multi object to interact with MultiReddits
/// </summary>
/// <param name="agent">WebAgent to use</param>
public Multi(IWebAgent agent) : base(agent)
{
}
/// <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 async Task<IList<MultiData>> GetCurrentUsersMultisAsync()
{
var json = await WebAgent.Get(GetCurrentUserMultiUrl).ConfigureAwait(false);
return json.Select(m => new MultiData(WebAgent, m)).ToList();
}
/// <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 async Task<IList<MultiData>> GetPublicUserMultisAsync(string username)
{
var json = await WebAgent.Get(GetPublicUserMultiUrl(username)).ConfigureAwait(false);
return json.Select(m => new MultiData(WebAgent, m)).ToList();
}
/// <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 async Task<MultiData> GetMultiByPathAsync(string path)
{
var json = await WebAgent.Get(GetMultiPathUrl(path)).ConfigureAwait(false);
return new MultiData(WebAgent, json);
}
/// <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 async Task<MultiData> GetMultiDescriptionAsync(string path)
{
var json = await WebAgent.Get(GetMultiDescriptionPathUrl(path)).ConfigureAwait(false);
return new MultiData(WebAgent, json, false);
}
/// <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 async Task<MultiSubs> GetSubInformationAsync(string path, string subreddit)
{
var json = await WebAgent.Get(GetMultiSubUrl(path, subreddit)).ConfigureAwait(false);
return new MultiSubs(WebAgent, json);
}
/// <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 async Task<JToken> RenameMultiAsync(string displayName, string pathFrom, string pathTo)
{
return await WebAgent.Post(PostMultiRenameUrl, new
{
display_name = displayName,
from = pathFrom,
to = pathTo
}).ConfigureAwait(false);
}
/// <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 async Task<string> PutSubMultiAsync(string path, string subName)
{
var request = WebAgent.CreateRequest(PutSubMultiUrl(path, subName), "PUT");
JObject modelData = new JObject();
modelData.Add("name", subName);
WebAgent.WritePostBody(request, new
{
model = modelData,
multipath = path,
srname = subName
});
var response = await WebAgent.GetResponseAsync(request).ConfigureAwait(false);
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
/// <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 async Task<string> PutMultiDescriptionAsync(string path, string description)
{
var request = WebAgent.CreateRequest(GetMultiDescriptionPathUrl(path), "PUT");
JObject modelData = new JObject();
modelData.Add("body_md", description);
WebAgent.WritePostBody(request, new
{
model = modelData,
multipath = path
});
var response = await WebAgent.GetResponseAsync(request).ConfigureAwait(false);
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
/// <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 async Task<JToken> CopyMultiAsync(string displayName, string pathFrom, string pathTo)
{
return await WebAgent.Post(CopyMultiUrl, new
{
display_name = displayName,
from = pathFrom,
to = pathTo
}).ConfigureAwait(false);
}
/// <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 async Task<string> DeleteSubAsync(string path, string subname)
{
var request = WebAgent.CreateRequest(GetMultiSubUrl(path, subname), "DELETE");
WebAgent.WritePostBody(request, new
{
multipath = path,
srname = subname
});
var response = await WebAgent.GetResponseAsync(request).ConfigureAwait(false);
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
/// <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 async Task<string> DeleteMultiAsync(string path)
{
var request = WebAgent.CreateRequest(GetMultiPathUrl(path), "DELETE");
WebAgent.WritePostBody(request, new
{
multipath = path
});
var response = await WebAgent.GetResponseAsync(request).ConfigureAwait(false);
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
/// <summary>
/// Create a new Multi for the authenticated user
/// </summary>
/// <param name="m">Multi Data</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 async Task<JToken> PostMultiAsync(MData m, string path)
{
JObject modelData = new JObject();
modelData.Add("description_md", m.DescriptionMD);
modelData.Add("display_name", m.DisplayName);
modelData.Add("icon_name", m.IconName);
modelData.Add("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);
return await WebAgent.Post(GetMultiPathUrl(path), new
{
model = modelData,
multipath = path
}).ConfigureAwait(false);
}
/// <summary>
/// Create or update a Multi for the authenticated user
/// </summary>
/// <param name="m">Multi Data</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 async Task<string> PutMultiAsync(MData m, string path)
{
var request = WebAgent.CreateRequest(GetMultiPathUrl(path), "PUT");
JObject modelData = new JObject();
modelData.Add("description_md", m.DescriptionMD);
modelData.Add("display_name", m.DisplayName);
modelData.Add("icon_name", m.IconName);
modelData.Add("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(request, new
{
model = modelData,
multipath = path
});
var response = await WebAgent.GetResponseAsync(request).ConfigureAwait(false);
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
}