forked from Mik4sa/Procon-1
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSdkTemplatePlugin.Http.cs
More file actions
128 lines (113 loc) · 3.92 KB
/
SdkTemplatePlugin.Http.cs
File metadata and controls
128 lines (113 loc) · 3.92 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
/*
* SdkTemplatePlugin — HTTP Requests (Partial Class)
*
* Shows two approaches:
* 1. HttpClient (built-in, low-level)
* 2. Flurl (axios-style, fluent API)
*
* Both work. Flurl is cleaner for JSON APIs.
*/
using System;
using System.Net.Http;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Flurl;
using Flurl.Http;
namespace PRoConEvents
{
public partial class SdkTemplatePlugin
{
// =================================================================
// Option 1: HttpClient — built-in, more verbose
// =================================================================
private string HttpClientExample(string playerName)
{
try
{
var client = new HttpClient();
// GET request
string json = client.GetStringAsync(
"https://api.example.com/player?name=" + Uri.EscapeDataString(playerName)
).Result;
var data = JObject.Parse(json);
return data["status"]?.ToString();
}
catch (Exception ex)
{
Log("Info", "HTTP error: {0}", ex.Message);
return null;
}
}
// =================================================================
// Option 2: Flurl — axios-style, fluent API
// =================================================================
private string FlurlExample(string playerName)
{
try
{
// GET with query params
var result = "https://api.example.com/player"
.SetQueryParam("name", playerName)
.GetJsonAsync<JObject>()
.Result;
return result["status"]?.ToString();
}
catch (Exception ex)
{
Log("Info", "HTTP error: {0}", ex.Message);
return null;
}
}
// =================================================================
// More Flurl examples
// =================================================================
private void FlurlShowcase()
{
// GET with headers
var data = "https://api.example.com/data"
.WithHeader("Authorization", "Bearer my-token")
.WithHeader("Accept", "application/json")
.GetJsonAsync<JObject>()
.Result;
// POST JSON body
var response = "https://api.example.com/report"
.PostJsonAsync(new
{
player = "PlayerA",
reason = "Suspicious activity",
timestamp = DateTime.UtcNow
})
.Result;
// POST form data
var formResponse = "https://api.example.com/login"
.PostUrlEncodedAsync(new
{
username = "admin",
password = "secret"
})
.Result;
// GET with timeout
var timedResult = "https://api.example.com/slow"
.WithTimeout(5) // 5 seconds
.GetStringAsync()
.Result;
// Download to string and parse manually
string raw = "https://api.example.com/text"
.GetStringAsync()
.Result;
// Check response status without throwing
try
{
var resp = "https://api.example.com/check"
.GetAsync()
.Result;
int statusCode = resp.StatusCode;
Log("Debug", "API returned status: {0}", statusCode);
}
catch (FlurlHttpException ex)
{
Log("Info", "API error: {0} - {1}", ex.StatusCode, ex.Message);
}
}
}
}