wabbajack/Wabbajack.Common/Http/Client.cs

153 lines
5.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-02-26 04:00:28 +00:00
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
2020-04-08 12:43:29 +00:00
using HtmlAgilityPack;
using Wabbajack.Common.Exceptions;
namespace Wabbajack.Common.Http
{
public class Client
{
2020-04-10 01:29:53 +00:00
public List<(string, string?)> Headers = new List<(string, string?)>();
public List<Cookie> Cookies = new List<Cookie>();
public async Task<HttpResponseMessage> GetAsync(string url, HttpCompletionOption responseHeadersRead = HttpCompletionOption.ResponseHeadersRead, bool errorsAsExceptions = true)
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
return await SendAsync(request, responseHeadersRead, errorsAsExceptions: errorsAsExceptions);
}
2020-05-20 03:25:41 +00:00
public async Task<HttpResponseMessage> GetAsync(Uri url, HttpCompletionOption responseHeadersRead = HttpCompletionOption.ResponseHeadersRead, bool errorsAsExceptions = true)
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
return await SendAsync(request, responseHeadersRead, errorsAsExceptions: errorsAsExceptions);
}
2020-02-26 04:00:28 +00:00
2020-05-20 03:25:41 +00:00
public async Task<HttpResponseMessage> PostAsync(string url, HttpContent content, HttpCompletionOption responseHeadersRead = HttpCompletionOption.ResponseHeadersRead, bool errorsAsExceptions = true)
2020-02-26 04:00:28 +00:00
{
var request = new HttpRequestMessage(HttpMethod.Post, url) {Content = content};
2020-05-20 03:25:41 +00:00
return await SendAsync(request, responseHeadersRead, errorsAsExceptions);
2020-02-26 04:00:28 +00:00
}
public async Task<HttpResponseMessage> PutAsync(string url, HttpContent content, HttpCompletionOption responseHeadersRead = HttpCompletionOption.ResponseHeadersRead)
{
var request = new HttpRequestMessage(HttpMethod.Put, url) {Content = content};
return await SendAsync(request, responseHeadersRead);
}
public async Task<string> GetStringAsync(string url)
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
return await SendStringAsync(request);
}
public async Task<string> GetStringAsync(Uri url)
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
return await SendStringAsync(request);
}
public async Task<string> DeleteStringAsync(string url)
{
var request = new HttpRequestMessage(HttpMethod.Delete, url);
return await SendStringAsync(request);
}
private async Task<string> SendStringAsync(HttpRequestMessage request)
{
using var result = await SendAsync(request);
if (!result.IsSuccessStatusCode)
{
Utils.Log("Internal Error");
Utils.Log(await result.Content.ReadAsStringAsync());
throw new Exception(
$"Bad HTTP request {result.StatusCode} {result.ReasonPhrase} - {request.RequestUri}");
}
return await result.Content.ReadAsStringAsync();
}
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage msg, HttpCompletionOption responseHeadersRead = HttpCompletionOption.ResponseHeadersRead, bool errorsAsExceptions = true)
{
2020-02-28 02:26:58 +00:00
foreach (var (k, v) in Headers)
msg.Headers.Add(k, v);
if (Cookies.Count > 0)
Cookies.ForEach(c => ClientFactory.Cookies.Add(c));
int retries = 0;
2020-05-23 21:03:25 +00:00
HttpResponseMessage response;
TOP:
try
{
2020-05-23 21:03:25 +00:00
response = await ClientFactory.Client.SendAsync(msg, responseHeadersRead);
2020-05-09 22:16:16 +00:00
if (response.IsSuccessStatusCode) return response;
2020-05-15 03:52:23 +00:00
if (errorsAsExceptions)
2020-05-23 21:03:25 +00:00
{
response.Dispose();
throw new HttpException(response);
2020-05-23 21:03:25 +00:00
}
return response;
}
2020-05-15 03:52:23 +00:00
catch (Exception ex)
{
if (ex is HttpException http)
{
2020-06-14 18:43:09 +00:00
if (http.Code != 503) throw;
2020-05-23 21:03:25 +00:00
retries++;
var ms = Utils.NextRandom(100, 1000);
Utils.Log($"Got a 503 from {msg.RequestUri} retrying in {ms}ms");
await Task.Delay(ms);
2020-05-23 21:03:25 +00:00
msg = CloneMessage(msg);
goto TOP;
}
if (retries > Consts.MaxHTTPRetries) throw;
retries++;
Utils.Log($"Http Connect error to {msg.RequestUri} retry {retries}");
await Task.Delay(100 * retries);
2020-02-15 23:28:20 +00:00
msg = CloneMessage(msg);
goto TOP;
}
}
2020-02-15 23:28:20 +00:00
private HttpRequestMessage CloneMessage(HttpRequestMessage msg)
{
var new_message = new HttpRequestMessage(msg.Method, msg.RequestUri);
foreach (var header in msg.Headers)
new_message.Headers.Add(header.Key, header.Value);
new_message.Content = msg.Content;
return new_message;
}
2020-04-02 21:16:46 +00:00
public async Task<T> GetJsonAsync<T>(string s)
{
var result = await GetStringAsync(s);
return result.FromJsonString<T>();
2020-04-02 21:16:46 +00:00
}
2020-04-08 12:43:29 +00:00
public async Task<HtmlDocument> GetHtmlAsync(string s)
{
var body = await GetStringAsync(s);
var doc = new HtmlDocument();
doc.LoadHtml(body);
return doc;
}
2020-06-16 22:21:01 +00:00
public Client WithHeader((string MetricsKeyHeader, string) header)
{
var newHeaders = Headers.Cons(header).ToList();
var client = new Client {Headers = newHeaders, Cookies = Cookies,};
return client;
}
}
}