mirror of
https://github.com/LukeHagar/plexgo.git
synced 2025-12-06 04:20:46 +00:00
921 lines
32 KiB
Go
921 lines
32 KiB
Go
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
|
|
|
package plexgo
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"github.com/LukeHagar/plexgo/internal/utils"
|
|
"github.com/LukeHagar/plexgo/models/operations"
|
|
"github.com/LukeHagar/plexgo/models/sdkerrors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// Library - API Calls interacting with Plex Media Server Libraries
|
|
type Library struct {
|
|
sdkConfiguration sdkConfiguration
|
|
}
|
|
|
|
func newLibrary(sdkConfig sdkConfiguration) *Library {
|
|
return &Library{
|
|
sdkConfiguration: sdkConfig,
|
|
}
|
|
}
|
|
|
|
// GetFileHash - Get Hash Value
|
|
// This resource returns hash values for local files
|
|
func (s *Library) GetFileHash(ctx context.Context, url_ string, type_ *float64) (*operations.GetFileHashResponse, error) {
|
|
request := operations.GetFileHashRequest{
|
|
URL: url_,
|
|
Type: type_,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url := strings.TrimSuffix(baseURL, "/") + "/library/hashes"
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
}
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetFileHashResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetFileHashResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetRecentlyAdded - Get Recently Added
|
|
// This endpoint will return the recently added content.
|
|
func (s *Library) GetRecentlyAdded(ctx context.Context) (*operations.GetRecentlyAddedResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url := strings.TrimSuffix(baseURL, "/") + "/library/recentlyAdded"
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetRecentlyAddedResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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(contentType, `application/json`):
|
|
var out operations.GetRecentlyAddedResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetRecentlyAddedResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetLibraries - Get All Libraries
|
|
// A library section (commonly referred to as just a library) is a collection of media.
|
|
// Libraries are typed, and depending on their type provide either a flat or a hierarchical view of the media.
|
|
// For example, a music library has an artist > albums > tracks structure, whereas a movie library is flat.
|
|
//
|
|
// Libraries have features beyond just being a collection of media; for starters, they include information about supported types, filters and sorts.
|
|
// This allows a client to provide a rich interface around the media (e.g. allow sorting movies by release year).
|
|
func (s *Library) GetLibraries(ctx context.Context) (*operations.GetLibrariesResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url := strings.TrimSuffix(baseURL, "/") + "/library/sections"
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetLibrariesResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetLibrariesResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetLibrary - Get Library Details
|
|
// Returns details for the library. This can be thought of as an interstitial endpoint because it contains information about the library, rather than content itself. These details are:
|
|
//
|
|
// - A list of `Directory` objects: These used to be used by clients to build a menuing system. There are four flavors of directory found here:
|
|
// - Primary: (e.g. all, On Deck) These are still used in some clients to provide "shortcuts" to subsets of media. However, with the exception of On Deck, all of them can be created by media queries, and the desire is to allow these to be customized by users.
|
|
// - Secondary: These are marked with `secondary="1"` and were used by old clients to provide nested menus allowing for primative (but structured) navigation.
|
|
// - Special: There is a By Folder entry which allows browsing the media by the underlying filesystem structure, and there's a completely obsolete entry marked `search="1"` which used to be used to allow clients to build search dialogs on the fly.
|
|
//
|
|
// - A list of `Type` objects: These represent the types of things found in this library, and for each one, a list of `Filter` and `Sort` objects. These can be used to build rich controls around a grid of media to allow filtering and organizing. Note that these filters and sorts are optional, and without them, the client won't render any filtering controls. The `Type` object contains:
|
|
// - `key`: This provides the root endpoint returning the actual media list for the type.
|
|
// - `type`: This is the metadata type for the type (if a standard Plex type).
|
|
// - `title`: The title for for the content of this type (e.g. "Movies").
|
|
//
|
|
// - Each `Filter` object contains a description of the filter. Note that it is not an exhaustive list of the full media query language, but an inportant subset useful for top-level API.
|
|
// - `filter`: This represents the filter name used for the filter, which can be used to construct complex media queries with.
|
|
// - `filterType`: This is either `string`, `integer`, or `boolean`, and describes the type of values used for the filter.
|
|
// - `key`: This provides the endpoint where the possible range of values for the filter can be retrieved (e.g. for a "Genre" filter, it returns a list of all the genres in the library). This will include a `type` argument that matches the metadata type of the Type element.
|
|
// - `title`: The title for the filter.
|
|
//
|
|
// - Each `Sort` object contains a description of the sort field.
|
|
// - `defaultDirection`: Can be either `asc` or `desc`, and specifies the default direction for the sort field (e.g. titles default to alphabetically ascending).
|
|
// - `descKey` and `key`: Contains the parameters passed to the `sort=...` media query for each direction of the sort.
|
|
// - `title`: The title of the field.
|
|
func (s *Library) GetLibrary(ctx context.Context, sectionID float64, includeDetails *operations.IncludeDetails) (*operations.GetLibraryResponse, error) {
|
|
request := operations.GetLibraryRequest{
|
|
SectionID: sectionID,
|
|
IncludeDetails: includeDetails,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionId}", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
}
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetLibraryResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// DeleteLibrary - Delete Library Section
|
|
// Delate a library using a specific section
|
|
func (s *Library) DeleteLibrary(ctx context.Context, sectionID float64) (*operations.DeleteLibraryResponse, error) {
|
|
request := operations.DeleteLibraryRequest{
|
|
SectionID: sectionID,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionId}", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "DELETE", url, 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)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.DeleteLibraryResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.DeleteLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetLibraryItems - Get Library Items
|
|
// This endpoint will return a list of library items filtered by the filter and type provided
|
|
func (s *Library) GetLibraryItems(ctx context.Context, sectionID float64, type_ *float64, filter *string) (*operations.GetLibraryItemsResponse, error) {
|
|
request := operations.GetLibraryItemsRequest{
|
|
SectionID: sectionID,
|
|
Type: type_,
|
|
Filter: filter,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionId}/all", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
}
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetLibraryItemsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetLibraryItemsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// RefreshLibrary - Refresh Library
|
|
// This endpoint Refreshes the library.
|
|
func (s *Library) RefreshLibrary(ctx context.Context, sectionID float64) (*operations.RefreshLibraryResponse, error) {
|
|
request := operations.RefreshLibraryRequest{
|
|
SectionID: sectionID,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionId}/refresh", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.RefreshLibraryResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.RefreshLibraryResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetLatestLibraryItems - Get Latest Library Items
|
|
// This endpoint will return a list of the latest library items filtered by the filter and type provided
|
|
func (s *Library) GetLatestLibraryItems(ctx context.Context, sectionID float64, type_ float64, filter *string) (*operations.GetLatestLibraryItemsResponse, error) {
|
|
request := operations.GetLatestLibraryItemsRequest{
|
|
SectionID: sectionID,
|
|
Type: type_,
|
|
Filter: filter,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionId}/latest", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
}
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetLatestLibraryItemsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetLatestLibraryItemsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetCommonLibraryItems - Get Common Library Items
|
|
// Represents a "Common" item. It contains only the common attributes of the items selected by the provided filter
|
|
func (s *Library) GetCommonLibraryItems(ctx context.Context, sectionID float64, type_ float64, filter *string) (*operations.GetCommonLibraryItemsResponse, error) {
|
|
request := operations.GetCommonLibraryItemsRequest{
|
|
SectionID: sectionID,
|
|
Type: type_,
|
|
Filter: filter,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/library/sections/{sectionId}/common", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
}
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetCommonLibraryItemsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode == 404:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetCommonLibraryItemsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetMetadata - Get Items Metadata
|
|
// This endpoint will return the metadata of a library item specified with the ratingKey.
|
|
func (s *Library) GetMetadata(ctx context.Context, ratingKey float64) (*operations.GetMetadataResponse, error) {
|
|
request := operations.GetMetadataRequest{
|
|
RatingKey: ratingKey,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/library/metadata/{ratingKey}", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetMetadataResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetMetadataResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetMetadataChildren - Get Items Children
|
|
// This endpoint will return the children of of a library item specified with the ratingKey.
|
|
func (s *Library) GetMetadataChildren(ctx context.Context, ratingKey float64) (*operations.GetMetadataChildrenResponse, error) {
|
|
request := operations.GetMetadataChildrenRequest{
|
|
RatingKey: ratingKey,
|
|
}
|
|
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/library/metadata/{ratingKey}/children", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetMetadataChildrenResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetMetadataChildrenResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetOnDeck - Get On Deck
|
|
// This endpoint will return the on deck content.
|
|
func (s *Library) GetOnDeck(ctx context.Context) (*operations.GetOnDeckResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url := strings.TrimSuffix(baseURL, "/") + "/library/onDeck"
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, 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)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetOnDeckResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
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(contentType, `application/json`):
|
|
var out operations.GetOnDeckResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode == 401:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.GetOnDeckResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
out.RawResponse = httpRes
|
|
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|