mirror of
https://github.com/LukeHagar/plexcsharp.git
synced 2025-12-06 12:37:46 +00:00
393 lines
17 KiB
C#
393 lines
17 KiB
C#
//------------------------------------------------------------------------------
|
|
// <auto-generated>
|
|
// This code was generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
|
//
|
|
// Changes to this file may cause incorrect behavior and will be lost when
|
|
// the code is regenerated.
|
|
// </auto-generated>
|
|
//------------------------------------------------------------------------------
|
|
#nullable enable
|
|
namespace PlexAPI
|
|
{
|
|
using Newtonsoft.Json;
|
|
using PlexAPI.Hooks;
|
|
using PlexAPI.Models.Components;
|
|
using PlexAPI.Models.Errors;
|
|
using PlexAPI.Models.Requests;
|
|
using PlexAPI.Utils.Retries;
|
|
using PlexAPI.Utils;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
|
|
/// <summary>
|
|
/// API Calls that perform operations directly against https://Plex.tv<br/>
|
|
///
|
|
/// <remarks>
|
|
///
|
|
/// </remarks>
|
|
/// </summary>
|
|
public interface IPlex
|
|
{
|
|
|
|
/// <summary>
|
|
/// Get Plex Home Data
|
|
///
|
|
/// <remarks>
|
|
/// Retrieves the home data for the authenticated user, including details like home ID, name, guest access information, and subscription status.
|
|
/// </remarks>
|
|
/// </summary>
|
|
Task<GetHomeDataResponse> GetHomeDataAsync();
|
|
|
|
/// <summary>
|
|
/// Get a Pin
|
|
///
|
|
/// <remarks>
|
|
/// Retrieve a Pin from Plex.tv for authentication flows
|
|
/// </remarks>
|
|
/// </summary>
|
|
Task<GetPinResponse> GetPinAsync(string xPlexProduct, bool? strong = null, string? xPlexClientIdentifier = null, string? serverUrl = null);
|
|
|
|
/// <summary>
|
|
/// Get Access Token
|
|
///
|
|
/// <remarks>
|
|
/// Retrieve an Access Token from Plex.tv after the Pin has already been authenticated
|
|
/// </remarks>
|
|
/// </summary>
|
|
Task<GetTokenResponse> GetTokenAsync(string pinID, string? xPlexClientIdentifier = null, string? serverUrl = null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// API Calls that perform operations directly against https://Plex.tv<br/>
|
|
///
|
|
/// <remarks>
|
|
///
|
|
/// </remarks>
|
|
/// </summary>
|
|
public class Plex: IPlex
|
|
{
|
|
/// <summary>
|
|
/// List of server URLs available for the getPin operation.
|
|
/// </summary>
|
|
public static readonly string[] GetPinServerList = {
|
|
"https://plex.tv/api/v2",
|
|
};
|
|
/// <summary>
|
|
/// List of server URLs available for the getToken operation.
|
|
/// </summary>
|
|
public static readonly string[] GetTokenServerList = {
|
|
"https://plex.tv/api/v2",
|
|
};
|
|
public SDKConfig SDKConfiguration { get; private set; }
|
|
private const string _language = "csharp";
|
|
private const string _sdkVersion = "0.3.0";
|
|
private const string _sdkGenVersion = "2.354.2";
|
|
private const string _openapiDocVersion = "0.0.3";
|
|
private const string _userAgent = "speakeasy-sdk/csharp 0.3.0 2.354.2 0.0.3 PlexAPI";
|
|
private string _serverUrl = "";
|
|
private ISpeakeasyHttpClient _client;
|
|
private Func<Security>? _securitySource;
|
|
|
|
public Plex(ISpeakeasyHttpClient client, Func<Security>? securitySource, string serverUrl, SDKConfig config)
|
|
{
|
|
_client = client;
|
|
_securitySource = securitySource;
|
|
_serverUrl = serverUrl;
|
|
SDKConfiguration = config;
|
|
}
|
|
|
|
public async Task<GetHomeDataResponse> GetHomeDataAsync()
|
|
{
|
|
string baseUrl = this.SDKConfiguration.GetTemplatedServerUrl();
|
|
|
|
var urlString = baseUrl + "/home";
|
|
|
|
var httpRequest = new HttpRequestMessage(HttpMethod.Get, urlString);
|
|
httpRequest.Headers.Add("user-agent", _userAgent);
|
|
|
|
if (_securitySource != null)
|
|
{
|
|
httpRequest = new SecurityMetadata(_securitySource).Apply(httpRequest);
|
|
}
|
|
|
|
var hookCtx = new HookContext("getHomeData", null, _securitySource);
|
|
|
|
httpRequest = await this.SDKConfiguration.Hooks.BeforeRequestAsync(new BeforeRequestContext(hookCtx), httpRequest);
|
|
|
|
HttpResponseMessage httpResponse;
|
|
try
|
|
{
|
|
httpResponse = await _client.SendAsync(httpRequest);
|
|
int _statusCode = (int)httpResponse.StatusCode;
|
|
|
|
if (_statusCode == 400 || _statusCode == 401 || _statusCode >= 400 && _statusCode < 500 || _statusCode >= 500 && _statusCode < 600)
|
|
{
|
|
var _httpResponse = await this.SDKConfiguration.Hooks.AfterErrorAsync(new AfterErrorContext(hookCtx), httpResponse, null);
|
|
if (_httpResponse != null)
|
|
{
|
|
httpResponse = _httpResponse;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
var _httpResponse = await this.SDKConfiguration.Hooks.AfterErrorAsync(new AfterErrorContext(hookCtx), null, error);
|
|
if (_httpResponse != null)
|
|
{
|
|
httpResponse = _httpResponse;
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
httpResponse = await this.SDKConfiguration.Hooks.AfterSuccessAsync(new AfterSuccessContext(hookCtx), httpResponse);
|
|
|
|
var contentType = httpResponse.Content.Headers.ContentType?.MediaType;
|
|
int responseStatusCode = (int)httpResponse.StatusCode;
|
|
if(responseStatusCode == 200)
|
|
{
|
|
if(Utilities.IsContentTypeMatch("application/json", contentType))
|
|
{
|
|
var obj = ResponseBodyDeserializer.Deserialize<Models.Requests.GetHomeDataResponseBody>(await httpResponse.Content.ReadAsStringAsync(), NullValueHandling.Ignore);
|
|
var response = new GetHomeDataResponse()
|
|
{
|
|
StatusCode = responseStatusCode,
|
|
ContentType = contentType,
|
|
RawResponse = httpResponse
|
|
};
|
|
response.Object = obj;
|
|
return response;
|
|
}
|
|
else
|
|
{
|
|
throw new SDKException("Unknown content type received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
}
|
|
else if(responseStatusCode == 400 || responseStatusCode >= 400 && responseStatusCode < 500 || responseStatusCode >= 500 && responseStatusCode < 600)
|
|
{
|
|
throw new SDKException("API error occurred", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
else if(responseStatusCode == 401)
|
|
{
|
|
if(Utilities.IsContentTypeMatch("application/json", contentType))
|
|
{
|
|
var obj = ResponseBodyDeserializer.Deserialize<Models.Errors.GetHomeDataResponseBody>(await httpResponse.Content.ReadAsStringAsync(), NullValueHandling.Ignore);
|
|
obj!.RawResponse = httpResponse;
|
|
throw obj!;
|
|
}
|
|
else
|
|
{
|
|
throw new SDKException("Unknown content type received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new SDKException("Unknown status code received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
}
|
|
|
|
public async Task<GetPinResponse> GetPinAsync(string xPlexProduct, bool? strong = null, string? xPlexClientIdentifier = null, string? serverUrl = null)
|
|
{
|
|
var request = new GetPinRequest()
|
|
{
|
|
XPlexProduct = xPlexProduct,
|
|
Strong = strong,
|
|
XPlexClientIdentifier = xPlexClientIdentifier,
|
|
};
|
|
request.XPlexClientIdentifier ??= SDKConfiguration.XPlexClientIdentifier;
|
|
|
|
string baseUrl = Utilities.TemplateUrl(GetPinServerList[0], new Dictionary<string, string>(){
|
|
});
|
|
if (serverUrl != null)
|
|
{
|
|
baseUrl = serverUrl;
|
|
}
|
|
var urlString = URLBuilder.Build(baseUrl, "/pins", request);
|
|
|
|
var httpRequest = new HttpRequestMessage(HttpMethod.Post, urlString);
|
|
httpRequest.Headers.Add("user-agent", _userAgent);
|
|
HeaderSerializer.PopulateHeaders(ref httpRequest, request);
|
|
|
|
var hookCtx = new HookContext("getPin", null, null);
|
|
|
|
httpRequest = await this.SDKConfiguration.Hooks.BeforeRequestAsync(new BeforeRequestContext(hookCtx), httpRequest);
|
|
|
|
HttpResponseMessage httpResponse;
|
|
try
|
|
{
|
|
httpResponse = await _client.SendAsync(httpRequest);
|
|
int _statusCode = (int)httpResponse.StatusCode;
|
|
|
|
if (_statusCode == 400 || _statusCode >= 400 && _statusCode < 500 || _statusCode >= 500 && _statusCode < 600)
|
|
{
|
|
var _httpResponse = await this.SDKConfiguration.Hooks.AfterErrorAsync(new AfterErrorContext(hookCtx), httpResponse, null);
|
|
if (_httpResponse != null)
|
|
{
|
|
httpResponse = _httpResponse;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
var _httpResponse = await this.SDKConfiguration.Hooks.AfterErrorAsync(new AfterErrorContext(hookCtx), null, error);
|
|
if (_httpResponse != null)
|
|
{
|
|
httpResponse = _httpResponse;
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
httpResponse = await this.SDKConfiguration.Hooks.AfterSuccessAsync(new AfterSuccessContext(hookCtx), httpResponse);
|
|
|
|
var contentType = httpResponse.Content.Headers.ContentType?.MediaType;
|
|
int responseStatusCode = (int)httpResponse.StatusCode;
|
|
if(responseStatusCode == 201)
|
|
{
|
|
if(Utilities.IsContentTypeMatch("application/json", contentType))
|
|
{
|
|
var obj = ResponseBodyDeserializer.Deserialize<Models.Requests.GetPinResponseBody>(await httpResponse.Content.ReadAsStringAsync(), NullValueHandling.Ignore);
|
|
var response = new GetPinResponse()
|
|
{
|
|
StatusCode = responseStatusCode,
|
|
ContentType = contentType,
|
|
RawResponse = httpResponse
|
|
};
|
|
response.Object = obj;
|
|
return response;
|
|
}
|
|
else
|
|
{
|
|
throw new SDKException("Unknown content type received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
}
|
|
else if(responseStatusCode == 400)
|
|
{
|
|
if(Utilities.IsContentTypeMatch("application/json", contentType))
|
|
{
|
|
var obj = ResponseBodyDeserializer.Deserialize<Models.Errors.GetPinResponseBody>(await httpResponse.Content.ReadAsStringAsync(), NullValueHandling.Ignore);
|
|
obj!.RawResponse = httpResponse;
|
|
throw obj!;
|
|
}
|
|
else
|
|
{
|
|
throw new SDKException("Unknown content type received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
}
|
|
else if(responseStatusCode >= 400 && responseStatusCode < 500 || responseStatusCode >= 500 && responseStatusCode < 600)
|
|
{
|
|
throw new SDKException("API error occurred", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
else
|
|
{
|
|
throw new SDKException("Unknown status code received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
}
|
|
|
|
public async Task<GetTokenResponse> GetTokenAsync(string pinID, string? xPlexClientIdentifier = null, string? serverUrl = null)
|
|
{
|
|
var request = new GetTokenRequest()
|
|
{
|
|
PinID = pinID,
|
|
XPlexClientIdentifier = xPlexClientIdentifier,
|
|
};
|
|
request.XPlexClientIdentifier ??= SDKConfiguration.XPlexClientIdentifier;
|
|
|
|
string baseUrl = Utilities.TemplateUrl(GetTokenServerList[0], new Dictionary<string, string>(){
|
|
});
|
|
if (serverUrl != null)
|
|
{
|
|
baseUrl = serverUrl;
|
|
}
|
|
var urlString = URLBuilder.Build(baseUrl, "/pins/{pinID}", request);
|
|
|
|
var httpRequest = new HttpRequestMessage(HttpMethod.Get, urlString);
|
|
httpRequest.Headers.Add("user-agent", _userAgent);
|
|
HeaderSerializer.PopulateHeaders(ref httpRequest, request);
|
|
|
|
var hookCtx = new HookContext("getToken", null, null);
|
|
|
|
httpRequest = await this.SDKConfiguration.Hooks.BeforeRequestAsync(new BeforeRequestContext(hookCtx), httpRequest);
|
|
|
|
HttpResponseMessage httpResponse;
|
|
try
|
|
{
|
|
httpResponse = await _client.SendAsync(httpRequest);
|
|
int _statusCode = (int)httpResponse.StatusCode;
|
|
|
|
if (_statusCode == 400 || _statusCode >= 400 && _statusCode < 500 || _statusCode >= 500 && _statusCode < 600)
|
|
{
|
|
var _httpResponse = await this.SDKConfiguration.Hooks.AfterErrorAsync(new AfterErrorContext(hookCtx), httpResponse, null);
|
|
if (_httpResponse != null)
|
|
{
|
|
httpResponse = _httpResponse;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
var _httpResponse = await this.SDKConfiguration.Hooks.AfterErrorAsync(new AfterErrorContext(hookCtx), null, error);
|
|
if (_httpResponse != null)
|
|
{
|
|
httpResponse = _httpResponse;
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
httpResponse = await this.SDKConfiguration.Hooks.AfterSuccessAsync(new AfterSuccessContext(hookCtx), httpResponse);
|
|
|
|
var contentType = httpResponse.Content.Headers.ContentType?.MediaType;
|
|
int responseStatusCode = (int)httpResponse.StatusCode;
|
|
if(responseStatusCode == 200)
|
|
{
|
|
if(Utilities.IsContentTypeMatch("application/json", contentType))
|
|
{
|
|
var obj = ResponseBodyDeserializer.Deserialize<Models.Requests.GetTokenResponseBody>(await httpResponse.Content.ReadAsStringAsync(), NullValueHandling.Ignore);
|
|
var response = new GetTokenResponse()
|
|
{
|
|
StatusCode = responseStatusCode,
|
|
ContentType = contentType,
|
|
RawResponse = httpResponse
|
|
};
|
|
response.Object = obj;
|
|
return response;
|
|
}
|
|
else
|
|
{
|
|
throw new SDKException("Unknown content type received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
}
|
|
else if(responseStatusCode == 400)
|
|
{
|
|
if(Utilities.IsContentTypeMatch("application/json", contentType))
|
|
{
|
|
var obj = ResponseBodyDeserializer.Deserialize<Models.Errors.GetTokenResponseBody>(await httpResponse.Content.ReadAsStringAsync(), NullValueHandling.Ignore);
|
|
obj!.RawResponse = httpResponse;
|
|
throw obj!;
|
|
}
|
|
else
|
|
{
|
|
throw new SDKException("Unknown content type received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
}
|
|
else if(responseStatusCode >= 400 && responseStatusCode < 500 || responseStatusCode >= 500 && responseStatusCode < 600)
|
|
{
|
|
throw new SDKException("API error occurred", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
else
|
|
{
|
|
throw new SDKException("Unknown status code received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse);
|
|
}
|
|
}
|
|
}
|
|
} |