-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cs
More file actions
159 lines (135 loc) Β· 4.9 KB
/
example.cs
File metadata and controls
159 lines (135 loc) Β· 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using GetStream;
using GetStream.Models;
/// <summary>
/// Stream .NET SDK Test using getstream-net v1.5.0
/// Tests basic functionality and activity creation
/// </summary>
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("π§ Setting up test environment...");
// API credentials
const string apiKey = "XXXXXX";
const string apiSecret = "XXXXXX";
try
{
// Initialize Stream client using getstream-net v1.5.0
var builder = new ClientBuilder()
.ApiKey(apiKey)
.ApiSecret(apiSecret);
var client = builder.Build();
var feedsClient = builder.BuildFeedsClient();
Console.WriteLine("β
Initialized Stream .NET client");
// Create a test user
var testUserId = $"test-user-{Guid.NewGuid().ToString("N")[..8]}";
Console.WriteLine($"β
Test user ID: {testUserId}");
// Create test user
await CreateUser(client, testUserId);
// Create user feed
var testFeedId = await CreateFeed(feedsClient, testUserId);
// Add an activity
var activityId = await AddActivity(feedsClient, testUserId, testFeedId);
// Query activities
await QueryActivities(feedsClient, testUserId);
// Get single activity
await GetSingleActivity(feedsClient, activityId);
Console.WriteLine("β
Test completed successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"β Error: {ex.Message}");
if (ex.InnerException != null)
{
Console.WriteLine($" Inner Exception: {ex.InnerException.Message}");
}
Environment.Exit(1);
}
}
static async Task CreateUser(StreamClient client, string userId)
{
var updateUsersRequest = new UpdateUsersRequest
{
Users = new Dictionary<string, UserRequest>
{
[userId] = new UserRequest
{
ID = userId,
Name = "Test User",
Role = "user"
}
}
};
await client.UpdateUsersAsync(updateUsersRequest);
Console.WriteLine("β
Created test user");
}
static async Task<string> CreateFeed(FeedsV3Client feedsClient, string userId)
{
var feedId = $"user-{userId}";
var feedRequest = new GetOrCreateFeedRequest
{
UserID = userId
};
var feedResponse = await feedsClient.GetOrCreateFeedAsync(
"user",
feedId,
feedRequest
);
var createdFeedId = feedResponse.Data?.Feed?.Feed ?? feedId;
Console.WriteLine($"β
Created user feed: {createdFeedId}");
return createdFeedId;
}
static async Task<string> AddActivity(FeedsV3Client feedsClient, string userId, string feedId)
{
var activity = new AddActivityRequest
{
Type = "post",
Text = "This is a test activity from .NET SDK v1.5.0",
UserID = userId,
Feeds = new List<string> { feedId }
};
var activityResponse = await feedsClient.AddActivityAsync(activity);
var activityId = activityResponse.Data?.Activity?.ID ?? "unknown";
Console.WriteLine($"β
Created activity with ID: {activityId}");
return activityId;
}
static async Task QueryActivities(FeedsV3Client feedsClient, string userId)
{
var filter = new Dictionary<string, object>
{
["user_id"] = userId
};
var queryRequest = new QueryActivitiesRequest
{
Filter = filter,
Limit = 10
};
var queryResponse = await feedsClient.QueryActivitiesAsync(queryRequest);
var activities = queryResponse.Data?.Activities ?? new List<ActivityResponse>();
Console.WriteLine($"β
Retrieved {activities.Count} activities:");
foreach (var activity in activities)
{
Console.WriteLine($" - ID: {activity.ID}, Type: {activity.Type}, Text: {activity.Text}");
}
}
static async Task GetSingleActivity(FeedsV3Client feedsClient, string activityId)
{
try
{
var getActivityResponse = await feedsClient.GetActivityAsync(activityId);
var activity = getActivityResponse.Data?.Activity;
if (activity != null)
{
Console.WriteLine($"β
Retrieved single activity: {activity.Text}");
}
else
{
Console.WriteLine("β οΈ Activity not found or empty response");
}
}
catch (Exception ex)
{
Console.WriteLine($"β οΈ Failed to retrieve single activity: {ex.Message}");
}
}
}