switched to using api tokens instead of login credentials
This commit is contained in:
parent
86f273d12d
commit
226caf4c1e
13 changed files with 215 additions and 85 deletions
|
|
@ -10,20 +10,19 @@ namespace JellyGlass.Repositories;
|
|||
|
||||
public class JellyfinApiClient
|
||||
{
|
||||
private string _apiKey = string.Empty;
|
||||
private readonly string _apiKey = string.Empty;
|
||||
public readonly string InstanceUrl;
|
||||
private readonly HttpClient _client;
|
||||
private readonly string _username, _password;
|
||||
|
||||
public string ID { get; private set; } = string.Empty;
|
||||
public string ServerName { get; private set; } = string.Empty;
|
||||
|
||||
public JellyfinApiClient(string instanceUrl, string username, string password)
|
||||
public JellyfinApiClient(string instanceUrl, string apiToken)
|
||||
{
|
||||
InstanceUrl = instanceUrl;
|
||||
_client = new HttpClient();
|
||||
_client.DefaultRequestHeaders.Clear();
|
||||
_username = username;
|
||||
_password = password;
|
||||
_apiKey = apiToken;
|
||||
}
|
||||
|
||||
public async Task<Item[]> GetInstanceLibraries()
|
||||
|
|
@ -37,7 +36,7 @@ public class JellyfinApiClient
|
|||
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ItemResponse>();
|
||||
|
||||
return apiResponse.Items.ToArray();
|
||||
return apiResponse!.Items.ToArray();
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
|
|
@ -47,76 +46,98 @@ public class JellyfinApiClient
|
|||
|
||||
public async Task<Item[]> GetItemChildren(string itemId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{InstanceUrl}/items?ParentId={itemId}");
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{InstanceUrl}/items?ParentId={itemId}");
|
||||
|
||||
var response = await MakeRequest(request);
|
||||
var response = await MakeRequest(request);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ItemResponse>();
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ItemResponse>();
|
||||
|
||||
return apiResponse!.Items.ToArray();
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
throw new JellyfinApiClientException(e.Message);
|
||||
}
|
||||
return apiResponse!.Items.ToArray();
|
||||
}
|
||||
|
||||
public async Task<Item[]> GetItems(string searchTerm = "", string years = "", string itemTypes = "", string limit = "", string parentId = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{InstanceUrl}/items?searchTerm={searchTerm}&recursive=true&includeItemTypes=Series,Movie");
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{InstanceUrl}/items?searchTerm={searchTerm}&recursive=true&includeItemTypes=Series,Movie");
|
||||
|
||||
var response = await MakeRequest(request);
|
||||
var response = await MakeRequest(request);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ItemResponse>();
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ItemResponse>();
|
||||
|
||||
return apiResponse!.Items.ToArray();
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
throw new JellyfinApiClientException(e.Message);
|
||||
}
|
||||
return apiResponse!.Items.ToArray();
|
||||
}
|
||||
|
||||
public async Task Authenticate()
|
||||
public async Task<ServerInfo> GetServerInfo()
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, $"{InstanceUrl}/Users/AuthenticateByName");
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{InstanceUrl}/System/Info");
|
||||
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("MediaBrowser", GetAuthHeader());
|
||||
var response = await MakeRequest(request);
|
||||
|
||||
var body = new
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ServerInfo>();
|
||||
|
||||
if (ID == string.Empty)
|
||||
{
|
||||
Username = _username,
|
||||
Pw = _password
|
||||
};
|
||||
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
|
||||
|
||||
try
|
||||
{
|
||||
var response = await _client.SendAsync(request);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var authResponse = await response.Content.ReadFromJsonAsync<AuthResponse>();
|
||||
|
||||
_apiKey = authResponse!.AccessToken;
|
||||
ID = authResponse.ServerId;
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
//TODO: What to do on an exception
|
||||
throw new JellyfinApiClientException(e.Message);
|
||||
ID = apiResponse!.Id;
|
||||
}
|
||||
|
||||
return apiResponse!;
|
||||
}
|
||||
|
||||
public async Task<object> GetPublicServerInfo()
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{InstanceUrl}/System/Info/Public");
|
||||
|
||||
var response = await MakeRequest(request);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ServerInfo>();
|
||||
|
||||
if (ID == string.Empty)
|
||||
{
|
||||
ID = apiResponse!.Id;
|
||||
}
|
||||
|
||||
|
||||
return apiResponse!;
|
||||
}
|
||||
|
||||
// public async Task Authenticate()
|
||||
// {
|
||||
// var request = new HttpRequestMessage(HttpMethod.Post, $"{InstanceUrl}/Users/AuthenticateByName");
|
||||
|
||||
// request.Headers.Authorization = new AuthenticationHeaderValue("MediaBrowser", GetAuthHeader());
|
||||
|
||||
// var body = new
|
||||
// {
|
||||
// Username = _username,
|
||||
// Pw = _password
|
||||
// };
|
||||
|
||||
// request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
|
||||
|
||||
// try
|
||||
// {
|
||||
// var response = await _client.SendAsync(request);
|
||||
|
||||
// response.EnsureSuccessStatusCode();
|
||||
|
||||
// var authResponse = await response.Content.ReadFromJsonAsync<AuthResponse>();
|
||||
|
||||
// _apiKey = authResponse!.AccessToken;
|
||||
// ID = authResponse.ServerId;
|
||||
// }
|
||||
// catch (HttpRequestException e)
|
||||
// {
|
||||
// throw new JellyfinApiClientException(e.Message);
|
||||
// }
|
||||
// }
|
||||
|
||||
private async Task<HttpResponseMessage> MakeRequest(HttpRequestMessage request)
|
||||
{
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("MediaBrowser", GetAuthHeader());
|
||||
|
|
@ -129,18 +150,19 @@ public class JellyfinApiClient
|
|||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
if (e.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
await Authenticate();
|
||||
// if (e.StatusCode == HttpStatusCode.Unauthorized)
|
||||
// {
|
||||
// await Authenticate();
|
||||
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("MediaBrowser", GetAuthHeader());
|
||||
// request.Headers.Authorization = new AuthenticationHeaderValue("MediaBrowser", GetAuthHeader());
|
||||
|
||||
response = await _client.SendAsync(request);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JellyfinApiClientException(e.Message);
|
||||
}
|
||||
// response = await _client.SendAsync(request);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// throw new JellyfinApiClientException(e.Message);
|
||||
// }
|
||||
throw new JellyfinApiClientException(e.Message);
|
||||
}
|
||||
|
||||
return response;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue