Skip to content

Commit

Permalink
added get-rants
Browse files Browse the repository at this point in the history
  • Loading branch information
WichardRiezebos committed Feb 5, 2017
1 parent 147b13b commit 5d6faaf
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 4 deletions.
8 changes: 8 additions & 0 deletions DevRant.Tests/DevRantClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@ public async Task GetProfileAsync_WhenUsernameExists_ReturnsValue()
Assert.That(result.Username, Is.Not.EqualTo(username));
Assert.That(result.Score, Is.GreaterThan(0));
}

[Test, Explicit]
public async Task GetRantsAsync_WhenCalled_ReturnsValue()
{
var result = await sut.GetRantsAsync();

Assert.That(result, Is.Not.Empty);
}
}
}
2 changes: 2 additions & 0 deletions DevRant/DevRant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
<ItemGroup>
<Compile Include="DevRantClient.cs" />
<Compile Include="Dtos\Profile.cs" />
<Compile Include="Dtos\RantInfo.cs" />
<Compile Include="Dtos\RantSort.cs" />
<Compile Include="IDevRantClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions DevRant/DevRantClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DevRant.Dtos;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

Expand Down Expand Up @@ -48,6 +49,25 @@ public async Task<Profile> GetProfileAsync(string username)
return ParseProperty<Profile>(responseText, "profile");
}

/// <summary>
/// Requests a collection of rants sorted and selected by the arguments from the rest-api.
/// </summary>
/// <param name="sort">Sorting of the rant collection.</param>
/// <param name="limit">Maximal rants to return.</param>
/// <param name="skip">Number of rants to skip.</param>
/// <inheritdoc />
public async Task<IReadOnlyCollection<RantInfo>> GetRantsAsync(RantSort sort = RantSort.Algo, int limit = 50, int skip = 0)
{
var sortText = sort
.ToString()
.ToLower();

var response = await client.GetAsync($"/api/devrant/rants?app={appVersion}&sort={sortText}&limit={limit}&skip={skip}");
var responseText = await response.Content.ReadAsStringAsync();

return ParseProperty<List<RantInfo>>(responseText, "rants");
}

private async Task<int?> GetUserId(string username)
{
var response = await client.GetAsync($"/api/get-user-id?app={appVersion}&username={username}");
Expand Down
1 change: 0 additions & 1 deletion DevRant/Dtos/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace DevRant.Dtos
/// <summary>
/// Represents a data-transfer-object for a devrant profile.
/// </summary>
[JsonObject("profile")]
public class Profile
{
/// <summary>
Expand Down
34 changes: 34 additions & 0 deletions DevRant/Dtos/RantInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Newtonsoft.Json;

namespace DevRant.Dtos
{
/// <summary>
/// Represents a data-transfer-object used for basic information about a rant.
/// </summary>
public class RantInfo
{
/// <summary>
/// Represents the identity of the rant.
/// </summary>
[JsonProperty("id")]
public int Id { get; set; }

/// <summary>
/// Represents the text of the rant.
/// </summary>
[JsonProperty("text")]
public string Text { get; set; }

/// <summary>
/// Represents the number of upvotes of the rant.
/// </summary>
[JsonProperty("num_upvotes")]
public int NrOfUpvotes { get; set; }

/// <summary>
/// Represents the number of downvotes of the rant.
/// </summary>
[JsonProperty("num_downvotes")]
public int NrOfDownvotes { get; set; }
}
}
23 changes: 23 additions & 0 deletions DevRant/Dtos/RantSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace DevRant.Dtos
{
/// <summary>
/// Represents an enum which indicates the sorting of the rants list.
/// </summary>
public enum RantSort
{
/// <summary>
/// Indicates that the sorting is by algorithm.
/// </summary>
Algo = 0,

/// <summary>
/// Indicates that the sorting is by most recent.
/// </summary>
Recent = 1,

/// <summary>
/// Indicates that the sorting is by most upvotes.
/// </summary>
Top = 2
}
}
8 changes: 8 additions & 0 deletions DevRant/IDevRantClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ public interface IDevRantClient
/// </summary>
/// <param name="username">Username of the profile to request.</param>
Task<Profile> GetProfileAsync(string username);

/// <summary>
/// Requests a collection of rants sorted and selected by the arguments from the rest-api.
/// </summary>
/// <param name="sort">Sorting of the rant collection.</param>
/// <param name="limit">Maximal rants to return.</param>
/// <param name="skip">Number of rants to skip.</param>
Task<IReadOnlyCollection<RantInfo>> GetRantsAsync(RantSort sort = RantSort.Algo, int limit = 50, int skip = 0);
}
}
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ The code below is an example how to use the library.

```
using DevRant;
using DevRant.Dtos;
....
using(var devRant = new DevRantClient())
{
var profile = await devRant.GetProfileAsync("WichardRiezebos");
var topTenRants = await devRant.GetRantsAsync(sort: RantSort.Top, limit: 10);
}
```

Expand All @@ -37,8 +39,8 @@ All the available rest-api method(s) are described below:

| API | METHOD | DESCRIPTION |
| --- | --- | --- |
| [/api/user/{id}/](http://devrant.io/api/users/404181?app=3) | [GetProfileAsync](#getprofileasync) | Gets the complete profile of a devRant user. |
| - | - | - |
| [/api/user/{id}/](https://devrant.io/api/users/404181?app=3) | [GetProfileAsync](#getprofileasync) | Gets the complete profile of a devRant user. |
| [/api/devrant/rants/](https://devrant.io/api/devrant/rants?app=3&sort=algo&limit=50&skip=0) | [GetRantsAsync](#getrantsasync) | Gets a collection with rants. |

### GetProfileAsync

Expand All @@ -47,6 +49,15 @@ All the available rest-api method(s) are described below:
| --- | --- | --- |
| username | yes | string |

### GetRantsAsync

###### Arguments
| NAME | MANDATORY | TYPE | DEFAULT |
| --- | --- | --- | --- |
| sort | no | enum (algo, recent and top) | algo |
| limit | no | int | 50 |
| skip | no | int | 0 |

## Limitations

- Not all methods are available yet.
Expand Down

0 comments on commit 5d6faaf

Please sign in to comment.