mirror of
https://github.com/LukeHagar/plexterraform.git
synced 2025-12-06 12:37:47 +00:00
330 lines
11 KiB
Go
330 lines
11 KiB
Go
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
|
|
|
package sdk
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/internal/hooks"
|
|
"github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/internal/utils"
|
|
"github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/models/errors"
|
|
"github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/models/operations"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// Log - Submit logs to the Log Handler for Plex Media Server
|
|
type Log struct {
|
|
sdkConfiguration sdkConfiguration
|
|
}
|
|
|
|
func newLog(sdkConfig sdkConfiguration) *Log {
|
|
return &Log{
|
|
sdkConfiguration: sdkConfig,
|
|
}
|
|
}
|
|
|
|
// LogLine - Logging a single line message.
|
|
// This endpoint will write a single-line log message, including a level and source to the main Plex Media Server log.
|
|
func (s *Log) LogLine(ctx context.Context, request operations.LogLineRequest) (*operations.LogLineResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "logLine",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/log")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", opURL, 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)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, nil); err != nil {
|
|
return nil, fmt.Errorf("error populating query params: %w", err)
|
|
}
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.LogLineResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
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))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
fallthrough
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.LogLineResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// LogMultiLine - Logging a multi-line message
|
|
// This endpoint allows for the batch addition of log entries to the main Plex Media Server log.
|
|
// It accepts a text/plain request body, where each line represents a distinct log entry.
|
|
// Each log entry consists of URL-encoded key-value pairs, specifying log attributes such as 'level', 'message', and 'source'.
|
|
//
|
|
// Log entries are separated by a newline character (`\n`).
|
|
// Each entry's parameters should be URL-encoded to ensure accurate parsing and handling of special characters.
|
|
// This method is efficient for logging multiple entries in a single API call, reducing the overhead of multiple individual requests.
|
|
//
|
|
// The 'level' parameter specifies the log entry's severity or importance, with the following integer values:
|
|
// - `0`: Error - Critical issues that require immediate attention.
|
|
// - `1`: Warning - Important events that are not critical but may indicate potential issues.
|
|
// - `2`: Info - General informational messages about system operation.
|
|
// - `3`: Debug - Detailed information useful for debugging purposes.
|
|
// - `4`: Verbose - Highly detailed diagnostic information for in-depth analysis.
|
|
//
|
|
// The 'message' parameter contains the log text, and 'source' identifies the log message's origin (e.g., an application name or module).
|
|
//
|
|
// Example of a single log entry format:
|
|
// `level=4&message=Sample%20log%20entry&source=applicationName`
|
|
//
|
|
// Ensure each parameter is properly URL-encoded to avoid interpretation issues.
|
|
func (s *Log) LogMultiLine(ctx context.Context, request string) (*operations.LogMultiLineResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "logMultiLine",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/log")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, false, "Request", "string", `request:"mediaType=text/plain"`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader)
|
|
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)
|
|
req.Header.Set("Content-Type", reqContentType)
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.LogMultiLineResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
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))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
fallthrough
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.LogMultiLineResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// EnablePaperTrail - Enabling Papertrail
|
|
// This endpoint will enable all Plex Media Serverlogs to be sent to the Papertrail networked logging site for a period of time.
|
|
func (s *Log) EnablePaperTrail(ctx context.Context) (*operations.EnablePaperTrailResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "enablePaperTrail",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/log/networked")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", opURL, 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)
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.EnablePaperTrailResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
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))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
fallthrough
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode == 403:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.EnablePaperTrailResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
}
|