Files
plexcsharp/LukeHagar/PlexAPI/SDK/Log.cs

458 lines
21 KiB
C#

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by Speakeasy (https://speakeasy.com). 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 LukeHagar.PlexAPI.SDK
{
using LukeHagar.PlexAPI.SDK.Hooks;
using LukeHagar.PlexAPI.SDK.Models.Components;
using LukeHagar.PlexAPI.SDK.Models.Errors;
using LukeHagar.PlexAPI.SDK.Models.Requests;
using LukeHagar.PlexAPI.SDK.Utils;
using LukeHagar.PlexAPI.SDK.Utils.Retries;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
/// <summary>
/// Submit logs to the Log Handler for Plex Media Server<br/>
///
/// <remarks>
///
/// </remarks>
/// </summary>
public interface ILog
{
/// <summary>
/// Logging a single line message.
///
/// <remarks>
/// This endpoint will write a single-line log message, including a level and source to the main Plex Media Server log.<br/>
///
/// </remarks>
/// </summary>
Task<LogLineResponse> LogLineAsync(Level level, string message, string source);
/// <summary>
/// Logging a multi-line message
///
/// <remarks>
/// This endpoint allows for the batch addition of log entries to the main Plex Media Server log.<br/>
/// It accepts a text/plain request body, where each line represents a distinct log entry.<br/>
/// Each log entry consists of URL-encoded key-value pairs, specifying log attributes such as &apos;level&apos;, &apos;message&apos;, and &apos;source&apos;.<br/>
/// <br/>
/// Log entries are separated by a newline character (`\n`).<br/>
/// Each entry&apos;s parameters should be URL-encoded to ensure accurate parsing and handling of special characters.<br/>
/// This method is efficient for logging multiple entries in a single API call, reducing the overhead of multiple individual requests.<br/>
/// <br/>
/// The &apos;level&apos; parameter specifies the log entry&apos;s severity or importance, with the following integer values:<br/>
/// - `0`: Error - Critical issues that require immediate attention.<br/>
/// - `1`: Warning - Important events that are not critical but may indicate potential issues.<br/>
/// - `2`: Info - General informational messages about system operation.<br/>
/// - `3`: Debug - Detailed information useful for debugging purposes.<br/>
/// - `4`: Verbose - Highly detailed diagnostic information for in-depth analysis.<br/>
/// <br/>
/// The &apos;message&apos; parameter contains the log text, and &apos;source&apos; identifies the log message&apos;s origin (e.g., an application name or module).<br/>
/// <br/>
/// Example of a single log entry format:<br/>
/// `level=4&amp;message=Sample%20log%20entry&amp;source=applicationName`<br/>
/// <br/>
/// Ensure each parameter is properly URL-encoded to avoid interpretation issues.<br/>
///
/// </remarks>
/// </summary>
Task<LogMultiLineResponse> LogMultiLineAsync(string request);
/// <summary>
/// Enabling Papertrail
///
/// <remarks>
/// This endpoint will enable all Plex Media Serverlogs to be sent to the Papertrail networked logging site for a period of time.<br/>
///
/// </remarks>
/// </summary>
Task<EnablePaperTrailResponse> EnablePaperTrailAsync();
}
/// <summary>
/// Submit logs to the Log Handler for Plex Media Server<br/>
///
/// <remarks>
///
/// </remarks>
/// </summary>
public class Log: ILog
{
public SDKConfig SDKConfiguration { get; private set; }
private const string _language = "csharp";
private const string _sdkVersion = "0.17.0";
private const string _sdkGenVersion = "2.698.4";
private const string _openapiDocVersion = "0.0.3";
public Log(SDKConfig config)
{
SDKConfiguration = config;
}
public async Task<LogLineResponse> LogLineAsync(Level level, string message, string source)
{
var request = new LogLineRequest()
{
Level = level,
Message = message,
Source = source,
};
string baseUrl = this.SDKConfiguration.GetTemplatedServerUrl();
var urlString = URLBuilder.Build(baseUrl, "/log", request);
var httpRequest = new HttpRequestMessage(HttpMethod.Get, urlString);
httpRequest.Headers.Add("user-agent", SDKConfiguration.UserAgent);
if (SDKConfiguration.SecuritySource != null)
{
httpRequest = new SecurityMetadata(SDKConfiguration.SecuritySource).Apply(httpRequest);
}
var hookCtx = new HookContext(SDKConfiguration, baseUrl, "logLine", new List<string> { }, SDKConfiguration.SecuritySource);
httpRequest = await this.SDKConfiguration.Hooks.BeforeRequestAsync(new BeforeRequestContext(hookCtx), httpRequest);
HttpResponseMessage httpResponse;
try
{
httpResponse = await SDKConfiguration.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)
{
return new LogLineResponse()
{
StatusCode = responseStatusCode,
ContentType = contentType,
RawResponse = httpResponse
};
}
else if(responseStatusCode == 400)
{
if(Utilities.IsContentTypeMatch("application/json", contentType))
{
var httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
LogLineBadRequestPayload payload;
try
{
payload = ResponseBodyDeserializer.DeserializeNotNull<LogLineBadRequestPayload>(httpResponseBody, NullValueHandling.Ignore);
}
catch (Exception ex)
{
throw new ResponseValidationException("Failed to deserialize response body into LogLineBadRequestPayload.", httpResponse, httpResponseBody, ex);
}
payload.RawResponse = httpResponse;
throw new LogLineBadRequest(payload, httpResponse, httpResponseBody);
}
throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
else if(responseStatusCode == 401)
{
if(Utilities.IsContentTypeMatch("application/json", contentType))
{
var httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
LogLineUnauthorizedPayload payload;
try
{
payload = ResponseBodyDeserializer.DeserializeNotNull<LogLineUnauthorizedPayload>(httpResponseBody, NullValueHandling.Ignore);
}
catch (Exception ex)
{
throw new ResponseValidationException("Failed to deserialize response body into LogLineUnauthorizedPayload.", httpResponse, httpResponseBody, ex);
}
payload.RawResponse = httpResponse;
throw new LogLineUnauthorized(payload, httpResponse, httpResponseBody);
}
throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
else if(responseStatusCode >= 400 && responseStatusCode < 500)
{
throw new Models.Errors.SDKException("API error occurred", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
else if(responseStatusCode >= 500 && responseStatusCode < 600)
{
throw new Models.Errors.SDKException("API error occurred", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
throw new Models.Errors.SDKException("Unknown status code received", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
public async Task<LogMultiLineResponse> LogMultiLineAsync(string request)
{
string baseUrl = this.SDKConfiguration.GetTemplatedServerUrl();
var urlString = baseUrl + "/log";
var httpRequest = new HttpRequestMessage(HttpMethod.Post, urlString);
httpRequest.Headers.Add("user-agent", SDKConfiguration.UserAgent);
var serializedBody = RequestBodySerializer.Serialize(request, "Request", "string", false, false);
if (serializedBody != null)
{
httpRequest.Content = serializedBody;
}
if (SDKConfiguration.SecuritySource != null)
{
httpRequest = new SecurityMetadata(SDKConfiguration.SecuritySource).Apply(httpRequest);
}
var hookCtx = new HookContext(SDKConfiguration, baseUrl, "logMultiLine", new List<string> { }, SDKConfiguration.SecuritySource);
httpRequest = await this.SDKConfiguration.Hooks.BeforeRequestAsync(new BeforeRequestContext(hookCtx), httpRequest);
HttpResponseMessage httpResponse;
try
{
httpResponse = await SDKConfiguration.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)
{
return new LogMultiLineResponse()
{
StatusCode = responseStatusCode,
ContentType = contentType,
RawResponse = httpResponse
};
}
else if(responseStatusCode == 400)
{
if(Utilities.IsContentTypeMatch("application/json", contentType))
{
var httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
LogMultiLineBadRequestPayload payload;
try
{
payload = ResponseBodyDeserializer.DeserializeNotNull<LogMultiLineBadRequestPayload>(httpResponseBody, NullValueHandling.Ignore);
}
catch (Exception ex)
{
throw new ResponseValidationException("Failed to deserialize response body into LogMultiLineBadRequestPayload.", httpResponse, httpResponseBody, ex);
}
payload.RawResponse = httpResponse;
throw new LogMultiLineBadRequest(payload, httpResponse, httpResponseBody);
}
throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
else if(responseStatusCode == 401)
{
if(Utilities.IsContentTypeMatch("application/json", contentType))
{
var httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
LogMultiLineUnauthorizedPayload payload;
try
{
payload = ResponseBodyDeserializer.DeserializeNotNull<LogMultiLineUnauthorizedPayload>(httpResponseBody, NullValueHandling.Ignore);
}
catch (Exception ex)
{
throw new ResponseValidationException("Failed to deserialize response body into LogMultiLineUnauthorizedPayload.", httpResponse, httpResponseBody, ex);
}
payload.RawResponse = httpResponse;
throw new LogMultiLineUnauthorized(payload, httpResponse, httpResponseBody);
}
throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
else if(responseStatusCode >= 400 && responseStatusCode < 500)
{
throw new Models.Errors.SDKException("API error occurred", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
else if(responseStatusCode >= 500 && responseStatusCode < 600)
{
throw new Models.Errors.SDKException("API error occurred", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
throw new Models.Errors.SDKException("Unknown status code received", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
public async Task<EnablePaperTrailResponse> EnablePaperTrailAsync()
{
string baseUrl = this.SDKConfiguration.GetTemplatedServerUrl();
var urlString = baseUrl + "/log/networked";
var httpRequest = new HttpRequestMessage(HttpMethod.Get, urlString);
httpRequest.Headers.Add("user-agent", SDKConfiguration.UserAgent);
if (SDKConfiguration.SecuritySource != null)
{
httpRequest = new SecurityMetadata(SDKConfiguration.SecuritySource).Apply(httpRequest);
}
var hookCtx = new HookContext(SDKConfiguration, baseUrl, "enablePaperTrail", new List<string> { }, SDKConfiguration.SecuritySource);
httpRequest = await this.SDKConfiguration.Hooks.BeforeRequestAsync(new BeforeRequestContext(hookCtx), httpRequest);
HttpResponseMessage httpResponse;
try
{
httpResponse = await SDKConfiguration.Client.SendAsync(httpRequest);
int _statusCode = (int)httpResponse.StatusCode;
if (_statusCode == 400 || _statusCode == 401 || _statusCode == 403 || _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)
{
return new EnablePaperTrailResponse()
{
StatusCode = responseStatusCode,
ContentType = contentType,
RawResponse = httpResponse
};
}
else if(responseStatusCode == 400)
{
if(Utilities.IsContentTypeMatch("application/json", contentType))
{
var httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
EnablePaperTrailBadRequestPayload payload;
try
{
payload = ResponseBodyDeserializer.DeserializeNotNull<EnablePaperTrailBadRequestPayload>(httpResponseBody, NullValueHandling.Ignore);
}
catch (Exception ex)
{
throw new ResponseValidationException("Failed to deserialize response body into EnablePaperTrailBadRequestPayload.", httpResponse, httpResponseBody, ex);
}
payload.RawResponse = httpResponse;
throw new EnablePaperTrailBadRequest(payload, httpResponse, httpResponseBody);
}
throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
else if(responseStatusCode == 401)
{
if(Utilities.IsContentTypeMatch("application/json", contentType))
{
var httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
EnablePaperTrailUnauthorizedPayload payload;
try
{
payload = ResponseBodyDeserializer.DeserializeNotNull<EnablePaperTrailUnauthorizedPayload>(httpResponseBody, NullValueHandling.Ignore);
}
catch (Exception ex)
{
throw new ResponseValidationException("Failed to deserialize response body into EnablePaperTrailUnauthorizedPayload.", httpResponse, httpResponseBody, ex);
}
payload.RawResponse = httpResponse;
throw new EnablePaperTrailUnauthorized(payload, httpResponse, httpResponseBody);
}
throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
else if(responseStatusCode == 403 || responseStatusCode >= 400 && responseStatusCode < 500)
{
throw new Models.Errors.SDKException("API error occurred", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
else if(responseStatusCode >= 500 && responseStatusCode < 600)
{
throw new Models.Errors.SDKException("API error occurred", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
throw new Models.Errors.SDKException("Unknown status code received", httpResponse, await httpResponse.Content.ReadAsStringAsync());
}
}
}