@@ -13,6 +13,8 @@ public class StreamClient : IStreamClient
1313 {
1414 internal const string BaseUrlFormat = "https://{0}-api.stream-io-api.com" ;
1515 internal const string BaseUrlPath = "/api/v1.0/" ;
16+ internal const string BasePersonalizationUrlFormat = "https://{0}-personalization.stream-io-api.com" ;
17+ internal const string BasePersonalizationUrlPath = "/personalization/v1.0/" ;
1618 internal const string ActivitiesUrlPath = "activities/" ;
1719 internal const int ActivityCopyLimitDefault = 300 ;
1820 internal const int ActivityCopyLimitMax = 1000 ;
@@ -38,9 +40,23 @@ public StreamClient(string apiKey, string apiSecret, StreamClientOptions options
3840 _apiKey = apiKey ;
3941 _apiSecret = apiSecret ;
4042 _options = options ?? StreamClientOptions . Default ;
41- _client = new RestClient ( GetBaseUrl ( ) , TimeSpan . FromMilliseconds ( _options . Timeout ) ) ;
43+ _client = new RestClient ( GetBaseUrl ( _options . Location ) , TimeSpan . FromMilliseconds ( _options . Timeout ) ) ;
4244 }
4345
46+ private StreamClient ( string apiKey , string apiSecret , RestClient client , StreamClientOptions options = null )
47+ {
48+ if ( string . IsNullOrWhiteSpace ( apiKey ) )
49+ throw new ArgumentNullException ( "apiKey" , "Must have an apiKey" ) ;
50+ if ( string . IsNullOrWhiteSpace ( apiSecret ) )
51+ throw new ArgumentNullException ( "apiSecret" , "Must have an apiSecret" ) ;
52+
53+ _apiKey = apiKey ;
54+ _apiSecret = apiSecret ;
55+ _options = options ?? StreamClientOptions . Default ;
56+ _client = client ;
57+ }
58+
59+
4460 /// <summary>
4561 /// Get a feed
4662 /// </summary>
@@ -124,39 +140,50 @@ public Users Users
124140 }
125141 }
126142
127- private Uri GetBaseUrl ( )
143+ public Personalization Personalization
128144 {
129- string region = "" ;
130- switch ( _options . Location )
145+ get
146+ {
147+ var _personalization = new RestClient ( GetBasePersonalizationUrl ( _options . PersonalizationLocation ) , TimeSpan . FromMilliseconds ( _options . PersonalizationTimeout ) ) ;
148+ return new Personalization ( new StreamClient ( _apiKey , _apiSecret , _personalization , _options ) ) ;
149+ }
150+ }
151+
152+ private Uri GetBaseUrl ( StreamApiLocation location )
153+ {
154+ return new Uri ( string . Format ( BaseUrlFormat , GetRegion ( _options . Location ) ) ) ;
155+ }
156+
157+ private Uri GetBasePersonalizationUrl ( StreamApiLocation location )
158+ {
159+ return new Uri ( string . Format ( BasePersonalizationUrlFormat , GetRegion ( _options . PersonalizationLocation ) ) ) ;
160+ }
161+
162+ private string GetRegion ( StreamApiLocation location )
163+ {
164+ switch ( location )
131165 {
132166 case StreamApiLocation . USEast :
133- region = "us-east" ;
134- break ;
167+ return "us-east" ;
135168 case StreamApiLocation . Tokyo :
136- region = "tokyo" ;
137- break ;
169+ return "tokyo" ;
138170 case StreamApiLocation . Dublin :
139- region = "dublin" ;
140- break ;
171+ return "dublin" ;
141172 case StreamApiLocation . Singapore :
142- region = "singapore" ;
143- break ;
173+ return "singapore" ;
144174 case StreamApiLocation . USWest :
145- region = "us-west" ;
146- break ;
175+ return "us-west" ;
147176 case StreamApiLocation . EUCentral :
148- region = "eu-central" ;
149- break ;
177+ return "eu-central" ;
150178 default :
151- break ;
179+ return "us-east" ;
152180 }
153- return new Uri ( string . Format ( BaseUrlFormat , region ) ) ;
154181 }
155182
156- private RestRequest BuildRestRequest ( string fullPath , HttpMethod method )
183+ private RestRequest BuildRestRequest ( string fullPath , HttpMethod method , string userID = null )
157184 {
158185 var request = new RestRequest ( fullPath , method ) ;
159- request . AddHeader ( "Authorization" , JWToken ( "*" ) ) ;
186+ request . AddHeader ( "Authorization" , JWToken ( "*" , userID ) ) ;
160187 request . AddHeader ( "stream-auth-type" , "jwt" ) ;
161188 request . AddQueryParameter ( "api_key" , _apiKey ) ;
162189 return request ;
@@ -189,6 +216,11 @@ internal RestRequest BuildAppRequest(string path, HttpMethod method)
189216 return request ;
190217 }
191218
219+ internal RestRequest BuildPersonalizationRequest ( string path , HttpMethod method )
220+ {
221+ return BuildRestRequest ( BasePersonalizationUrlPath + path , method , "*" ) ;
222+ }
223+
192224 internal void SignRequest ( RestRequest request )
193225 {
194226 // make signature
@@ -237,14 +269,18 @@ internal string Sign256(string feedId)
237269 return Convert . ToBase64String ( hmac . ComputeHash ( encoding . GetBytes ( feedId ) ) ) ;
238270 }
239271
240- internal string JWToken ( string feedId )
272+ internal string JWToken ( string feedId , string userID = null )
241273 {
242- var payload = new
274+ var payload = new Dictionary < string , string > ( )
243275 {
244- resource = "*" ,
245- action = "*" ,
246- feed_id = feedId
276+ { " resource" , "*" } ,
277+ { " action" , "*" } ,
278+ { " feed_id" , feedId }
247279 } ;
280+ if ( userID != null )
281+ {
282+ payload [ "user_id" ] = userID ;
283+ }
248284 return this . JWToken ( payload ) ;
249285 }
250286
0 commit comments