Skip to content

Commit 5de8ba4

Browse files
fix indentation (#385)
Co-authored-by: GoWMan813 <[email protected]>
1 parent ce8f75e commit 5de8ba4

File tree

1 file changed

+111
-111
lines changed

1 file changed

+111
-111
lines changed

streamerbot/4.examples/http-post.md

Lines changed: 111 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -20,172 +20,172 @@ Read more about the `Execute C# Code` sub-action
2020

2121
1. Basic understanding of writing C# code
2222

23-
Since all of this *has* to be implemented in a C# code module, basic understanding of writing C# code and using common libraries is required. I will only give the most basic example, you will likely need to adapt it to your usecase.
23+
Since all of this *has* to be implemented in a C# code module, basic understanding of writing C# code and using common libraries is required. I will only give the most basic example, you will likely need to adapt it to your use case.
2424

2525
2. Understanding on how to use C# in sub-actions
2626

27-
Be it a single use `Execute C# Code` block for a single instance of needing to send a request, or a more persistent C# module with different actions triggering different `Execute C# Method` sub-actions - you need to know how use this feature in your normal action flow.
27+
Be it a single use `Execute C# Code` block for a single instance of needing to send a request, or a more persistent C# module with different actions triggering different `Execute C# Method` sub-actions - you need to know how use this feature in your normal action flow.
2828

29-
The following tutorial will only focus on the magic happening inside of the C# code module.
29+
The following tutorial will only focus on the magic happening inside of the C# code module.
3030

3131
## Instructions
3232

33-
1. Setup the HTTPClient
33+
1. Set up the HTTPClient
3434

35-
```cs [Basic Setup]
36-
using System;
37-
using System.Text;
38-
using System.Threading.Tasks;
39-
using System.Net.Http;
40-
using System.Net.Http.Headers;
41-
using Newtonsoft.Json;
42-
using Newtonsoft.Json.Linq;
35+
```cs [Basic Setup]
36+
using System;
37+
using System.Text;
38+
using System.Threading.Tasks;
39+
using System.Net.Http;
40+
using System.Net.Http.Headers;
41+
using Newtonsoft.Json;
42+
using Newtonsoft.Json.Linq;
4343

44-
public class CPHInline {
45-
// You can set the timeout to any length you need
46-
private static readonly HttpClient _httpClient = new HttpClient{Timeout = TimeSpan.FromSeconds(30)};
44+
public class CPHInline {
45+
// You can set the timeout to any length you need
46+
private static readonly HttpClient _httpClient = new HttpClient{Timeout = TimeSpan.FromSeconds(30)};
4747

48-
public void Init() {
49-
// Ensure we are working with a clean slate
50-
_httpClient.DefaultRequestHeaders.Clear();
51-
}
48+
public void Init() {
49+
// Ensure we are working with a clean slate
50+
_httpClient.DefaultRequestHeaders.Clear();
51+
}
5252

53-
public void Dispose() {
54-
// Free up allocations
55-
_httpClient.Dispose();
56-
}
53+
public void Dispose() {
54+
// Free up allocations
55+
_httpClient.Dispose();
56+
}
5757

58-
public bool Execute() {
59-
// ...
60-
return true;
61-
}
58+
public bool Execute() {
59+
// ...
60+
return true;
6261
}
63-
```
62+
}
63+
```
6464

65-
::tip
66-
While people using the HTTPClient in a `using` statement or creating a new one within the executed function may be a common occurence, it is heavily discouraged to do so. [Read More](https://extensions.streamer.bot/t/httpclient-and-you/1369)
67-
::
65+
::tip
66+
While people using the HTTPClient in a `using` statement or creating a new one within the executed function may be a common occurence, it is heavily discouraged to do so. [Read More](https://extensions.streamer.bot/t/httpclient-and-you/1369)
67+
::
6868

6969
2. Setup the sending of your payload
7070

71-
We are going to send a `%userName%` and `%userId%` pair (from common triggers) as `PUT` request to our fictional logging website.
71+
We are going to send a `%userName%` and `%userId%` pair (from common triggers) as `PUT` request to our fictional logging website.
7272

73-
```cs [Send PUT payload in Execute method]
74-
public bool Execute() {
75-
// Get the arguments - or abort if it does not exist
76-
if(!CPH.TryGetArg("userName", out string userName)) return false;
77-
if(!CPH.TryGetArg("userId", out string userId)) return false;
73+
```cs [Send PUT payload in Execute method]
74+
public bool Execute() {
75+
// Get the arguments - or abort if it does not exist
76+
if(!CPH.TryGetArg("userName", out string userName)) return false;
77+
if(!CPH.TryGetArg("userId", out string userId)) return false;
7878

79-
// Build a wrapper object as payload
80-
var data = new {
81-
id = userId,
82-
name = userName
83-
};
79+
// Build a wrapper object as payload
80+
var data = new {
81+
id = userId,
82+
name = userName
83+
};
8484

85-
// Serialize JSON
86-
string json = JsonConvert.SerializeObject(data);
85+
// Serialize JSON
86+
string json = JsonConvert.SerializeObject(data);
8787

88-
// Create web request payload
89-
var payload = new StringContent(json, Encoding.UTF8, "application/json");
88+
// Create web request payload
89+
var payload = new StringContent(json, Encoding.UTF8, "application/json");
9090

91-
// Finally, send request
92-
HttpResponseMessage response = _httpClient.PutAsync("https://my-logging-server.com", payload).GetAwaiter().GetResult();
91+
// Finally, send request
92+
HttpResponseMessage response = _httpClient.PutAsync("https://my-logging-server.com", payload).GetAwaiter().GetResult();
9393

94-
// Do with the response what you need to.
95-
// ...
94+
// Do with the response what you need to.
95+
// ...
9696
97-
return true;
98-
}
99-
```
97+
return true;
98+
}
99+
```
100100

101-
To use different request types, simply use the methods `GetAsync()`, `PutAsync()`, `PostAsync()` or `DeleteAsync()`.
101+
To use different request types, simply use the methods `GetAsync()`, `PutAsync()`, `PostAsync()` or `DeleteAsync()`.
102102

103103
3. The PATCH request
104104

105-
Specifically the built-in handling for the `PATCH` request type may not be available in the .net versions used by StreamerBot. In this case, you need a workaround.
105+
Specifically the built-in handling for the `PATCH` request type may not be available in the .net versions used by StreamerBot. In this case, you need a workaround.
106106

107-
```cs [PATCH workaround]
108-
// ...
107+
```cs [PATCH workaround]
108+
// ...
109109
110-
// Finally, send the request
111-
var request = new HttpRequestMessage(new HttpMethod("PATCH"), "https://my-logging-server.com"){
112-
Content = payload
113-
};
114-
HttpResponseMessage response = _httpClient.SendAsync(request).GetAwaiter().GetResult();
110+
// Finally, send the request
111+
var request = new HttpRequestMessage(new HttpMethod("PATCH"), "https://my-logging-server.com"){
112+
Content = payload
113+
};
114+
HttpResponseMessage response = _httpClient.SendAsync(request).GetAwaiter().GetResult();
115115

116-
// ...
117-
```
116+
// ...
117+
```
118118

119119
4. Additional Headers
120120

121-
If you need to additionally include specific headers - like submitting an authorization token for the Twitch API or Discord API - make sure to clear and set the headers on each call you make.
121+
If you need to additionally include specific headers - like submitting an authorization token for the Twitch API or Discord API - make sure to clear and set the headers on each call you make.
122122

123-
You can also send individual headers with each request itself, but that requires you to always use the `SendAsync` method and building your own `HttpRequestMessage`.
123+
You can also send individual headers with each request itself, but that requires you to always use the `SendAsync` method and building your own `HttpRequestMessage`.
124124

125-
If your code only does a static request whose headers never change, resetting the headers is **not strictly necessary**. But it's good practice to not forget later when you need it.
125+
If your code only does a static request whose headers never change, resetting the headers is **not strictly necessary**. But it's good practice to not forget later when you need it.
126126

127-
```cs [Header management]
128-
public bool Execute() {
129-
// Making a call to the Twitch API to get all currently live streams
130-
string token = CPH.TwitchOAuthToken;
131-
string clientId = CPH.TwitchClientId;
127+
```cs [Header management]
128+
public bool Execute() {
129+
// Making a call to the Twitch API to get all currently live streams
130+
string token = CPH.TwitchOAuthToken;
131+
string clientId = CPH.TwitchClientId;
132132

133-
// Clear our old headers
134-
_httpClient.DefaultRequestHeaders.Clear();
133+
// Clear our old headers
134+
_httpClient.DefaultRequestHeaders.Clear();
135135

136-
// Now set your Twitch API authorization
137-
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer "+token);
138-
_httpClient.DefaultRequestHeaders.Add("Client-Id", clientId);
136+
// Now set your Twitch API authorization
137+
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer "+token);
138+
_httpClient.DefaultRequestHeaders.Add("Client-Id", clientId);
139139

140-
// Send your request
141-
HttpResponseMessage response = _httpClient.GetAsync("https://api.twitch.tv/helix/streams").GetAwaiter().GetResult();
140+
// Send your request
141+
HttpResponseMessage response = _httpClient.GetAsync("https://api.twitch.tv/helix/streams").GetAwaiter().GetResult();
142142

143-
// Do with the response what you need to.
144-
// ...
143+
// Do with the response what you need to.
144+
// ...
145145
146-
return true;
147-
}
148-
```
146+
return true;
147+
}
148+
```
149149

150150
5. Handling the response
151151

152-
Since you have to do the entire request handling yourself, that includes handling the response.
152+
Since you have to do the entire request handling yourself, that includes handling the response.
153153

154-
- If you don't need to know what the server responded, you can ignore it like in the examples above.
154+
- If you don't need to know what the server responded, you can ignore it like in the examples above.
155155

156-
- If you need to know if the request succeeded or are expecting data in return, follow the below example.
156+
- If you need to know if the request succeeded or are expecting data in return, follow the below example.
157157

158-
```cs [Response handling]
159-
public bool Execute() {
160-
// ...all your data preparation
158+
```cs [Response handling]
159+
public bool Execute() {
160+
// ...all your data preparation
161161
162-
HttpResponseMessage response = _httpClient.GetAsync("https://api.twitch.tv/helix/streams").GetAwaiter().GetResult();
162+
HttpResponseMessage response = _httpClient.GetAsync("https://api.twitch.tv/helix/streams").GetAwaiter().GetResult();
163163

164-
// Check if our request was successful
165-
try {
166-
if(!response.IsSuccessStatusCode) {
167-
// It was not. Abort.
168-
CPH.LogError($"{response.StatusCode} - {response.ReasonPhrase}");
169-
return false;
170-
}
164+
// Check if our request was successful
165+
try {
166+
if(!response.IsSuccessStatusCode) {
167+
// It was not. Abort.
168+
CPH.LogError($"{response.StatusCode} - {response.ReasonPhrase}");
169+
return false;
170+
}
171171

172-
// Get the response data
173-
string content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
172+
// Get the response data
173+
string content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
174174

175-
// Usually, data is JSON. So you would decode it now.
176-
JObject parsed = JObject.Parse(content);
175+
// Usually, data is JSON. So you would decode it now.
176+
JObject parsed = JObject.Parse(content);
177177

178-
// And then do with the data whatever you need.
179-
// ...
178+
// And then do with the data whatever you need.
179+
// ...
180180
181-
return true;
182-
} catch (Exception e) {
183-
// Something went wrong
184-
CPH.LogError(e.Message);
185-
return false;
186-
}
181+
return true;
182+
} catch (Exception e) {
183+
// Something went wrong
184+
CPH.LogError(e.Message);
185+
return false;
187186
}
188-
```
187+
}
188+
```
189189

190190
## Tips & Tricks
191191

0 commit comments

Comments
 (0)