40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using DotBased.Monads;
|
|
using Manager.YouTube.Util;
|
|
|
|
namespace Manager.YouTube;
|
|
|
|
public static class NetworkService
|
|
{
|
|
public const string Origin = "https://www.youtube.com";
|
|
|
|
public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client)
|
|
{
|
|
request.Headers.Add("Origin", Origin);
|
|
request.Headers.UserAgent.ParseAdd(client.UserAgent);
|
|
if (client.SapisidCookie != null)
|
|
{
|
|
request.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.External.GetDatasyncId(), client.SapisidCookie.Value, Origin);
|
|
}
|
|
|
|
var httpClient = client.GetHttpClient();
|
|
if (httpClient == null)
|
|
{
|
|
return ResultError.Fail("Failed getting http client!");
|
|
}
|
|
|
|
try
|
|
{
|
|
var response = await httpClient.SendAsync(request);
|
|
var contentString = await response.Content.ReadAsStringAsync();
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
return ResultError.Fail(contentString);
|
|
}
|
|
return contentString;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return ResultError.Error(e);
|
|
}
|
|
}
|
|
} |