// 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" ) // 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) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url := strings.TrimSuffix(baseURL, "/") + "/log" 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) if err := utils.PopulateQueryParams(ctx, req, request, nil); err != nil { return nil, fmt.Errorf("error populating query params: %w", err) } 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.LogLineResponse{ 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.LogLineResponseBody 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 } // 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) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url := strings.TrimSuffix(baseURL, "/") + "/log" bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, false, "Request", "string", `request:"mediaType=text/plain"`) if err != nil { return nil, fmt.Errorf("error serializing request body: %w", err) } if bodyReader == nil { return nil, fmt.Errorf("request body is required") } debugBody := bytes.NewBuffer([]byte{}) debugReader := io.TeeReader(bodyReader, debugBody) req, err := http.NewRequestWithContext(ctx, "POST", url, debugReader) 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) 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.Request.Body = io.NopCloser(debugBody) httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) contentType := httpRes.Header.Get("Content-Type") res := &operations.LogMultiLineResponse{ 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.LogMultiLineResponseBody 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 } // 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) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url := strings.TrimSuffix(baseURL, "/") + "/log/networked" 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.EnablePaperTrailResponse{ StatusCode: httpRes.StatusCode, ContentType: contentType, RawResponse: httpRes, } switch { case httpRes.StatusCode == 200: fallthrough case httpRes.StatusCode == 400: fallthrough case httpRes.StatusCode == 403: case httpRes.StatusCode == 401: switch { case utils.MatchContentType(contentType, `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, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } } return res, nil }