Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 281034a

Browse files
committed
add article suggestions
1 parent 8248f90 commit 281034a

5 files changed

Lines changed: 79 additions & 26 deletions

File tree

PocketSharp.Tests/GetTests.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,34 @@ public async Task IsItemRetrievedById()
3333
Assert.True(item.Uri == itemDuplicate.Uri);
3434
}
3535

36-
// [Fact]
37-
// public async Task IsItemJsonPopulated()
38-
// {
39-
// List<PocketItem> items = (await client.Get()).ToList();
40-
// string schemaJson = @"{
41-
// 'description': 'PocketItem',
42-
// 'type': 'object'
43-
// }";
44-
45-
// JsonSchema schema = JsonSchema.Parse(schemaJson);
46-
// foreach (var pocketItem in items)
47-
// {
48-
// Assert.True(!string.IsNullOrWhiteSpace(pocketItem.Json));
49-
// var jObject = JObject.Parse(pocketItem.Json);
50-
// Assert.True(jObject.IsValid(schema));
51-
// }
52-
// }
36+
37+
[Fact]
38+
public async Task AreSuggestionsRetrieved()
39+
{
40+
var articles = await client.GetTrendingArticles();
41+
string itemId = articles.First().ID;
42+
43+
List<PocketItem> suggestions = (await client.GetSuggestions(itemId)).ToList();
44+
Assert.True(suggestions.Count > 0);
45+
}
46+
47+
// [Fact]
48+
// public async Task IsItemJsonPopulated()
49+
// {
50+
// List<PocketItem> items = (await client.Get()).ToList();
51+
// string schemaJson = @"{
52+
// 'description': 'PocketItem',
53+
// 'type': 'object'
54+
// }";
55+
56+
// JsonSchema schema = JsonSchema.Parse(schemaJson);
57+
// foreach (var pocketItem in items)
58+
// {
59+
// Assert.True(!string.IsNullOrWhiteSpace(pocketItem.Json));
60+
// var jObject = JObject.Parse(pocketItem.Json);
61+
// Assert.True(jObject.IsValid(schema));
62+
// }
63+
// }
5364

5465
[Fact]
5566
public async Task AreFilteredItemsRetrieved()

PocketSharp.Tests/TrendingTests.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,15 @@ public class TrendingTests : TestsBase
1010
[Fact]
1111
public async Task AreTrendingArticlesReturned()
1212
{
13-
string guid = await client.GetGuid();
14-
var articles = await client.GetTrendingArticles(guid);
15-
13+
var articles = await client.GetTrendingArticles();
1614
Assert.NotEmpty(articles);
1715
}
1816

1917

2018
[Fact]
2119
public async Task AreTrendingTopicsReturned()
2220
{
23-
string guid = await client.GetGuid();
24-
var topics = await client.GetTrendingTopics(guid);
25-
21+
var topics = await client.GetTrendingTopics();
2622
Assert.NotEmpty(topics);
2723
}
2824
}

PocketSharp/Components/Get.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using PocketSharp.Models;
1+
using PocketSharp.Models;
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -224,6 +224,29 @@ public IEnumerable<PocketItem> ConvertJsonToList(string itemsJSON)
224224

225225
return await Request<PocketArticle>("", cancellationToken, parameters, false, true);
226226
}
227+
228+
229+
/// <summary>
230+
/// Get article suggestions for an existing Pocket item.
231+
/// </summary>
232+
/// <param name="itemId">Get suggestions based on this item.</param>
233+
/// <param name="count">Requested item count.</param>
234+
/// <param name="languageCode">Two-letter language code for language-specific results.</param>
235+
/// <param name="cancellationToken">The cancellation token.</param>
236+
/// <returns></returns>
237+
/// <exception cref="PocketException"></exception>
238+
public async Task<IEnumerable<PocketItem>> GetSuggestions(string itemId, int count = 3, string languageCode = "en", CancellationToken cancellationToken = default(CancellationToken))
239+
{
240+
Dictionary<string, string> parameters = new Dictionary<string, string>()
241+
{
242+
{ "resolved_id", itemId },
243+
{ "version", "1" },
244+
{ "locale_lang", languageCode },
245+
{ "count", count.ToString() }
246+
};
247+
248+
return (await Request<Retrieve>("getSuggestedItems", cancellationToken, parameters)).Items ?? new List<PocketItem>();
249+
}
227250
}
228251

229252

PocketSharp/IPocketClient.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@ Task<IEnumerable<PocketItem>> Get(
220220
/// <returns></returns>
221221
/// <exception cref="PocketException"></exception>
222222
Task<PocketArticle> GetArticle(Uri uri, bool includeImages = true, bool includeVideos = true, bool forceRefresh = false, CancellationToken cancellationToken = default(CancellationToken));
223+
224+
/// <summary>
225+
/// Get article suggestions for an existing Pocket item.
226+
/// </summary>
227+
/// <param name="itemId">Get suggestions based on this item.</param>
228+
/// <param name="count">Requested item count.</param>
229+
/// <param name="languageCode">Two-letter language code for language-specific results.</param>
230+
/// <param name="cancellationToken">The cancellation token.</param>
231+
/// <returns></returns>
232+
/// <exception cref="PocketException"></exception>
233+
Task<IEnumerable<PocketItem>> GetSuggestions(string itemId, int count = 3, string languageCode = "en", CancellationToken cancellationToken = default(CancellationToken));
223234
#endregion
224235

225236
#region modify methods

PocketSharp/Utilities/JsonExtensions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Newtonsoft.Json.Converters;
33
using Newtonsoft.Json.Linq;
44
using System;
@@ -51,7 +51,19 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
5151
return null;
5252
}
5353

54-
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Convert.ToDouble(reader.Value));
54+
double value;
55+
if (!Double.TryParse((string)reader.Value, out value))
56+
{
57+
DateTime date;
58+
if (DateTime.TryParse((string)reader.Value, out date))
59+
{
60+
return date;
61+
}
62+
63+
return null;
64+
}
65+
66+
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(value);
5567
}
5668
}
5769

0 commit comments

Comments
 (0)