mirror of
https://github.com/LukeHagar/plexterraform.git
synced 2025-12-06 04:20:52 +00:00
949 lines
33 KiB
Go
949 lines
33 KiB
Go
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
|
|
|
package sdk
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/internal/hooks"
|
|
"github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/internal/utils"
|
|
"github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/models/errors"
|
|
"github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/models/operations"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// 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) (*operations.CreatePlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "createPlaylist",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/playlists")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
_httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.CreatePlaylistResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.CreatePlaylistResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.TwoHundredApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.CreatePlaylistPlaylistsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.FourHundredAndOneApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetPlaylists - Get All Playlists
|
|
// Get All Playlists given the specified filters.
|
|
func (s *Playlists) GetPlaylists(ctx context.Context, request operations.GetPlaylistsRequest) (*operations.GetPlaylistsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getPlaylists",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := url.JoinPath(baseURL, "/playlists")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, nil); err != nil {
|
|
return nil, fmt.Errorf("error populating query params: %w", err)
|
|
}
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
_httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.GetPlaylistsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.GetPlaylistsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.TwoHundredApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.GetPlaylistsPlaylistsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.FourHundredAndOneApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// 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, request operations.GetPlaylistRequest) (*operations.GetPlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getPlaylist",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent)
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
_httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else 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,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.GetPlaylistResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.TwoHundredApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.GetPlaylistPlaylistsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.FourHundredAndOneApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// DeletePlaylist - Deletes a Playlist
|
|
// This endpoint will delete a playlist
|
|
func (s *Playlists) DeletePlaylist(ctx context.Context, request operations.DeletePlaylistRequest) (*operations.DeletePlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "deletePlaylist",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
_httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.DeletePlaylistResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
fallthrough
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.DeletePlaylistResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// 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, request operations.UpdatePlaylistRequest) (*operations.UpdatePlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "updatePlaylist",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
_httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.UpdatePlaylistResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
fallthrough
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.UpdatePlaylistResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// 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, request operations.GetPlaylistContentsRequest) (*operations.GetPlaylistContentsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "getPlaylistContents",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, nil); err != nil {
|
|
return nil, fmt.Errorf("error populating query params: %w", err)
|
|
}
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
_httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.GetPlaylistContentsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.GetPlaylistContentsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.TwoHundredApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.GetPlaylistContentsPlaylistsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.FourHundredAndOneApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// ClearPlaylistContents - Delete Playlist Contents
|
|
// Clears a playlist, only works with dumb playlists. Returns the playlist.
|
|
func (s *Playlists) ClearPlaylistContents(ctx context.Context, request operations.ClearPlaylistContentsRequest) (*operations.ClearPlaylistContentsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "clearPlaylistContents",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
_httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.ClearPlaylistContentsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
fallthrough
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.ClearPlaylistContentsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// 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, request operations.AddPlaylistContentsRequest) (*operations.AddPlaylistContentsResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "addPlaylistContents",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
_httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.AddPlaylistContentsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.AddPlaylistContentsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.TwoHundredApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.AddPlaylistContentsPlaylistsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.FourHundredAndOneApplicationJSONObject = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// 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, request operations.UploadPlaylistRequest) (*operations.UploadPlaylistResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "uploadPlaylist",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpRes, err := s.sdkConfiguration.Client.Do(req)
|
|
if err != nil || httpRes == nil {
|
|
if err != nil {
|
|
err = fmt.Errorf("error sending request: %w", err)
|
|
} else {
|
|
err = fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
_, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{}, httpRes.StatusCode) {
|
|
_httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
res := &operations.UploadPlaylistResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
fallthrough
|
|
case httpRes.StatusCode == 400:
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
var out operations.UploadPlaylistResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
return nil, errors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
default:
|
|
return nil, errors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|