mirror of
https://github.com/LukeHagar/plexgo.git
synced 2025-12-06 04:20:46 +00:00
3024 lines
86 KiB
Go
3024 lines
86 KiB
Go
// Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
|
|
package plexgo
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"github.com/LukeHagar/plexgo/internal/hooks"
|
|
"github.com/LukeHagar/plexgo/internal/utils"
|
|
"github.com/LukeHagar/plexgo/models/operations"
|
|
"github.com/LukeHagar/plexgo/models/sdkerrors"
|
|
"github.com/cenkalti/backoff/v4"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// Library - API Calls interacting with Plex Media Server Libraries
|
|
type Library struct {
|
|
sdkConfiguration sdkConfiguration
|
|
}
|
|
|
|
func newLibrary(sdkConfig sdkConfiguration) *Library {
|
|
return &Library{
|
|
sdkConfiguration: sdkConfig,
|
|
}
|
|
}
|
|
|
|
// GetFileHash - Get Hash Value
|
|
// This resource returns hash values for local files
|
|
func (s *Library) GetFileHash(ctx context.Context, url_ string, type_ *float64, opts ...operations.Option) (*operations.GetFileHashResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getFileHash",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetFileHashRequest{
|
|
URL: url_,
|
|
Type: type_,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/library/hashes")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetFileHashResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetFileHashResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetFileHashLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetRecentlyAdded - Get Recently Added
|
|
// This endpoint will return the recently added content.
|
|
func (s *Library) GetRecentlyAdded(ctx context.Context, xPlexContainerStart *int, xPlexContainerSize *int, opts ...operations.Option) (*operations.GetRecentlyAddedResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getRecentlyAdded",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetRecentlyAddedRequest{
|
|
XPlexContainerStart: xPlexContainerStart,
|
|
XPlexContainerSize: xPlexContainerSize,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/library/recentlyAdded")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetRecentlyAddedResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetRecentlyAddedResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetRecentlyAddedResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetRecentlyAddedLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetAllLibraries - Get All Libraries
|
|
// A library section (commonly referred to as just a library) is a collection of media.
|
|
// Libraries are typed, and depending on their type provide either a flat or a hierarchical view of the media.
|
|
// For example, a music library has an artist > albums > tracks structure, whereas a movie library is flat.
|
|
//
|
|
// Libraries have features beyond just being a collection of media; for starters, they include information about supported types, filters and sorts.
|
|
// This allows a client to provide a rich interface around the media (e.g. allow sorting movies by release year).
|
|
func (s *Library) GetAllLibraries(ctx context.Context, opts ...operations.Option) (*operations.GetAllLibrariesResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "get-all-libraries",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/library/sections")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetAllLibrariesResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetAllLibrariesResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetAllLibrariesResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetAllLibrariesLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetLibraryDetails - Get Library Details
|
|
// ## Library Details Endpoint
|
|
//
|
|
// This endpoint provides comprehensive details about the library, focusing on organizational aspects rather than the content itself.
|
|
//
|
|
// The details include:
|
|
//
|
|
// ### Directories
|
|
// Organized into three categories:
|
|
//
|
|
// - **Primary Directories**:
|
|
// - Used in some clients for quick access to media subsets (e.g., "All", "On Deck").
|
|
// - Most can be replicated via media queries.
|
|
// - Customizable by users.
|
|
//
|
|
// - **Secondary Directories**:
|
|
// - Marked with `secondary="1"`.
|
|
// - Used in older clients for structured navigation.
|
|
//
|
|
// - **Special Directories**:
|
|
// - Includes a "By Folder" entry for filesystem-based browsing.
|
|
// - Contains an obsolete `search="1"` entry for on-the-fly search dialog creation.
|
|
//
|
|
// ### Types
|
|
// Each type in the library comes with a set of filters and sorts, aiding in building dynamic media controls:
|
|
//
|
|
// - **Type Object Attributes**:
|
|
// - `key`: Endpoint for the media list of this type.
|
|
// - `type`: Metadata type (if standard Plex type).
|
|
// - `title`: Title for this content type (e.g., "Movies").
|
|
//
|
|
// - **Filter Objects**:
|
|
// - Subset of the media query language.
|
|
// - Attributes include `filter` (name), `filterType` (data type), `key` (endpoint for value range), and `title`.
|
|
//
|
|
// - **Sort Objects**:
|
|
// - Description of sort fields.
|
|
// - Attributes include `defaultDirection` (asc/desc), `descKey` and `key` (sort parameters), and `title`.
|
|
//
|
|
// > **Note**: Filters and sorts are optional; without them, no filtering controls are rendered.
|
|
func (s *Library) GetLibraryDetails(ctx context.Context, sectionKey int, includeDetails *operations.IncludeDetails, opts ...operations.Option) (*operations.GetLibraryDetailsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "get-library-details",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetLibraryDetailsRequest{
|
|
SectionKey: sectionKey,
|
|
IncludeDetails: includeDetails,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionKey}", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetLibraryDetailsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetLibraryDetailsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetLibraryDetailsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetLibraryDetailsLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// DeleteLibrary - Delete Library Section
|
|
// Delete a library using a specific section id
|
|
func (s *Library) DeleteLibrary(ctx context.Context, sectionKey int, opts ...operations.Option) (*operations.DeleteLibraryResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "deleteLibrary",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.DeleteLibraryRequest{
|
|
SectionKey: sectionKey,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionKey}", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "DELETE", 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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.DeleteLibraryResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.DeleteLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.DeleteLibraryLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetLibraryItems - Get Library Items
|
|
// Fetches details from a specific section of the library identified by a section key and a tag. The tag parameter accepts the following values:
|
|
// - `all`: All items in the section.
|
|
// - `unwatched`: Items that have not been played.
|
|
// - `newest`: Items that are recently released.
|
|
// - `recentlyAdded`: Items that are recently added to the library.
|
|
// - `recentlyViewed`: Items that were recently viewed.
|
|
// - `onDeck`: Items to continue watching.
|
|
// - `collection`: Items categorized by collection.
|
|
// - `edition`: Items categorized by edition.
|
|
// - `genre`: Items categorized by genre.
|
|
// - `year`: Items categorized by year of release.
|
|
// - `decade`: Items categorized by decade.
|
|
// - `director`: Items categorized by director.
|
|
// - `actor`: Items categorized by starring actor.
|
|
// - `country`: Items categorized by country of origin.
|
|
// - `contentRating`: Items categorized by content rating.
|
|
// - `rating`: Items categorized by rating.
|
|
// - `resolution`: Items categorized by resolution.
|
|
// - `firstCharacter`: Items categorized by the first letter.
|
|
// - `folder`: Items categorized by folder.
|
|
func (s *Library) GetLibraryItems(ctx context.Context, request operations.GetLibraryItemsRequest, opts ...operations.Option) (*operations.GetLibraryItemsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "get-library-items",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionKey}/{tag}", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetLibraryItemsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetLibraryItemsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetLibraryItemsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetLibraryItemsLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetRefreshLibraryMetadata - Refresh Metadata Of The Library
|
|
// This endpoint Refreshes all the Metadata of the library.
|
|
func (s *Library) GetRefreshLibraryMetadata(ctx context.Context, sectionKey int, force *operations.Force, opts ...operations.Option) (*operations.GetRefreshLibraryMetadataResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "get-refresh-library-metadata",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetRefreshLibraryMetadataRequest{
|
|
SectionKey: sectionKey,
|
|
Force: force,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionKey}/refresh", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetRefreshLibraryMetadataResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetRefreshLibraryMetadataResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetRefreshLibraryMetadataLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetSearchLibrary - Search Library
|
|
// Search for content within a specific section of the library.
|
|
//
|
|
// ### Types
|
|
// Each type in the library comes with a set of filters and sorts, aiding in building dynamic media controls:
|
|
//
|
|
// - **Type Object Attributes**:
|
|
// - `type`: Metadata type (if standard Plex type).
|
|
// - `title`: Title for this content type (e.g., "Movies").
|
|
//
|
|
// - **Filter Objects**:
|
|
// - Subset of the media query language.
|
|
// - Attributes include `filter` (name), `filterType` (data type), `key` (endpoint for value range), and `title`.
|
|
//
|
|
// - **Sort Objects**:
|
|
// - Description of sort fields.
|
|
// - Attributes include `defaultDirection` (asc/desc), `descKey` and `key` (sort parameters), and `title`.
|
|
//
|
|
// > **Note**: Filters and sorts are optional; without them, no filtering controls are rendered.
|
|
func (s *Library) GetSearchLibrary(ctx context.Context, sectionKey int, type_ operations.QueryParamType, opts ...operations.Option) (*operations.GetSearchLibraryResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "get-search-library",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetSearchLibraryRequest{
|
|
SectionKey: sectionKey,
|
|
Type: type_,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionKey}/search", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetSearchLibraryResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetSearchLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetSearchLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetSearchLibraryLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetMetaDataByRatingKey - Get Metadata by RatingKey
|
|
// This endpoint will return the metadata of a library item specified with the ratingKey.
|
|
func (s *Library) GetMetaDataByRatingKey(ctx context.Context, ratingKey int64, opts ...operations.Option) (*operations.GetMetaDataByRatingKeyResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "get-meta-data-by-rating-key",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetMetaDataByRatingKeyRequest{
|
|
RatingKey: ratingKey,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/library/metadata/{ratingKey}", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetMetaDataByRatingKeyResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetMetaDataByRatingKeyResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetMetaDataByRatingKeyResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetMetaDataByRatingKeyLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetMetadataChildren - Get Items Children
|
|
// This endpoint will return the children of of a library item specified with the ratingKey.
|
|
func (s *Library) GetMetadataChildren(ctx context.Context, ratingKey float64, includeElements *string, opts ...operations.Option) (*operations.GetMetadataChildrenResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getMetadataChildren",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetMetadataChildrenRequest{
|
|
RatingKey: ratingKey,
|
|
IncludeElements: includeElements,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/library/metadata/{ratingKey}/children", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetMetadataChildrenResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetMetadataChildrenResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetMetadataChildrenResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetMetadataChildrenLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetTopWatchedContent - Get Top Watched Content
|
|
// This endpoint will return the top watched content from libraries of a certain type
|
|
func (s *Library) GetTopWatchedContent(ctx context.Context, type_ operations.GetTopWatchedContentQueryParamType, includeGuids *int64, opts ...operations.Option) (*operations.GetTopWatchedContentResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getTopWatchedContent",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetTopWatchedContentRequest{
|
|
Type: type_,
|
|
IncludeGuids: includeGuids,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/library/all/top")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetTopWatchedContentResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetTopWatchedContentResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetTopWatchedContentResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetTopWatchedContentLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetOnDeck - Get On Deck
|
|
// This endpoint will return the on deck content.
|
|
func (s *Library) GetOnDeck(ctx context.Context, opts ...operations.Option) (*operations.GetOnDeckResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getOnDeck",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/library/onDeck")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
timeout := o.Timeout
|
|
if timeout == nil {
|
|
timeout = s.sdkConfiguration.Timeout
|
|
}
|
|
|
|
if timeout != nil {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, *timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
globalRetryConfig := s.sdkConfiguration.RetryConfig
|
|
retryConfig := o.Retries
|
|
if retryConfig == nil {
|
|
if globalRetryConfig != nil {
|
|
retryConfig = globalRetryConfig
|
|
}
|
|
}
|
|
|
|
var httpRes *http.Response
|
|
if retryConfig != nil {
|
|
httpRes, err = utils.Retry(ctx, utils.Retries{
|
|
Config: retryConfig,
|
|
StatusCodes: []string{
|
|
"429",
|
|
"500",
|
|
"502",
|
|
"503",
|
|
"504",
|
|
},
|
|
}, func() (*http.Response, error) {
|
|
if req.Body != nil {
|
|
copyBody, err := req.GetBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, backoff.Permanent(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 httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
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{"400", "401", "4XX", "5XX"}, 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.GetOnDeckResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
getRawBody := func() ([]byte, error) {
|
|
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))
|
|
return rawBody, nil
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetOnDeckResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetOnDeckResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out sdkerrors.GetOnDeckLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.RawResponse = httpRes
|
|
return nil, &out
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := getRawBody()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|