Skip to content

Commit 603a5b3

Browse files
author
Ihtasham
committed
Made the methods async as I don't see a reason not to, added documentation.
1 parent bea6318 commit 603a5b3

File tree

4 files changed

+95
-38
lines changed

4 files changed

+95
-38
lines changed

devRantDotNet/Source/devRant.cs

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,41 @@
1212

1313
namespace devRantDotNet
1414
{
15+
/// <summary>
16+
/// A C# Wrapper for the devRant API
17+
/// </summary>
1518
public class devRant
1619
{
20+
21+
/// <summary>
22+
/// Rant sort type
23+
/// </summary>
1724
public enum SortType
1825
{
26+
/// <summary>
27+
/// Sorts using algo on devRant
28+
/// </summary>
1929
algo,
30+
31+
/// <summary>
32+
/// Sorts by top rants
33+
/// </summary>
2034
top,
35+
36+
/// <summary>
37+
/// Sorts by recent rants
38+
/// </summary>
2139
recent
2240
}
2341

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)
2549
{
26-
2750
// Set a default policy level for the "http:" and "https" schemes.
2851
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
2952
HttpWebRequest.DefaultCachePolicy = policy;
@@ -37,7 +60,7 @@ private string MakeRequest(string url)
3760

3861
request.ContentType = "application/json; charset=utf-8";
3962
string t;
40-
var response = (HttpWebResponse)request.GetResponse();
63+
var response = (HttpWebResponse)await request.GetResponseAsync();
4164

4265
using(var sr = new StreamReader(response.GetResponseStream()))
4366
{
@@ -47,6 +70,11 @@ private string MakeRequest(string url)
4770
return t;
4871
}
4972

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>
5078
private Rant JSONToRantObject(dynamic r)
5179
{
5280
var rant = new Rant
@@ -75,6 +103,11 @@ private Rant JSONToRantObject(dynamic r)
75103

76104
}
77105

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>
78111
private Comment JSONToCommentObject(dynamic c)
79112
{
80113
Comment comment = new Comment()
@@ -101,9 +134,9 @@ private Comment JSONToCommentObject(dynamic c)
101134
/// </summary>
102135
/// <param name="type"> Type of sort e.g. Top, Algo or Recent</param>
103136
/// <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)
105138
{
106-
var req = MakeRequest(Values.AllRants+"?sort="+type+"&app=3");
139+
var req = await MakeRequestAsync(Values.AllRants+"?sort="+type+"&app=3");
107140
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
108141

109142
if (results.success != "true")
@@ -124,11 +157,16 @@ public List<Rant> GetRants(SortType type)
124157
return rants;
125158
}
126159

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)
128166
{
129167
try
130168
{
131-
var req = MakeRequest(Values.SingleRant + id + Values.AppId);
169+
var req = await MakeRequestAsync(Values.SingleRant + id + Values.AppId);
132170
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
133171
var r = results.rant;
134172

@@ -146,11 +184,16 @@ public Rant GetRant(int id)
146184
}
147185
}
148186

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)
150193
{
151194
try
152195
{
153-
var req = MakeRequest(Values.UsernameById + "?username=" + username + "&app=3");
196+
var req = await MakeRequestAsync(Values.UsernameById + "?username=" + username + "&app=3");
154197
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
155198

156199
if (results.success != "true")
@@ -164,11 +207,16 @@ public int GetUserId(string username)
164207
catch { return 0; }
165208
}
166209

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)
168216
{
169217
try
170218
{
171-
var req = MakeRequest(Values.User + id + Values.AppId);
219+
var req = await MakeRequestAsync(Values.User + id + Values.AppId);
172220
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
173221
var profile = results.profile;
174222
if(results.success != "true")
@@ -221,11 +269,16 @@ public User GetProfile(long id)
221269
}
222270
}
223271

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)
225278
{
226279
try
227280
{
228-
var req = MakeRequest(Values.Search + "?term=" + term + "&app=3");
281+
var req = await MakeRequestAsync(Values.Search + "?term=" + term + "&app=3");
229282
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
230283

231284
if (results.success != "true")
@@ -249,11 +302,15 @@ public List<Rant> Search(string term)
249302
}
250303
}
251304

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()
253310
{
254311
try
255312
{
256-
var req = MakeRequest(Values.Random + Values.AppId);
313+
var req = await MakeRequestAsync(Values.Random + Values.AppId);
257314
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
258315

259316
return results.success == "true" ? JSONToRantObject(results.rant) : null;

devRantDotNet/devRantDotNet.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
<WarningLevel>4</WarningLevel>
3131
</PropertyGroup>
3232
<ItemGroup>
33-
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
34-
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
33+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
34+
<HintPath>..\packages\Newtonsoft.Json.10.0.1-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>
3535
<Private>True</Private>
3636
</Reference>
3737
<Reference Include="System" />

devRantDotNet/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
3+
<package id="Newtonsoft.Json" version="10.0.1-beta1" targetFramework="net452" />
44
</packages>

devRantTests/Program.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,52 @@ class Program
1111
{
1212
static devRant dr = new devRant();
1313

14-
public static void getAllRants()
14+
public static async void getAllRants()
1515
{
16-
dr.GetRants(devRant.SortType.recent).ForEach(r => Console.WriteLine(r.text));
16+
var res = await dr.GetRantsAsync(devRant.SortType.recent);
17+
res.ForEach(r => Console.WriteLine(r.text));
1718
}
1819

19-
public static void getSingleRant(int id)
20+
public static async void getSingleRant(int id)
2021
{
21-
dr.GetRant(id).rant_comments.ForEach(
22+
var res = await dr.GetRantAsync(id);
23+
res.rant_comments.ForEach(
2224
c => Console.WriteLine(c.user_username + ": " + c.body +"("+c.id+")")
2325
);
2426
}
2527

26-
public static void getUserId(string username)
28+
public static async Task<int> getUserId(string username)
2729
{
28-
Console.WriteLine(dr.GetUserId(username));
30+
var res = await dr.GetUserIdAsync(username);
31+
Console.WriteLine(res);
32+
return res;
2933
}
3034

31-
public static void getUserProfile(int id)
35+
public static async void getUserProfile(int id)
3236
{
33-
var user = dr.GetProfile(id);
37+
var user = await dr.GetProfileAsync(id);
3438
var dump = ObjectDumper.Dump(user);
3539
Console.WriteLine(dump);
3640
}
3741

38-
public static void getSearchResults(string q)
42+
public static async void getSearchResults(string q)
3943
{
40-
var results = dr.Search(q);
44+
var results = await dr.SearchAsync(q);
4145
results.ForEach(r => Console.WriteLine(r.text));
4246
}
4347

44-
public static void getRandomRant()
48+
public static async void getRandomRant()
4549
{
46-
var dump = ObjectDumper.Dump(dr.GetRandomRant());
50+
var dump = ObjectDumper.Dump(await dr.GetRandomRantAsync());
4751
Console.WriteLine(dump);
4852
}
4953

5054
static void Main(string[] args)
5155
{
52-
//getAllRants();
53-
//getUserProfile(dr.GetUserId("px06"));
54-
//getSearchResults("px06");
55-
//getSingleRant(448369);
56-
57-
getRandomRant();
58-
Console.WriteLine("-----------------------------------");
59-
getUserProfile(dr.GetUserId("px06"));
56+
getAllRants();
57+
getSearchResults("px06");
58+
getSingleRant(448369);
59+
getUserProfile(getUserId("px06").Result);
6060

6161
Console.ReadLine();
6262
}

0 commit comments

Comments
 (0)