Add a user agent to the Xtream client #159

Merged
Kevinjil merged 1 commits from feature/user-agent into master 2025-02-10 08:36:07 +00:00

View File

@@ -16,6 +16,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Jellyfin.Xtream.Client.Models; using Jellyfin.Xtream.Client.Models;
@@ -36,10 +38,21 @@ public class XtreamClient(HttpClient client) : IDisposable
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="XtreamClient"/> class. /// Initializes a new instance of the <see cref="XtreamClient"/> class.
/// </summary> /// </summary>
public XtreamClient() : this(new HttpClient()) public XtreamClient() : this(CreateDefaultClient())
{ {
} }
private static HttpClient CreateDefaultClient()
{
HttpClient client = new HttpClient();
ProductHeaderValue header = new ProductHeaderValue("Jellyfin.Xtream", Assembly.GetExecutingAssembly().GetName().Version?.ToString());
ProductInfoHeaderValue userAgent = new ProductInfoHeaderValue(header);
client.DefaultRequestHeaders.UserAgent.Add(userAgent);
return client;
}
private async Task<T> QueryApi<T>(ConnectionInfo connectionInfo, string urlPath, CancellationToken cancellationToken) private async Task<T> QueryApi<T>(ConnectionInfo connectionInfo, string urlPath, CancellationToken cancellationToken)
{ {
Uri uri = new Uri(connectionInfo.BaseUrl + urlPath); Uri uri = new Uri(connectionInfo.BaseUrl + urlPath);