// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. package sdk import ( "bytes" "context" "fmt" "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/models/operations" "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/models/sdkerrors" "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/utils" "io" "net/http" "strings" ) // Butler is the task manager of the Plex Media Server Ecosystem. type Butler struct { sdkConfiguration sdkConfiguration } func newButler(sdkConfig sdkConfiguration) *Butler { return &Butler{ sdkConfiguration: sdkConfig, } } // GetButlerTasks - Get Butler tasks // Returns a list of butler tasks func (s *Butler) GetButlerTasks(ctx context.Context) (*operations.GetButlerTasksResponse, error) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url := strings.TrimSuffix(baseURL, "/") + "/butler" req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("user-agent", s.sdkConfiguration.UserAgent) client := s.sdkConfiguration.SecurityClient httpRes, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error sending request: %w", err) } if httpRes == nil { return nil, fmt.Errorf("error sending request: no response") } rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) contentType := httpRes.Header.Get("Content-Type") res := &operations.GetButlerTasksResponse{ StatusCode: httpRes.StatusCode, ContentType: contentType, RawResponse: httpRes, } switch { case httpRes.StatusCode == 200: switch { case utils.MatchContentType(contentType, `application/json`): var out operations.GetButlerTasksResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.TwoHundredApplicationJSONObject = &out default: return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode == 400: case httpRes.StatusCode == 401: switch { case utils.MatchContentType(contentType, `application/json`): var out operations.GetButlerTasksButlerResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.FourHundredAndOneApplicationJSONObject = &out default: return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } } return res, nil } // StartAllTasks - 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. func (s *Butler) StartAllTasks(ctx context.Context) (*operations.StartAllTasksResponse, error) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url := strings.TrimSuffix(baseURL, "/") + "/butler" req, err := http.NewRequestWithContext(ctx, "POST", url, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("user-agent", s.sdkConfiguration.UserAgent) client := s.sdkConfiguration.SecurityClient httpRes, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error sending request: %w", err) } if httpRes == nil { return nil, fmt.Errorf("error sending request: no response") } rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) contentType := httpRes.Header.Get("Content-Type") res := &operations.StartAllTasksResponse{ StatusCode: httpRes.StatusCode, ContentType: contentType, RawResponse: httpRes, } switch { case httpRes.StatusCode == 200: fallthrough case httpRes.StatusCode == 400: case httpRes.StatusCode == 401: switch { case utils.MatchContentType(contentType, `application/json`): var out operations.StartAllTasksResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.Object = &out default: return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } } return res, nil } // StopAllTasks - Stop all Butler tasks // This endpoint will stop all currently running tasks and remove any scheduled tasks from the queue. func (s *Butler) StopAllTasks(ctx context.Context) (*operations.StopAllTasksResponse, error) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url := strings.TrimSuffix(baseURL, "/") + "/butler" req, err := http.NewRequestWithContext(ctx, "DELETE", url, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("user-agent", s.sdkConfiguration.UserAgent) client := s.sdkConfiguration.SecurityClient httpRes, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error sending request: %w", err) } if httpRes == nil { return nil, fmt.Errorf("error sending request: no response") } rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) contentType := httpRes.Header.Get("Content-Type") res := &operations.StopAllTasksResponse{ StatusCode: httpRes.StatusCode, ContentType: contentType, RawResponse: httpRes, } switch { case httpRes.StatusCode == 200: fallthrough case httpRes.StatusCode == 400: case httpRes.StatusCode == 401: switch { case utils.MatchContentType(contentType, `application/json`): var out operations.StopAllTasksResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.Object = &out default: return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } } return res, nil } // StartTask - 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. func (s *Butler) StartTask(ctx context.Context, request operations.StartTaskRequest) (*operations.StartTaskResponse, error) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url, err := utils.GenerateURL(ctx, baseURL, "/butler/{taskName}", request, nil) if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } req, err := http.NewRequestWithContext(ctx, "POST", url, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("user-agent", s.sdkConfiguration.UserAgent) client := s.sdkConfiguration.SecurityClient httpRes, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error sending request: %w", err) } if httpRes == nil { return nil, fmt.Errorf("error sending request: no response") } rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) contentType := httpRes.Header.Get("Content-Type") res := &operations.StartTaskResponse{ StatusCode: httpRes.StatusCode, ContentType: contentType, RawResponse: httpRes, } switch { case httpRes.StatusCode == 200: fallthrough case httpRes.StatusCode == 202: fallthrough case httpRes.StatusCode == 400: case httpRes.StatusCode == 401: switch { case utils.MatchContentType(contentType, `application/json`): var out operations.StartTaskResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.Object = &out default: return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } } return res, nil } // StopTask - 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. func (s *Butler) StopTask(ctx context.Context, request operations.StopTaskRequest) (*operations.StopTaskResponse, error) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url, err := utils.GenerateURL(ctx, baseURL, "/butler/{taskName}", request, nil) if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } req, err := http.NewRequestWithContext(ctx, "DELETE", url, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("user-agent", s.sdkConfiguration.UserAgent) client := s.sdkConfiguration.SecurityClient httpRes, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error sending request: %w", err) } if httpRes == nil { return nil, fmt.Errorf("error sending request: no response") } rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) contentType := httpRes.Header.Get("Content-Type") res := &operations.StopTaskResponse{ StatusCode: httpRes.StatusCode, ContentType: contentType, RawResponse: httpRes, } switch { case httpRes.StatusCode == 200: fallthrough case httpRes.StatusCode == 400: fallthrough case httpRes.StatusCode == 404: case httpRes.StatusCode == 401: switch { case utils.MatchContentType(contentType, `application/json`): var out operations.StopTaskResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.Object = &out default: return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } } return res, nil }