//------------------------------------------------------------------------------ // // 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. // //------------------------------------------------------------------------------ #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; /// /// Butler is the task manager of the Plex Media Server Ecosystem.
/// /// /// /// ///
public interface IButler { /// /// Get Butler tasks /// /// /// Returns a list of butler tasks /// /// Task GetButlerTasksAsync(); /// /// Start all Butler tasks /// /// /// This endpoint will attempt to start all Butler tasks that are enabled in the settings. Butler tasks normally run automatically during a time window configured on the server's Settings page but can be manually started using this endpoint. Tasks will run with the following criteria:
/// 1. Any tasks not scheduled to run on the current day will be skipped.
/// 2. If a task is configured to run at a random time during the configured window and we are outside that window, the task will start immediately.
/// 3. If a task is configured to run at a random time during the configured window and we are within that window, the task will be scheduled at a random time within the window.
/// 4. If we are outside the configured window, the task will start immediately.
/// ///
///
Task StartAllTasksAsync(); /// /// Stop all Butler tasks /// /// /// This endpoint will stop all currently running tasks and remove any scheduled tasks from the queue.
/// ///
///
Task StopAllTasksAsync(); /// /// Start a single Butler task /// /// /// This endpoint will attempt to start a single Butler task that is enabled in the settings. Butler tasks normally run automatically during a time window configured on the server's Settings page but can be manually started using this endpoint. Tasks will run with the following criteria:
/// 1. Any tasks not scheduled to run on the current day will be skipped.
/// 2. If a task is configured to run at a random time during the configured window and we are outside that window, the task will start immediately.
/// 3. If a task is configured to run at a random time during the configured window and we are within that window, the task will be scheduled at a random time within the window.
/// 4. If we are outside the configured window, the task will start immediately.
/// ///
///
Task StartTaskAsync(TaskName taskName); /// /// Stop a single Butler task /// /// /// This endpoint will stop a currently running task by name, or remove it from the list of scheduled tasks if it exists. See the section above for a list of task names for this endpoint.
/// ///
///
Task StopTaskAsync(PathParamTaskName taskName); } /// /// Butler is the task manager of the Plex Media Server Ecosystem.
/// /// /// /// ///
public class Butler: IButler { 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 Butler(SDKConfig config) { SDKConfiguration = config; } public async Task GetButlerTasksAsync() { string baseUrl = this.SDKConfiguration.GetTemplatedServerUrl(); var urlString = baseUrl + "/butler"; 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, "getButlerTasks", new List { }, 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) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); GetButlerTasksResponseBody obj; try { obj = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into GetButlerTasksResponseBody.", httpResponse, httpResponseBody, ex); } var response = new GetButlerTasksResponse() { StatusCode = responseStatusCode, ContentType = contentType, RawResponse = httpResponse }; response.Object = obj; return response; } throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync()); } else if(responseStatusCode == 400) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); GetButlerTasksBadRequestPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into GetButlerTasksBadRequestPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new GetButlerTasksBadRequest(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(); GetButlerTasksUnauthorizedPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into GetButlerTasksUnauthorizedPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new GetButlerTasksUnauthorized(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 StartAllTasksAsync() { string baseUrl = this.SDKConfiguration.GetTemplatedServerUrl(); var urlString = baseUrl + "/butler"; var httpRequest = new HttpRequestMessage(HttpMethod.Post, 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, "startAllTasks", new List { }, 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 StartAllTasksResponse() { StatusCode = responseStatusCode, ContentType = contentType, RawResponse = httpResponse }; } else if(responseStatusCode == 400) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); StartAllTasksBadRequestPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into StartAllTasksBadRequestPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new StartAllTasksBadRequest(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(); StartAllTasksUnauthorizedPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into StartAllTasksUnauthorizedPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new StartAllTasksUnauthorized(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 StopAllTasksAsync() { string baseUrl = this.SDKConfiguration.GetTemplatedServerUrl(); var urlString = baseUrl + "/butler"; var httpRequest = new HttpRequestMessage(HttpMethod.Delete, 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, "stopAllTasks", new List { }, 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 StopAllTasksResponse() { StatusCode = responseStatusCode, ContentType = contentType, RawResponse = httpResponse }; } else if(responseStatusCode == 400) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); StopAllTasksBadRequestPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into StopAllTasksBadRequestPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new StopAllTasksBadRequest(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(); StopAllTasksUnauthorizedPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into StopAllTasksUnauthorizedPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new StopAllTasksUnauthorized(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 StartTaskAsync(TaskName taskName) { var request = new StartTaskRequest() { TaskName = taskName, }; string baseUrl = this.SDKConfiguration.GetTemplatedServerUrl(); var urlString = URLBuilder.Build(baseUrl, "/butler/{taskName}", request); var httpRequest = new HttpRequestMessage(HttpMethod.Post, 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, "startTask", new List { }, 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(new List{200, 202}.Contains(responseStatusCode)) { return new StartTaskResponse() { StatusCode = responseStatusCode, ContentType = contentType, RawResponse = httpResponse }; } else if(responseStatusCode == 400) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); StartTaskBadRequestPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into StartTaskBadRequestPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new StartTaskBadRequest(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(); StartTaskUnauthorizedPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into StartTaskUnauthorizedPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new StartTaskUnauthorized(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 StopTaskAsync(PathParamTaskName taskName) { var request = new StopTaskRequest() { TaskName = taskName, }; string baseUrl = this.SDKConfiguration.GetTemplatedServerUrl(); var urlString = URLBuilder.Build(baseUrl, "/butler/{taskName}", request); var httpRequest = new HttpRequestMessage(HttpMethod.Delete, 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, "stopTask", new List { }, 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 == 404 || _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 StopTaskResponse() { StatusCode = responseStatusCode, ContentType = contentType, RawResponse = httpResponse }; } else if(responseStatusCode == 400) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); StopTaskBadRequestPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into StopTaskBadRequestPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new StopTaskBadRequest(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(); StopTaskUnauthorizedPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Ignore); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into StopTaskUnauthorizedPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new StopTaskUnauthorized(payload, httpResponse, httpResponseBody); } throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync()); } else if(responseStatusCode == 404 || 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()); } } }