-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (113 loc) · 4.64 KB
/
Copy pathProgram.cs
File metadata and controls
136 lines (113 loc) · 4.64 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
using System.Text.Json;
using Fingerprint.ServerSdk.Api;
using Fingerprint.ServerSdk.Client;
using Fingerprint.ServerSdk.Extensions;
using Fingerprint.ServerSdk.Model;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Fingerprint.ServerSdk.Examples;
public static class Program
{
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
var api = host.Services.GetRequiredService<IFingerprintApi>();
await SearchEventsExample(api);
var eventId = Environment.GetEnvironmentVariable("EVENT_ID");
var rulesetId = Environment.GetEnvironmentVariable("RULESET_ID");
if (!string.IsNullOrWhiteSpace(eventId))
{
await GetEventExample(api, eventId, rulesetId);
}
var updateEventId = Environment.GetEnvironmentVariable("UPDATE_EVENT_ID");
if (!string.IsNullOrWhiteSpace(updateEventId))
{
await UpdateEventExample(api, updateEventId);
}
var visitorId = Environment.GetEnvironmentVariable("VISITOR_ID");
if (!string.IsNullOrWhiteSpace(visitorId))
{
await DeleteVisitorDataExample(api, visitorId);
}
}
private static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.ConfigureFingerprint((_, _, options) =>
{
var token = Environment.GetEnvironmentVariable("SECRET_API_KEY");
if (string.IsNullOrWhiteSpace(token))
{
throw new InvalidOperationException("SECRET_API_KEY is missing");
}
var region = Environment.GetEnvironmentVariable("REGION");
if (!string.IsNullOrWhiteSpace(region))
{
options.Region = Regions.Parse(region);
}
var bearerToken = new BearerToken(token);
options.AddTokens(bearerToken);
});
private static async Task SearchEventsExample(IFingerprintApi api)
{
var events = await api.SearchEventsAsync(new SearchEventsRequest()
.WithLimit(2)
.WithBot(SearchEventsBot.Bad));
if (! events.IsOk)
{
throw new ExampleException("SearchEventsAsync failed", events);
}
Console.WriteLine("SearchEventsAsync result:");
Console.WriteLine(JsonSerializer.Serialize(events.Ok()));
}
private static async Task GetEventExample(IFingerprintApi api, string eventId, string? rulesetId = null)
{
var model = await api.GetEventAsync(eventId: eventId, rulesetId: rulesetId);
if (!model.IsOk)
{
throw new ExampleException("GetEventAsync failed", model);
}
Console.WriteLine("GetEventAsync result:");
var result = model.Ok();
Console.WriteLine(JsonSerializer.Serialize(result));
if (rulesetId != null)
{
Console.WriteLine("GetEventAsync ruleset evaluation:");
var allow = result.RuleAction.EventRuleActionAllow;
var block = result.RuleAction.EventRuleActionBlock;
if (allow != null)
{
Console.WriteLine("Action type: 'allow'");
Console.WriteLine($"Request header modifications: `{JsonSerializer.Serialize(allow.RequestHeaderModifications)}`");
}
else if (block != null)
{
Console.WriteLine("Action type: `block`");
Console.WriteLine($"Response body: `{block.Body}`");
Console.WriteLine($"Response status code: `{block.StatusCode}`");
Console.WriteLine($"Headers: `{JsonSerializer.Serialize(block.Headers)}`");
}
}
}
private static async Task UpdateEventExample(IFingerprintApi api, string eventId)
{
var tags = new Dictionary<string, object>
{
{ "sdk", "dotnet" }
};
var body = new EventUpdate(linkedId: "<linked_id>", tags: tags, suspect: false);
var updateEvent = await api.UpdateEventAsync(eventId: eventId, eventUpdate: body);
if (! updateEvent.IsOk)
{
throw new ExampleException("UpdateEventAsync failed", updateEvent);
}
Console.WriteLine("Event updated");
}
private static async Task DeleteVisitorDataExample(IFingerprintApi api, string visitorId)
{
var deleteVisitorData = await api.DeleteVisitorDataAsync(visitorId: visitorId);
if (! deleteVisitorData.IsOk)
{
throw new ExampleException("DeleteVisitorDataAsync failed", deleteVisitorData);
}
Console.WriteLine("Scheduled visitor data removal");
}
}