12
12
13
13
namespace devRantDotNet
14
14
{
15
+ /// <summary>
16
+ /// A C# Wrapper for the devRant API
17
+ /// </summary>
15
18
public class devRant
16
19
{
20
+
21
+ /// <summary>
22
+ /// Rant sort type
23
+ /// </summary>
17
24
public enum SortType
18
25
{
26
+ /// <summary>
27
+ /// Sorts using algo on devRant
28
+ /// </summary>
19
29
algo ,
30
+
31
+ /// <summary>
32
+ /// Sorts by top rants
33
+ /// </summary>
20
34
top ,
35
+
36
+ /// <summary>
37
+ /// Sorts by recent rants
38
+ /// </summary>
21
39
recent
22
40
}
23
41
24
- private string MakeRequest ( string url )
42
+ /// <summary>
43
+ /// Uses <see cref="HttpWebRequest"/> and <see cref="HttpWebResponse"/> to create requests to the API.
44
+ /// Returns the JSON result.
45
+ /// </summary>
46
+ /// <param name="url">The url of the endpoint</param>
47
+ /// <returns>The JSON Result</returns>
48
+ private async Task < string > MakeRequestAsync ( string url )
25
49
{
26
-
27
50
// Set a default policy level for the "http:" and "https" schemes.
28
51
HttpRequestCachePolicy policy = new HttpRequestCachePolicy ( HttpRequestCacheLevel . Default ) ;
29
52
HttpWebRequest . DefaultCachePolicy = policy ;
@@ -37,7 +60,7 @@ private string MakeRequest(string url)
37
60
38
61
request . ContentType = "application/json; charset=utf-8" ;
39
62
string t ;
40
- var response = ( HttpWebResponse ) request . GetResponse ( ) ;
63
+ var response = ( HttpWebResponse ) await request . GetResponseAsync ( ) ;
41
64
42
65
using ( var sr = new StreamReader ( response . GetResponseStream ( ) ) )
43
66
{
@@ -47,6 +70,11 @@ private string MakeRequest(string url)
47
70
return t ;
48
71
}
49
72
73
+ /// <summary>
74
+ /// Converting the JSON received into a Rant object which is specified in <see cref="Rant"/>
75
+ /// </summary>
76
+ /// <param name="r">The JSON string received by the API containing the Rant</param>
77
+ /// <returns>A Rant object with the properties received</returns>
50
78
private Rant JSONToRantObject ( dynamic r )
51
79
{
52
80
var rant = new Rant
@@ -75,6 +103,11 @@ private Rant JSONToRantObject(dynamic r)
75
103
76
104
}
77
105
106
+ /// <summary>
107
+ /// Convert a comment on a rant to a Comment object as specified in <see cref="Comment"/>
108
+ /// </summary>
109
+ /// <param name="c">The JSON string received by the API containing the comment</param>
110
+ /// <returns>A Comment object with the received properties</returns>
78
111
private Comment JSONToCommentObject ( dynamic c )
79
112
{
80
113
Comment comment = new Comment ( )
@@ -101,9 +134,9 @@ private Comment JSONToCommentObject(dynamic c)
101
134
/// </summary>
102
135
/// <param name="type"> Type of sort e.g. Top, Algo or Recent</param>
103
136
/// <returns>A List of Rants which are iterable</returns>
104
- public List < Rant > GetRants ( SortType type )
137
+ public async Task < List < Rant > > GetRantsAsync ( SortType type )
105
138
{
106
- var req = MakeRequest ( Values . AllRants + "?sort=" + type + "&app=3" ) ;
139
+ var req = await MakeRequestAsync ( Values . AllRants + "?sort=" + type + "&app=3" ) ;
107
140
dynamic results = JsonConvert . DeserializeObject < dynamic > ( req ) ;
108
141
109
142
if ( results . success != "true" )
@@ -124,11 +157,16 @@ public List<Rant> GetRants(SortType type)
124
157
return rants ;
125
158
}
126
159
127
- public Rant GetRant ( int id )
160
+ /// <summary>
161
+ /// Gets a single rant given the Id of it
162
+ /// </summary>
163
+ /// <param name="id">The Id of the rant that is being queried</param>
164
+ /// <returns>The Rant that is requested as a Rant object</returns>
165
+ public async Task < Rant > GetRantAsync ( int id )
128
166
{
129
167
try
130
168
{
131
- var req = MakeRequest ( Values . SingleRant + id + Values . AppId ) ;
169
+ var req = await MakeRequestAsync ( Values . SingleRant + id + Values . AppId ) ;
132
170
dynamic results = JsonConvert . DeserializeObject < dynamic > ( req ) ;
133
171
var r = results . rant ;
134
172
@@ -146,11 +184,16 @@ public Rant GetRant(int id)
146
184
}
147
185
}
148
186
149
- public int GetUserId ( string username )
187
+ /// <summary>
188
+ /// Gets the user Id given a username
189
+ /// </summary>
190
+ /// <param name="username">username of the person that the Id is wanted for</param>
191
+ /// <returns>The Id of the user with the username specified</returns>
192
+ public async Task < int > GetUserIdAsync ( string username )
150
193
{
151
194
try
152
195
{
153
- var req = MakeRequest ( Values . UsernameById + "?username=" + username + "&app=3" ) ;
196
+ var req = await MakeRequestAsync ( Values . UsernameById + "?username=" + username + "&app=3" ) ;
154
197
dynamic results = JsonConvert . DeserializeObject < dynamic > ( req ) ;
155
198
156
199
if ( results . success != "true" )
@@ -164,11 +207,16 @@ public int GetUserId(string username)
164
207
catch { return 0 ; }
165
208
}
166
209
167
- public User GetProfile ( long id )
210
+ /// <summary>
211
+ /// Gets a user profile with all the rants and other info associated with it
212
+ /// </summary>
213
+ /// <param name="id">Id of the user</param>
214
+ /// <returns>The User object with all the user-info and rants</returns>
215
+ public async Task < User > GetProfileAsync ( long id )
168
216
{
169
217
try
170
218
{
171
- var req = MakeRequest ( Values . User + id + Values . AppId ) ;
219
+ var req = await MakeRequestAsync ( Values . User + id + Values . AppId ) ;
172
220
dynamic results = JsonConvert . DeserializeObject < dynamic > ( req ) ;
173
221
var profile = results . profile ;
174
222
if ( results . success != "true" )
@@ -221,11 +269,16 @@ public User GetProfile(long id)
221
269
}
222
270
}
223
271
224
- public List < Rant > Search ( string term )
272
+ /// <summary>
273
+ /// Allows to search the devRant website
274
+ /// </summary>
275
+ /// <param name="term">The term to search with</param>
276
+ /// <returns>A List of rants matching the search terms</returns>
277
+ public async Task < List < Rant > > SearchAsync ( string term )
225
278
{
226
279
try
227
280
{
228
- var req = MakeRequest ( Values . Search + "?term=" + term + "&app=3" ) ;
281
+ var req = await MakeRequestAsync ( Values . Search + "?term=" + term + "&app=3" ) ;
229
282
dynamic results = JsonConvert . DeserializeObject < dynamic > ( req ) ;
230
283
231
284
if ( results . success != "true" )
@@ -249,11 +302,15 @@ public List<Rant> Search(string term)
249
302
}
250
303
}
251
304
252
- public Rant GetRandomRant ( )
305
+ /// <summary>
306
+ /// SURPRISE!!!
307
+ /// </summary>
308
+ /// <returns>A random rant as a Rant object</returns>
309
+ public async Task < Rant > GetRandomRantAsync ( )
253
310
{
254
311
try
255
312
{
256
- var req = MakeRequest ( Values . Random + Values . AppId ) ;
313
+ var req = await MakeRequestAsync ( Values . Random + Values . AppId ) ;
257
314
dynamic results = JsonConvert . DeserializeObject < dynamic > ( req ) ;
258
315
259
316
return results . success == "true" ? JSONToRantObject ( results . rant ) : null ;
0 commit comments