Files
plexterraform/internal/sdk/statistics.go

134 lines
4.3 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"
)
// Statistics - API Calls that perform operations with Plex Media Server Statistics
type Statistics struct {
sdkConfiguration sdkConfiguration
}
func newStatistics(sdkConfig sdkConfiguration) *Statistics {
return &Statistics{
sdkConfiguration: sdkConfig,
}
}
// GetStatistics - Get Media Statistics
// This will return the media statistics for the server
func (s *Statistics) GetStatistics(ctx context.Context, request operations.GetStatisticsRequest) (*operations.GetStatisticsResponse, error) {
hookCtx := hooks.HookContext{
Context: ctx,
OperationID: "getStatistics",
OAuth2Scopes: []string{},
SecuritySource: s.sdkConfiguration.Security,
}
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
opURL, err := url.JoinPath(baseURL, "/statistics/media")
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 if _httpRes != nil {
httpRes = _httpRes
}
} else {
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
if err != nil {
return nil, err
}
}
res := &operations.GetStatisticsResponse{
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:
switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
var out operations.GetStatisticsResponseBody
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
return nil, err
}
res.TwoHundredApplicationJSONObject = &out
default:
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
}
case httpRes.StatusCode == 400:
case httpRes.StatusCode == 401:
switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
var out operations.GetStatisticsStatisticsResponseBody
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
return nil, err
}
res.FourHundredAndOneApplicationJSONObject = &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
}