@@ -163,9 +163,135 @@ events = [impression, engagement]
163163redirect_url = client.create_redirect_url(' http://google.com/' , ' user_id' , events)
164164```
165165
166+ ### Async code usage
167+ ``` python
168+ import datetime
169+ import stream
170+ client = stream.connect(' YOUR_API_KEY' , ' API_KEY_SECRET' , use_async = True )
171+
172+
173+ # Create a new client specifying data center location
174+ client = stream.connect(' YOUR_API_KEY' , ' API_KEY_SECRET' , location = ' us-east' , use_async = True )
175+ # Find your API keys here https://getstream.io/dashboard/
176+
177+ # Create a feed object
178+ user_feed_1 = client.feed(' user' , ' 1' )
179+
180+ # Get activities from 5 to 10 (slow pagination)
181+ result = await user_feed_1.get(limit = 5 , offset = 5 )
182+ # (Recommended & faster) Filter on an id less than the given UUID
183+ result = await user_feed_1.get(limit = 5 , id_lt = " e561de8f-00f1-11e4-b400-0cc47a024be0" )
184+
185+ # Create a new activity
186+ activity_data = {' actor' : 1 , ' verb' : ' tweet' , ' object' : 1 , ' foreign_id' : ' tweet:1' }
187+ activity_response = await user_feed_1.add_activity(activity_data)
188+ # Create a bit more complex activity
189+ activity_data = {' actor' : 1 , ' verb' : ' run' , ' object' : 1 , ' foreign_id' : ' run:1' ,
190+ ' course' : {' name' : ' Golden Gate park' , ' distance' : 10 },
191+ ' participants' : [' Thierry' , ' Tommaso' ],
192+ ' started_at' : datetime.datetime.now()
193+ }
194+ await user_feed_1.add_activity(activity_data)
195+
196+ # Remove an activity by its id
197+ await user_feed_1.remove_activity(" e561de8f-00f1-11e4-b400-0cc47a024be0" )
198+ # or by foreign id
199+ await user_feed_1.remove_activity(foreign_id = ' tweet:1' )
200+
201+ # Follow another feed
202+ await user_feed_1.follow(' flat' , ' 42' )
203+
204+ # Stop following another feed
205+ await user_feed_1.unfollow(' flat' , ' 42' )
206+
207+ # List followers/following
208+ following = await user_feed_1.following(offset = 0 , limit = 2 )
209+ followers = await user_feed_1.followers(offset = 0 , limit = 10 )
210+
211+ # Creates many follow relationships in one request
212+ follows = [
213+ {' source' : ' flat:1' , ' target' : ' user:1' },
214+ {' source' : ' flat:1' , ' target' : ' user:2' },
215+ {' source' : ' flat:1' , ' target' : ' user:3' }
216+ ]
217+ await client.follow_many(follows)
218+
219+ # Batch adding activities
220+ activities = [
221+ {' actor' : 1 , ' verb' : ' tweet' , ' object' : 1 },
222+ {' actor' : 2 , ' verb' : ' watch' , ' object' : 3 }
223+ ]
224+ await user_feed_1.add_activities(activities)
225+
226+ # Add an activity and push it to other feeds too using the `to` field
227+ activity = {
228+ " actor" :" 1" ,
229+ " verb" :" like" ,
230+ " object" :" 3" ,
231+ " to" :[" user:44" , " user:45" ]
232+ }
233+ await user_feed_1.add_activity(activity)
234+
235+ # Retrieve an activity by its ID
236+ await client.get_activities(ids = [activity_id])
237+
238+ # Retrieve an activity by the combination of foreign_id and time
239+ await client.get_activities(foreign_id_times = [
240+ (foreign_id, activity_time),
241+ ])
242+
243+ # Enrich while getting activities
244+ await client.get_activities(ids = [activity_id], enrich = True , reactions = {" counts" : True })
245+
246+ # Update some parts of an activity with activity_partial_update
247+ set = {
248+ ' product.name' : ' boots' ,
249+ ' colors' : {
250+ ' red' : ' 0xFF0000' ,
251+ ' green' : ' 0x00FF00'
252+ }
253+ }
254+ unset = [ ' popularity' , ' details.info' ]
255+ # ...by ID
256+ await client.activity_partial_update(id = activity_id, set = set , unset = unset)
257+ # ...or by combination of foreign_id and time
258+ await client.activity_partial_update(foreign_id = foreign_id, time = activity_time, set = set , unset = unset)
259+
260+ # Generating user token for client side usage (JS client)
261+ user_token = client.create_user_token(" user-42" )
262+
263+ # Javascript client side feed initialization
264+ # client = stream.connect(apiKey, userToken, appId);
265+
266+ # Generate a redirect url for the Stream Analytics platform to track
267+ # events/impressions on url clicks
268+ impression = {
269+ ' content_list' : [' tweet:1' , ' tweet:2' , ' tweet:3' ],
270+ ' user_data' : ' tommaso' ,
271+ ' location' : ' email' ,
272+ ' feed_id' : ' user:global'
273+ }
274+
275+ engagement = {
276+ ' content' : ' tweet:2' ,
277+ ' label' : ' click' ,
278+ ' position' : 1 ,
279+ ' user_data' : ' tommaso' ,
280+ ' location' : ' email' ,
281+ ' feed_id' :
282+ ' user:global'
283+ }
284+
285+ events = [impression, engagement]
286+
287+ redirect_url = client.create_redirect_url(' http://google.com/' , ' user_id' , events)
288+
289+ ```
290+
166291[ JS client] ( http://github.com/getstream/stream-js ) .
167292
168293## ✍️ Contributing
294+ =======
169295
170296We welcome code changes that improve this library or fix a problem, please make sure to follow all best practices and add tests if applicable before submitting a Pull Request on Github. We are very happy to merge your code in the official repository. Make sure to sign our [ Contributor License Agreement (CLA)] ( https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform ) first. See our [ license file] ( ./LICENSE ) for more details.
171297
0 commit comments