Skip to content

Commit 8258f2c

Browse files
authored
Add personalized feed support (#86)
1 parent da0b12a commit 8258f2c

File tree

6 files changed

+81
-3
lines changed

6 files changed

+81
-3
lines changed

src/IStreamClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public interface IStreamClient
6060
/// </summary>
6161
IStreamFeed Feed(string feedSlug, string userId);
6262

63+
/// <summary>
64+
/// Reads enriched activities of a personalized feed.
65+
/// </summary>
66+
Task<PersonalizedGetResponse<EnrichedActivity>> GetPersonalizedFeedAsync(GetOptions options = null);
67+
6368
/// <summary>
6469
/// Allows you to retrieve open graph information from a URL which you can then use to add images and a description to activities.
6570
/// </summary>

src/Models/GenericResponse.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ public class GenericGetResponse<T> : ResponseBase
88
/// <summary>Container for <typeparamref name="T"/> objects.</summary>
99
public List<T> Results { get; set; }
1010
}
11-
}
11+
12+
/// <summary>Base class for personalized read responses of <typeparamref name="T"/>.</summary>
13+
public class PersonalizedGetResponse<T> : GenericGetResponse<T>
14+
{
15+
public int Limit { get; set; }
16+
public string Next { get; set; }
17+
public int Offset { get; set; }
18+
public string Version { get; set; }
19+
}
20+
}

src/Models/GetOptions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class GetOptions
1313
private ReactionOption _reaction = null;
1414
private string _ranking = null;
1515
private string _session = null;
16+
private string _endpoint = null;
17+
private string _feed_slug = null;
18+
private string _user_id = null;
1619

1720
public GetOptions WithOffset(int offset)
1821
{
@@ -56,6 +59,24 @@ public GetOptions WithSession(string session)
5659
return this;
5760
}
5861

62+
public GetOptions WithEndpoint(string endpoint)
63+
{
64+
_endpoint = endpoint;
65+
return this;
66+
}
67+
68+
public GetOptions WithFeedSlug(string feedSlug)
69+
{
70+
_feed_slug = feedSlug;
71+
return this;
72+
}
73+
74+
public GetOptions WithUserId(string userId)
75+
{
76+
_user_id = userId;
77+
return this;
78+
}
79+
5980
internal void Apply(RestRequest request)
6081
{
6182
request.AddQueryParameter("offset", _offset.ToString());
@@ -67,6 +88,15 @@ internal void Apply(RestRequest request)
6788
if (!string.IsNullOrWhiteSpace(_session))
6889
request.AddQueryParameter("session", _session);
6990

91+
if (!string.IsNullOrWhiteSpace(_endpoint))
92+
request.AddQueryParameter("endpoint", _endpoint);
93+
94+
if (!string.IsNullOrWhiteSpace(_feed_slug))
95+
request.AddQueryParameter("feed_slug", _feed_slug);
96+
97+
if (!string.IsNullOrWhiteSpace(_endpoint))
98+
request.AddQueryParameter("user_id", _user_id);
99+
70100
_filter?.Apply(request);
71101
_marker?.Apply(request);
72102
_reaction?.Apply(request);

src/StreamClient.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ public IStreamFeed Feed(string feedSlug, string userId)
9999
return new StreamFeed(this, feedSlug, userId);
100100
}
101101

102+
public async Task<PersonalizedGetResponse<EnrichedActivity>> GetPersonalizedFeedAsync(GetOptions options = null)
103+
{
104+
options = options ?? GetOptions.Default;
105+
var request = this.BuildPersonalizedFeedRequest();
106+
options.Apply(request);
107+
108+
var response = await this.MakeRequestAsync(request);
109+
if (response.StatusCode == HttpStatusCode.OK)
110+
return StreamJsonConverter.DeserializeObject<PersonalizedGetResponse<EnrichedActivity>>(response.Content);
111+
112+
throw StreamException.FromResponse(response);
113+
}
114+
102115
public async Task<ResponseBase> ActivityPartialUpdateAsync(string id = null, ForeignIdTime foreignIdTime = null, Dictionary<string, object> set = null, IEnumerable<string> unset = null)
103116
{
104117
if (id == null && foreignIdTime == null)
@@ -175,6 +188,9 @@ internal RestRequest BuildFeedRequest(StreamFeed feed, string path, HttpMethod m
175188
internal RestRequest BuildEnrichedFeedRequest(StreamFeed feed, string path, HttpMethod method)
176189
=> BuildRestRequest(BaseUrlPath + feed.EnrichedPath + path, method);
177190

191+
internal RestRequest BuildPersonalizedFeedRequest()
192+
=> BuildRestRequest(BaseUrlPath + "enrich/personalization/feed/", HttpMethod.Get);
193+
178194
internal RestRequest BuildActivitiesRequest()
179195
=> BuildRestRequest(BaseUrlPath + ActivitiesUrlPath, HttpMethod.Post);
180196

tests/PersonalizationTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using NUnit.Framework;
2+
using System;
3+
using Stream.Models;
24
using System.Collections.Generic;
35
using System.Threading.Tasks;
46

@@ -25,5 +27,21 @@ public async Task ReadPersonalization()
2527
Assert.True(d.ContainsKey("duration"));
2628
Assert.True(d.ContainsKey("results"));
2729
}
30+
31+
[Test]
32+
[Ignore("Not always needed, set credentials to run when needed")]
33+
public async Task ReadPersonalizedFeed()
34+
{
35+
var options = GetOptions.Default.
36+
WithEndpoint("etoro_newsfeed").
37+
WithFeedSlug("newsfeed").
38+
WithRanking("etoro").
39+
WithUserId(Guid.NewGuid().ToString());
40+
41+
var response = await Client.GetPersonalizedFeedAsync(options);
42+
Assert.AreEqual(20, response.Limit);
43+
Assert.AreEqual(0, response.Offset);
44+
Assert.AreEqual(response.Results.Count, 0);
45+
}
2846
}
29-
}
47+
}

tests/stream-net-tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<Name>stream-net</Name>
1818
</ProjectReference>
1919
</ItemGroup>
20-
<PropertyGroup>
20+
<PropertyGroup>
2121
<CodeAnalysisRuleSet>../.stylecop.ruleset</CodeAnalysisRuleSet>
2222
</PropertyGroup>
2323
</Project>

0 commit comments

Comments
 (0)