From f68290d8d7d740718b537021bce3b4d14bcc9602 Mon Sep 17 00:00:00 2001 From: ferhat elmas Date: Wed, 22 Jun 2022 13:12:02 +0200 Subject: [PATCH] feat: add personalized feed --- src/IStreamClient.cs | 5 +++++ src/Models/GenericResponse.cs | 11 ++++++++++- src/Models/GetOptions.cs | 30 ++++++++++++++++++++++++++++++ src/StreamClient.cs | 16 ++++++++++++++++ tests/PersonalizationTests.cs | 20 +++++++++++++++++++- tests/stream-net-tests.csproj | 2 +- 6 files changed, 81 insertions(+), 3 deletions(-) diff --git a/src/IStreamClient.cs b/src/IStreamClient.cs index b6e8977..c12575d 100644 --- a/src/IStreamClient.cs +++ b/src/IStreamClient.cs @@ -60,6 +60,11 @@ public interface IStreamClient /// IStreamFeed Feed(string feedSlug, string userId); + /// + /// Reads enriched activities of a personalized feed. + /// + Task> GetPersonalizedFeedAsync(GetOptions options = null); + /// /// Allows you to retrieve open graph information from a URL which you can then use to add images and a description to activities. /// diff --git a/src/Models/GenericResponse.cs b/src/Models/GenericResponse.cs index 8d76a4b..70cfbeb 100644 --- a/src/Models/GenericResponse.cs +++ b/src/Models/GenericResponse.cs @@ -8,4 +8,13 @@ public class GenericGetResponse : ResponseBase /// Container for objects. public List Results { get; set; } } -} \ No newline at end of file + + /// Base class for personalized read responses of . + public class PersonalizedGetResponse : GenericGetResponse + { + public int Limit { get; set; } + public string Next { get; set; } + public int Offset { get; set; } + public string Version { get; set; } + } +} diff --git a/src/Models/GetOptions.cs b/src/Models/GetOptions.cs index ea488fe..7faf751 100644 --- a/src/Models/GetOptions.cs +++ b/src/Models/GetOptions.cs @@ -13,6 +13,9 @@ public class GetOptions private ReactionOption _reaction = null; private string _ranking = null; private string _session = null; + private string _endpoint = null; + private string _feed_slug = null; + private string _user_id = null; public GetOptions WithOffset(int offset) { @@ -56,6 +59,24 @@ public GetOptions WithSession(string session) return this; } + public GetOptions WithEndpoint(string endpoint) + { + _endpoint = endpoint; + return this; + } + + public GetOptions WithFeedSlug(string feedSlug) + { + _feed_slug = feedSlug; + return this; + } + + public GetOptions WithUserId(string userId) + { + _user_id = userId; + return this; + } + internal void Apply(RestRequest request) { request.AddQueryParameter("offset", _offset.ToString()); @@ -67,6 +88,15 @@ internal void Apply(RestRequest request) if (!string.IsNullOrWhiteSpace(_session)) request.AddQueryParameter("session", _session); + if (!string.IsNullOrWhiteSpace(_endpoint)) + request.AddQueryParameter("endpoint", _endpoint); + + if (!string.IsNullOrWhiteSpace(_feed_slug)) + request.AddQueryParameter("feed_slug", _feed_slug); + + if (!string.IsNullOrWhiteSpace(_endpoint)) + request.AddQueryParameter("user_id", _user_id); + _filter?.Apply(request); _marker?.Apply(request); _reaction?.Apply(request); diff --git a/src/StreamClient.cs b/src/StreamClient.cs index 6251e73..7850132 100644 --- a/src/StreamClient.cs +++ b/src/StreamClient.cs @@ -99,6 +99,19 @@ public IStreamFeed Feed(string feedSlug, string userId) return new StreamFeed(this, feedSlug, userId); } + public async Task> GetPersonalizedFeedAsync(GetOptions options = null) + { + options = options ?? GetOptions.Default; + var request = this.BuildPersonalizedFeedRequest(); + options.Apply(request); + + var response = await this.MakeRequestAsync(request); + if (response.StatusCode == HttpStatusCode.OK) + return StreamJsonConverter.DeserializeObject>(response.Content); + + throw StreamException.FromResponse(response); + } + public async Task ActivityPartialUpdateAsync(string id = null, ForeignIdTime foreignIdTime = null, Dictionary set = null, IEnumerable unset = null) { if (id == null && foreignIdTime == null) @@ -175,6 +188,9 @@ internal RestRequest BuildFeedRequest(StreamFeed feed, string path, HttpMethod m internal RestRequest BuildEnrichedFeedRequest(StreamFeed feed, string path, HttpMethod method) => BuildRestRequest(BaseUrlPath + feed.EnrichedPath + path, method); + internal RestRequest BuildPersonalizedFeedRequest() + => BuildRestRequest(BaseUrlPath + "enrich/personalization/feed/", HttpMethod.Get); + internal RestRequest BuildActivitiesRequest() => BuildRestRequest(BaseUrlPath + ActivitiesUrlPath, HttpMethod.Post); diff --git a/tests/PersonalizationTests.cs b/tests/PersonalizationTests.cs index c53e449..e58495d 100644 --- a/tests/PersonalizationTests.cs +++ b/tests/PersonalizationTests.cs @@ -1,4 +1,6 @@ using NUnit.Framework; +using System; +using Stream.Models; using System.Collections.Generic; using System.Threading.Tasks; @@ -25,5 +27,21 @@ public async Task ReadPersonalization() Assert.True(d.ContainsKey("duration")); Assert.True(d.ContainsKey("results")); } + + [Test] + [Ignore("Not always needed, set credentials to run when needed")] + public async Task ReadPersonalizedFeed() + { + var options = GetOptions.Default. + WithEndpoint("etoro_newsfeed"). + WithFeedSlug("newsfeed"). + WithRanking("etoro"). + WithUserId(Guid.NewGuid().ToString()); + + var response = await Client.GetPersonalizedFeedAsync(options); + Assert.AreEqual(20, response.Limit); + Assert.AreEqual(0, response.Offset); + Assert.AreEqual(response.Results.Count, 0); + } } -} \ No newline at end of file +} diff --git a/tests/stream-net-tests.csproj b/tests/stream-net-tests.csproj index 7c5672e..c6f0673 100644 --- a/tests/stream-net-tests.csproj +++ b/tests/stream-net-tests.csproj @@ -17,7 +17,7 @@ stream-net - + ../.stylecop.ruleset