mirror of
https://github.com/LukeHagar/plexgo.git
synced 2025-12-06 04:20:46 +00:00
2197 lines
62 KiB
Go
2197 lines
62 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"
|
|
)
|
|
|
|
// Playlists are ordered collections of media. They can be dumb (just a list of media) or smart (based on a media query, such as "all albums from 2017").
|
|
// They can be organized in (optionally nesting) folders.
|
|
// Retrieving a playlist, or its items, will trigger a refresh of its metadata.
|
|
// This may cause the duration and number of items to change.
|
|
type Playlists struct {
|
|
sdkConfiguration sdkConfiguration
|
|
}
|
|
|
|
func newPlaylists(sdkConfig sdkConfiguration) *Playlists {
|
|
return &Playlists{
|
|
sdkConfiguration: sdkConfig,
|
|
}
|
|
}
|
|
|
|
// CreatePlaylist - Create a Playlist
|
|
// Create a new playlist. By default the playlist is blank. To create a playlist along with a first item, pass:
|
|
// - `uri` - The content URI for what we're playing (e.g. `server://1234/com.plexapp.plugins.library/library/metadata/1`).
|
|
// - `playQueueID` - To create a playlist from an existing play queue.
|
|
func (s *Playlists) CreatePlaylist(ctx context.Context, request operations.CreatePlaylistRequest, opts ...operations.Option) (*operations.CreatePlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "createPlaylist",
|
|
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, "/playlists")
|
|
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, "POST", 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.CreatePlaylistResponse{
|
|
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.CreatePlaylistResponseBody
|
|
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.CreatePlaylistBadRequest
|
|
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.CreatePlaylistUnauthorized
|
|
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
|
|
|
|
}
|
|
|
|
// GetPlaylists - Get All Playlists
|
|
// Get All Playlists given the specified filters.
|
|
func (s *Playlists) GetPlaylists(ctx context.Context, playlistType *operations.PlaylistType, smart *operations.QueryParamSmart, opts ...operations.Option) (*operations.GetPlaylistsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getPlaylists",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetPlaylistsRequest{
|
|
PlaylistType: playlistType,
|
|
Smart: smart,
|
|
}
|
|
|
|
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, "/playlists")
|
|
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.GetPlaylistsResponse{
|
|
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.GetPlaylistsResponseBody
|
|
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.GetPlaylistsBadRequest
|
|
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.GetPlaylistsUnauthorized
|
|
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
|
|
|
|
}
|
|
|
|
// GetPlaylist - Retrieve Playlist
|
|
// Gets detailed metadata for a playlist. A playlist for many purposes (rating, editing metadata, tagging), can be treated like a regular metadata item:
|
|
// Smart playlist details contain the `content` attribute. This is the content URI for the generator. This can then be parsed by a client to provide smart playlist editing.
|
|
func (s *Playlists) GetPlaylist(ctx context.Context, playlistID float64, opts ...operations.Option) (*operations.GetPlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getPlaylist",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetPlaylistRequest{
|
|
PlaylistID: playlistID,
|
|
}
|
|
|
|
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, "/playlists/{playlistID}", 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.GetPlaylistResponse{
|
|
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.GetPlaylistResponseBody
|
|
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.GetPlaylistBadRequest
|
|
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.GetPlaylistUnauthorized
|
|
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
|
|
|
|
}
|
|
|
|
// DeletePlaylist - Deletes a Playlist
|
|
// This endpoint will delete a playlist
|
|
func (s *Playlists) DeletePlaylist(ctx context.Context, playlistID float64, opts ...operations.Option) (*operations.DeletePlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "deletePlaylist",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.DeletePlaylistRequest{
|
|
PlaylistID: playlistID,
|
|
}
|
|
|
|
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, "/playlists/{playlistID}", 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.DeletePlaylistResponse{
|
|
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 == 204:
|
|
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.DeletePlaylistBadRequest
|
|
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.DeletePlaylistUnauthorized
|
|
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
|
|
|
|
}
|
|
|
|
// UpdatePlaylist - Update a Playlist
|
|
// From PMS version 1.9.1 clients can also edit playlist metadata using this endpoint as they would via `PUT /library/metadata/{playlistID}`
|
|
func (s *Playlists) UpdatePlaylist(ctx context.Context, playlistID float64, title *string, summary *string, opts ...operations.Option) (*operations.UpdatePlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "updatePlaylist",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.UpdatePlaylistRequest{
|
|
PlaylistID: playlistID,
|
|
Title: title,
|
|
Summary: summary,
|
|
}
|
|
|
|
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, "/playlists/{playlistID}", 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, "PUT", 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.UpdatePlaylistResponse{
|
|
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.UpdatePlaylistBadRequest
|
|
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.UpdatePlaylistUnauthorized
|
|
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
|
|
|
|
}
|
|
|
|
// GetPlaylistContents - Retrieve Playlist Contents
|
|
// Gets the contents of a playlist. Should be paged by clients via standard mechanisms.
|
|
// By default leaves are returned (e.g. episodes, movies). In order to return other types you can use the `type` parameter.
|
|
// For example, you could use this to display a list of recently added albums vis a smart playlist.
|
|
// Note that for dumb playlists, items have a `playlistItemID` attribute which is used for deleting or moving items.
|
|
func (s *Playlists) GetPlaylistContents(ctx context.Context, playlistID float64, type_ operations.GetPlaylistContentsQueryParamType, opts ...operations.Option) (*operations.GetPlaylistContentsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getPlaylistContents",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.GetPlaylistContentsRequest{
|
|
PlaylistID: playlistID,
|
|
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, "/playlists/{playlistID}/items", 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.GetPlaylistContentsResponse{
|
|
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.GetPlaylistContentsResponseBody
|
|
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.GetPlaylistContentsBadRequest
|
|
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.GetPlaylistContentsUnauthorized
|
|
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
|
|
|
|
}
|
|
|
|
// ClearPlaylistContents - Delete Playlist Contents
|
|
// Clears a playlist, only works with dumb playlists. Returns the playlist.
|
|
func (s *Playlists) ClearPlaylistContents(ctx context.Context, playlistID float64, opts ...operations.Option) (*operations.ClearPlaylistContentsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "clearPlaylistContents",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.ClearPlaylistContentsRequest{
|
|
PlaylistID: playlistID,
|
|
}
|
|
|
|
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, "/playlists/{playlistID}/items", 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.ClearPlaylistContentsResponse{
|
|
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.ClearPlaylistContentsBadRequest
|
|
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.ClearPlaylistContentsUnauthorized
|
|
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
|
|
|
|
}
|
|
|
|
// AddPlaylistContents - Adding to a Playlist
|
|
// Adds a generator to a playlist, same parameters as the POST to create. With a dumb playlist, this adds the specified items to the playlist.
|
|
// With a smart playlist, passing a new `uri` parameter replaces the rules for the playlist. Returns the playlist.
|
|
func (s *Playlists) AddPlaylistContents(ctx context.Context, playlistID float64, uri string, playQueueID *float64, opts ...operations.Option) (*operations.AddPlaylistContentsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "addPlaylistContents",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.AddPlaylistContentsRequest{
|
|
PlaylistID: playlistID,
|
|
URI: uri,
|
|
PlayQueueID: playQueueID,
|
|
}
|
|
|
|
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, "/playlists/{playlistID}/items", 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, "PUT", 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.AddPlaylistContentsResponse{
|
|
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.AddPlaylistContentsResponseBody
|
|
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.AddPlaylistContentsBadRequest
|
|
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.AddPlaylistContentsUnauthorized
|
|
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
|
|
|
|
}
|
|
|
|
// UploadPlaylist - Upload Playlist
|
|
// Imports m3u playlists by passing a path on the server to scan for m3u-formatted playlist files, or a path to a single playlist file.
|
|
func (s *Playlists) UploadPlaylist(ctx context.Context, path string, force operations.QueryParamForce, sectionID int64, opts ...operations.Option) (*operations.UploadPlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "uploadPlaylist",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.UploadPlaylistRequest{
|
|
Path: path,
|
|
Force: force,
|
|
SectionID: sectionID,
|
|
}
|
|
|
|
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, "/playlists/upload")
|
|
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, "POST", 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.UploadPlaylistResponse{
|
|
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.UploadPlaylistBadRequest
|
|
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.UploadPlaylistUnauthorized
|
|
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
|
|
|
|
}
|