-
Notifications
You must be signed in to change notification settings - Fork 8
/
FetchUtils.cs
137 lines (124 loc) · 3.7 KB
/
FetchUtils.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Spark
{
public static class FetchUtils
{
/// <summary>
/// Client used for one-off requests
/// </summary>
public static readonly HttpClient client = new HttpClient();
/// <summary>
/// Generic method for getting data from a web url
/// </summary>
/// <param name="headers">Key-value pairs for headers. Leave null if none.</param>
public static void GetRequestCallback(string uri, Dictionary<string, string> headers, Action<string> callback)
{
Task.Run(async () =>
{
string resp = await GetRequestAsync(uri, headers);
callback(resp);
});
}
/// <summary>
/// Generic method for getting data from a web url
/// </summary>
/// <param name="uri">The URL to GET</param>
/// <param name="headers">Key-value pairs for headers. Leave null if none.</param>
public static async Task<string> GetRequestAsync(string uri, Dictionary<string, string> headers = null)
{
try
{
using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
if (headers != null)
{
foreach ((string key, string value) in headers)
{
request.Headers.Add(key, value);
}
}
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// Generic method for getting data from a web url. Returns a Stream
/// </summary>
/// <param name="uri">The URL to GET</param>
/// <param name="headers">Key-value pairs for headers. Leave null if none.</param>
public static async Task<Stream> GetRequestAsyncStream(string uri, Dictionary<string, string> headers)
{
try
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
if (headers != null)
{
foreach ((string key, string value) in headers)
{
request.Headers[key] = value;
}
}
request.UserAgent = $"Spark/{Program.AppVersionString()}";
using WebResponse response = await request.GetResponseAsync();
return response.GetResponseStream();
}
catch (Exception e)
{
Console.WriteLine($"Can't get data\n{e}");
}
return null;
}
/// <summary>
/// Generic method for posting data to a web url
/// </summary>
/// <param name="headers">Key-value pairs for headers. Leave null if none.</param>
public static void PostRequestCallback(string uri, Dictionary<string, string> headers, string body, Action<string> callback)
{
Task.Run(async () =>
{
string resp = await PostRequestAsync(uri, headers, body);
callback?.Invoke(resp);
});
}
/// <summary>
/// Generic method for posting data to a web url
/// </summary>
/// <param name="headers">Key-value pairs for headers. Leave null if none.</param>
public static async Task<string> PostRequestAsync(string uri, Dictionary<string, string> headers, string body, bool readResponse = true)
{
try
{
if (headers != null)
{
foreach (KeyValuePair<string, string> header in headers)
{
client.DefaultRequestHeaders.Remove(header.Key);
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
StringContent content = new StringContent(body, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(uri, content);
if (readResponse)
{
return await response.Content.ReadAsStringAsync();
}
return string.Empty;
}
catch (Exception e)
{
Console.WriteLine($"Can't get data\n{e}");
return string.Empty;
}
}
}
}