mirror of
https://github.com/LukeHagar/plexgo.git
synced 2025-12-06 04:20:46 +00:00
1198 lines
33 KiB
Go
1198 lines
33 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"
|
|
)
|
|
|
|
// Media - API Calls interacting with Plex Media Server Media
|
|
type Media struct {
|
|
sdkConfiguration sdkConfiguration
|
|
}
|
|
|
|
func newMedia(sdkConfig sdkConfiguration) *Media {
|
|
return &Media{
|
|
sdkConfiguration: sdkConfig,
|
|
}
|
|
}
|
|
|
|
// MarkPlayed - Mark Media Played
|
|
// This will mark the provided media key as Played.
|
|
func (s *Media) MarkPlayed(ctx context.Context, key float64, opts ...operations.Option) (*operations.MarkPlayedResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "markPlayed",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.MarkPlayedRequest{
|
|
Key: key,
|
|
}
|
|
|
|
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, "/:/scrobble")
|
|
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.MarkPlayedResponse{
|
|
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.MarkPlayedResponseBody
|
|
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.MarkPlayedMediaResponseBody
|
|
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
|
|
|
|
}
|
|
|
|
// MarkUnplayed - Mark Media Unplayed
|
|
// This will mark the provided media key as Unplayed.
|
|
func (s *Media) MarkUnplayed(ctx context.Context, key float64, opts ...operations.Option) (*operations.MarkUnplayedResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "markUnplayed",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.MarkUnplayedRequest{
|
|
Key: key,
|
|
}
|
|
|
|
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, "/:/unscrobble")
|
|
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.MarkUnplayedResponse{
|
|
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.MarkUnplayedResponseBody
|
|
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.MarkUnplayedMediaResponseBody
|
|
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
|
|
|
|
}
|
|
|
|
// UpdatePlayProgress - Update Media Play Progress
|
|
// This API command can be used to update the play progress of a media item.
|
|
func (s *Media) UpdatePlayProgress(ctx context.Context, key string, time float64, state string, opts ...operations.Option) (*operations.UpdatePlayProgressResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "updatePlayProgress",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
request := operations.UpdatePlayProgressRequest{
|
|
Key: key,
|
|
Time: time,
|
|
State: state,
|
|
}
|
|
|
|
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, "/:/progress")
|
|
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.UpdatePlayProgressResponse{
|
|
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.UpdatePlayProgressResponseBody
|
|
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.UpdatePlayProgressMediaResponseBody
|
|
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
|
|
|
|
}
|
|
|
|
// GetBannerImage - Get Banner Image
|
|
// Gets the banner image of the media item
|
|
func (s *Media) GetBannerImage(ctx context.Context, request operations.GetBannerImageRequest, opts ...operations.Option) (*operations.GetBannerImageResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "get-banner-image",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/library/metadata/{ratingKey}/banner", 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", "image/jpeg")
|
|
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.GetBannerImageResponse{
|
|
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:
|
|
res.Headers = httpRes.Header
|
|
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `image/jpeg`):
|
|
res.ResponseStream = httpRes.Body
|
|
|
|
return res, nil
|
|
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.GetBannerImageResponseBody
|
|
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.GetBannerImageMediaResponseBody
|
|
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
|
|
|
|
}
|
|
|
|
// GetThumbImage - Get Thumb Image
|
|
// Gets the thumbnail image of the media item
|
|
func (s *Media) GetThumbImage(ctx context.Context, request operations.GetThumbImageRequest, opts ...operations.Option) (*operations.GetThumbImageResponse, error) {
|
|
hookCtx := hooks.HookContext{
|
|
Context: ctx,
|
|
OperationID: "get-thumb-image",
|
|
OAuth2Scopes: []string{},
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
o := operations.Options{}
|
|
supportedOptions := []string{
|
|
operations.SupportedOptionRetries,
|
|
operations.SupportedOptionTimeout,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(&o, supportedOptions...); err != nil {
|
|
return nil, fmt.Errorf("error applying option: %w", err)
|
|
}
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/library/metadata/{ratingKey}/thumb", 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", "image/jpeg")
|
|
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.GetThumbImageResponse{
|
|
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:
|
|
res.Headers = httpRes.Header
|
|
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `image/jpeg`):
|
|
res.ResponseStream = httpRes.Body
|
|
|
|
return res, nil
|
|
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.GetThumbImageResponseBody
|
|
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.GetThumbImageMediaResponseBody
|
|
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
|
|
|
|
}
|