diff --git a/.speakeasy/gen.lock b/.speakeasy/gen.lock index ffaacd1..f04a48f 100755 --- a/.speakeasy/gen.lock +++ b/.speakeasy/gen.lock @@ -1,21 +1,22 @@ lockVersion: 2.0.0 id: e742591b-391d-4f4e-8484-d01a093b32ec management: - docChecksum: 95d4ac4070b7f9db42126fc81b258e02 + docChecksum: 94439465ae5d544f0e52b00719af05ee docVersion: 0.0.3 speakeasyVersion: internal - generationVersion: 2.237.3 - releaseVersion: 0.3.3 - configChecksum: 9e5cb1e3ab2e0be2fce9e48e71feacda + generationVersion: 2.239.4 + releaseVersion: 0.4.0 + configChecksum: b244e39f31dda4bc1f3428b5e4025f10 repoURL: https://github.com/LukeHagar/plexterraform.git repoSubDirectory: . published: true features: terraform: constsAndDefaults: 0.1.2 - core: 3.8.4 + core: 3.9.2 globalSecurity: 2.81.3 globalServerURLs: 2.82.1 + methodServerURLs: 2.82.1 nameOverrides: 2.81.1 unions: 2.81.7 generatedFiles: @@ -27,6 +28,8 @@ generatedFiles: - internal/sdk/search.go - internal/sdk/library.go - internal/sdk/log.go + - internal/sdk/tv.go + - internal/sdk/plex.go - internal/sdk/playlists.go - internal/sdk/security.go - internal/sdk/sessions.go @@ -130,6 +133,8 @@ generatedFiles: - internal/sdk/pkg/models/operations/logline.go - internal/sdk/pkg/models/operations/logmultiline.go - internal/sdk/pkg/models/operations/enablepapertrail.go + - internal/sdk/pkg/models/operations/getpin.go + - internal/sdk/pkg/models/operations/gettoken.go - internal/sdk/pkg/models/operations/createplaylist.go - internal/sdk/pkg/models/operations/getplaylists.go - internal/sdk/pkg/models/operations/getplaylist.go @@ -154,4 +159,5 @@ generatedFiles: - USAGE.md - internal/provider/provider.go - examples/provider/provider.tf + - internal/sdk/pkg/models/operations/options.go - .gitattributes diff --git a/README.md b/README.md index f71aaee..ade547e 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ terraform { required_providers { PlexAPI = { source = "LukeHagar/PlexAPI" - version = "0.3.3" + version = "0.4.0" } } } diff --git a/RELEASES.md b/RELEASES.md index c849888..6063a1f 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -94,4 +94,12 @@ Based on: - OpenAPI Doc 0.0.3 - Speakeasy CLI 1.150.0 (2.237.3) https://github.com/speakeasy-api/speakeasy ### Generated -- [terraform v0.3.3] . \ No newline at end of file +- [terraform v0.3.3] . + +## 2024-01-31 15:06:03 +### Changes +Based on: +- OpenAPI Doc 0.0.3 +- Speakeasy CLI 1.158.0 (2.239.4) https://github.com/speakeasy-api/speakeasy +### Generated +- [terraform v0.4.0] . \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 3e9e540..064888a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -17,7 +17,7 @@ terraform { required_providers { PlexAPI = { source = "LukeHagar/PlexAPI" - version = "0.3.3" + version = "0.4.0" } } } diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index 08894b9..586d7ef 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { PlexAPI = { source = "LukeHagar/PlexAPI" - version = "0.3.3" + version = "0.4.0" } } } diff --git a/gen.yaml b/gen.yaml index 89a7e2a..d24ea08 100644 --- a/gen.yaml +++ b/gen.yaml @@ -8,7 +8,7 @@ generation: fixes: nameResolutionDec2023: false terraform: - version: 0.3.3 + version: 0.4.0 author: LukeHagar imports: option: openapi diff --git a/internal/sdk/pkg/models/operations/getlibraries.go b/internal/sdk/pkg/models/operations/getlibraries.go index b293a61..8233d7c 100644 --- a/internal/sdk/pkg/models/operations/getlibraries.go +++ b/internal/sdk/pkg/models/operations/getlibraries.go @@ -45,19 +45,19 @@ func (o *GetLibrariesLibraryResponseBody) GetErrors() []GetLibrariesErrors { return o.Errors } -type Location struct { +type GetLibrariesLocation struct { ID *int `json:"id,omitempty"` Path *string `json:"path,omitempty"` } -func (o *Location) GetID() *int { +func (o *GetLibrariesLocation) GetID() *int { if o == nil { return nil } return o.ID } -func (o *Location) GetPath() *string { +func (o *GetLibrariesLocation) GetPath() *string { if o == nil { return nil } @@ -65,27 +65,27 @@ func (o *Location) GetPath() *string { } type GetLibrariesDirectory struct { - AllowSync *bool `json:"allowSync,omitempty"` - Art *string `json:"art,omitempty"` - Composite *string `json:"composite,omitempty"` - Filters *bool `json:"filters,omitempty"` - Refreshing *bool `json:"refreshing,omitempty"` - Thumb *string `json:"thumb,omitempty"` - Key *string `json:"key,omitempty"` - Type *string `json:"type,omitempty"` - Title *string `json:"title,omitempty"` - Agent *string `json:"agent,omitempty"` - Scanner *string `json:"scanner,omitempty"` - Language *string `json:"language,omitempty"` - UUID *string `json:"uuid,omitempty"` - UpdatedAt *int `json:"updatedAt,omitempty"` - CreatedAt *int `json:"createdAt,omitempty"` - ScannedAt *int `json:"scannedAt,omitempty"` - Content *bool `json:"content,omitempty"` - Directory *bool `json:"directory,omitempty"` - ContentChangedAt *int `json:"contentChangedAt,omitempty"` - Hidden *int `json:"hidden,omitempty"` - Location []Location `json:"Location,omitempty"` + AllowSync *bool `json:"allowSync,omitempty"` + Art *string `json:"art,omitempty"` + Composite *string `json:"composite,omitempty"` + Filters *bool `json:"filters,omitempty"` + Refreshing *bool `json:"refreshing,omitempty"` + Thumb *string `json:"thumb,omitempty"` + Key *string `json:"key,omitempty"` + Type *string `json:"type,omitempty"` + Title *string `json:"title,omitempty"` + Agent *string `json:"agent,omitempty"` + Scanner *string `json:"scanner,omitempty"` + Language *string `json:"language,omitempty"` + UUID *string `json:"uuid,omitempty"` + UpdatedAt *int `json:"updatedAt,omitempty"` + CreatedAt *int `json:"createdAt,omitempty"` + ScannedAt *int `json:"scannedAt,omitempty"` + Content *bool `json:"content,omitempty"` + Directory *bool `json:"directory,omitempty"` + ContentChangedAt *int `json:"contentChangedAt,omitempty"` + Hidden *int `json:"hidden,omitempty"` + Location []GetLibrariesLocation `json:"Location,omitempty"` } func (o *GetLibrariesDirectory) GetAllowSync() *bool { @@ -228,7 +228,7 @@ func (o *GetLibrariesDirectory) GetHidden() *int { return o.Hidden } -func (o *GetLibrariesDirectory) GetLocation() []Location { +func (o *GetLibrariesDirectory) GetLocation() []GetLibrariesLocation { if o == nil { return nil } diff --git a/internal/sdk/pkg/models/operations/getpin.go b/internal/sdk/pkg/models/operations/getpin.go new file mode 100644 index 0000000..025224f --- /dev/null +++ b/internal/sdk/pkg/models/operations/getpin.go @@ -0,0 +1,334 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package operations + +import ( + "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/utils" + "net/http" + "time" +) + +var GetPinServerList = []string{ + "https://plex.tv/api/v2", +} + +type GetPinRequest struct { + // Determines the kind of code returned by the API call + // Strong codes are used for Pin authentication flows + // Non-Strong codes are used for `Plex.tv/link` + // + Strong *bool `default:"false" queryParam:"style=form,explode=true,name=strong"` + // Plex Authentication Token + XPlexClientIdentifier string `header:"style=simple,explode=false,name=X-Plex-Client-Identifier"` +} + +func (g GetPinRequest) MarshalJSON() ([]byte, error) { + return utils.MarshalJSON(g, "", false) +} + +func (g *GetPinRequest) UnmarshalJSON(data []byte) error { + if err := utils.UnmarshalJSON(data, &g, "", false, false); err != nil { + return err + } + return nil +} + +func (o *GetPinRequest) GetStrong() *bool { + if o == nil { + return nil + } + return o.Strong +} + +func (o *GetPinRequest) GetXPlexClientIdentifier() string { + if o == nil { + return "" + } + return o.XPlexClientIdentifier +} + +type GetPinErrors struct { + Code *float64 `json:"code,omitempty"` + Message *string `json:"message,omitempty"` + Status *float64 `json:"status,omitempty"` +} + +func (o *GetPinErrors) GetCode() *float64 { + if o == nil { + return nil + } + return o.Code +} + +func (o *GetPinErrors) GetMessage() *string { + if o == nil { + return nil + } + return o.Message +} + +func (o *GetPinErrors) GetStatus() *float64 { + if o == nil { + return nil + } + return o.Status +} + +// GetPinPlexTvResponseBody - X-Plex-Client-Identifier is missing +type GetPinPlexTvResponseBody struct { + Errors []GetPinErrors `json:"errors,omitempty"` +} + +func (o *GetPinPlexTvResponseBody) GetErrors() []GetPinErrors { + if o == nil { + return nil + } + return o.Errors +} + +type Location struct { + Code *string `json:"code,omitempty"` + EuropeanUnionMember *bool `json:"european_union_member,omitempty"` + ContinentCode *string `json:"continent_code,omitempty"` + Country *string `json:"country,omitempty"` + City *string `json:"city,omitempty"` + TimeZone *string `json:"time_zone,omitempty"` + PostalCode *float64 `json:"postal_code,omitempty"` + InPrivacyRestrictedCountry *bool `json:"in_privacy_restricted_country,omitempty"` + Subdivisions *string `json:"subdivisions,omitempty"` + Coordinates *string `json:"coordinates,omitempty"` +} + +func (o *Location) GetCode() *string { + if o == nil { + return nil + } + return o.Code +} + +func (o *Location) GetEuropeanUnionMember() *bool { + if o == nil { + return nil + } + return o.EuropeanUnionMember +} + +func (o *Location) GetContinentCode() *string { + if o == nil { + return nil + } + return o.ContinentCode +} + +func (o *Location) GetCountry() *string { + if o == nil { + return nil + } + return o.Country +} + +func (o *Location) GetCity() *string { + if o == nil { + return nil + } + return o.City +} + +func (o *Location) GetTimeZone() *string { + if o == nil { + return nil + } + return o.TimeZone +} + +func (o *Location) GetPostalCode() *float64 { + if o == nil { + return nil + } + return o.PostalCode +} + +func (o *Location) GetInPrivacyRestrictedCountry() *bool { + if o == nil { + return nil + } + return o.InPrivacyRestrictedCountry +} + +func (o *Location) GetSubdivisions() *string { + if o == nil { + return nil + } + return o.Subdivisions +} + +func (o *Location) GetCoordinates() *string { + if o == nil { + return nil + } + return o.Coordinates +} + +// GetPinResponseBody - The Pin +type GetPinResponseBody struct { + // PinID for use with authentication + ID *float64 `json:"id,omitempty"` + Code *string `json:"code,omitempty"` + Product *string `json:"product,omitempty"` + Trusted *bool `json:"trusted,omitempty"` + // a link to a QR code hosted on plex.tv + // The QR code redirects to the relevant `plex.tv/link` authentication page + // Which then prompts the user for the 4 Digit Link Pin + // + Qr *string `json:"qr,omitempty"` + ClientIdentifier *string `json:"clientIdentifier,omitempty"` + Location *Location `json:"location,omitempty"` + ExpiresIn *float64 `json:"expiresIn,omitempty"` + CreatedAt *time.Time `json:"createdAt,omitempty"` + ExpiresAt *time.Time `json:"expiresAt,omitempty"` + AuthToken *string `json:"authToken,omitempty"` + NewRegistration *string `json:"newRegistration,omitempty"` +} + +func (g GetPinResponseBody) MarshalJSON() ([]byte, error) { + return utils.MarshalJSON(g, "", false) +} + +func (g *GetPinResponseBody) UnmarshalJSON(data []byte) error { + if err := utils.UnmarshalJSON(data, &g, "", false, false); err != nil { + return err + } + return nil +} + +func (o *GetPinResponseBody) GetID() *float64 { + if o == nil { + return nil + } + return o.ID +} + +func (o *GetPinResponseBody) GetCode() *string { + if o == nil { + return nil + } + return o.Code +} + +func (o *GetPinResponseBody) GetProduct() *string { + if o == nil { + return nil + } + return o.Product +} + +func (o *GetPinResponseBody) GetTrusted() *bool { + if o == nil { + return nil + } + return o.Trusted +} + +func (o *GetPinResponseBody) GetQr() *string { + if o == nil { + return nil + } + return o.Qr +} + +func (o *GetPinResponseBody) GetClientIdentifier() *string { + if o == nil { + return nil + } + return o.ClientIdentifier +} + +func (o *GetPinResponseBody) GetLocation() *Location { + if o == nil { + return nil + } + return o.Location +} + +func (o *GetPinResponseBody) GetExpiresIn() *float64 { + if o == nil { + return nil + } + return o.ExpiresIn +} + +func (o *GetPinResponseBody) GetCreatedAt() *time.Time { + if o == nil { + return nil + } + return o.CreatedAt +} + +func (o *GetPinResponseBody) GetExpiresAt() *time.Time { + if o == nil { + return nil + } + return o.ExpiresAt +} + +func (o *GetPinResponseBody) GetAuthToken() *string { + if o == nil { + return nil + } + return o.AuthToken +} + +func (o *GetPinResponseBody) GetNewRegistration() *string { + if o == nil { + return nil + } + return o.NewRegistration +} + +type GetPinResponse struct { + // HTTP response content type for this operation + ContentType string + // HTTP response status code for this operation + StatusCode int + // Raw HTTP response; suitable for custom response parsing + RawResponse *http.Response + // The Pin + TwoHundredApplicationJSONObject *GetPinResponseBody + // X-Plex-Client-Identifier is missing + FourHundredApplicationJSONObject *GetPinPlexTvResponseBody +} + +func (o *GetPinResponse) GetContentType() string { + if o == nil { + return "" + } + return o.ContentType +} + +func (o *GetPinResponse) GetStatusCode() int { + if o == nil { + return 0 + } + return o.StatusCode +} + +func (o *GetPinResponse) GetRawResponse() *http.Response { + if o == nil { + return nil + } + return o.RawResponse +} + +func (o *GetPinResponse) GetTwoHundredApplicationJSONObject() *GetPinResponseBody { + if o == nil { + return nil + } + return o.TwoHundredApplicationJSONObject +} + +func (o *GetPinResponse) GetFourHundredApplicationJSONObject() *GetPinPlexTvResponseBody { + if o == nil { + return nil + } + return o.FourHundredApplicationJSONObject +} diff --git a/internal/sdk/pkg/models/operations/gettoken.go b/internal/sdk/pkg/models/operations/gettoken.go new file mode 100644 index 0000000..1d938f9 --- /dev/null +++ b/internal/sdk/pkg/models/operations/gettoken.go @@ -0,0 +1,110 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package operations + +import ( + "net/http" +) + +var GetTokenServerList = []string{ + "https://plex.tv/api/v2", +} + +type GetTokenRequest struct { + // The PinID to retrieve an access token for + PinID string `pathParam:"style=simple,explode=false,name=pinID"` + // Plex Authentication Token + XPlexClientIdentifier string `header:"style=simple,explode=false,name=X-Plex-Client-Identifier"` +} + +func (o *GetTokenRequest) GetPinID() string { + if o == nil { + return "" + } + return o.PinID +} + +func (o *GetTokenRequest) GetXPlexClientIdentifier() string { + if o == nil { + return "" + } + return o.XPlexClientIdentifier +} + +type GetTokenErrors struct { + Code *float64 `json:"code,omitempty"` + Message *string `json:"message,omitempty"` + Status *float64 `json:"status,omitempty"` +} + +func (o *GetTokenErrors) GetCode() *float64 { + if o == nil { + return nil + } + return o.Code +} + +func (o *GetTokenErrors) GetMessage() *string { + if o == nil { + return nil + } + return o.Message +} + +func (o *GetTokenErrors) GetStatus() *float64 { + if o == nil { + return nil + } + return o.Status +} + +// GetTokenResponseBody - X-Plex-Client-Identifier is missing +type GetTokenResponseBody struct { + Errors []GetTokenErrors `json:"errors,omitempty"` +} + +func (o *GetTokenResponseBody) GetErrors() []GetTokenErrors { + if o == nil { + return nil + } + return o.Errors +} + +type GetTokenResponse struct { + // HTTP response content type for this operation + ContentType string + // HTTP response status code for this operation + StatusCode int + // Raw HTTP response; suitable for custom response parsing + RawResponse *http.Response + // X-Plex-Client-Identifier is missing + Object *GetTokenResponseBody +} + +func (o *GetTokenResponse) GetContentType() string { + if o == nil { + return "" + } + return o.ContentType +} + +func (o *GetTokenResponse) GetStatusCode() int { + if o == nil { + return 0 + } + return o.StatusCode +} + +func (o *GetTokenResponse) GetRawResponse() *http.Response { + if o == nil { + return nil + } + return o.RawResponse +} + +func (o *GetTokenResponse) GetObject() *GetTokenResponseBody { + if o == nil { + return nil + } + return o.Object +} diff --git a/internal/sdk/pkg/models/operations/options.go b/internal/sdk/pkg/models/operations/options.go new file mode 100644 index 0000000..7089b05 --- /dev/null +++ b/internal/sdk/pkg/models/operations/options.go @@ -0,0 +1,63 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package operations + +import ( + "errors" + "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/utils" +) + +var ErrUnsupportedOption = errors.New("unsupported option") + +const ( + SupportedOptionServerURL = "serverURL" + SupportedOptionRetries = "retries" + SupportedOptionAcceptHeaderOverride = "acceptHeaderOverride" +) + +type Options struct { + ServerURL *string + Retries *utils.RetryConfig +} + +type Option func(*Options, ...string) error + +// WithServerURL allows providing an alternative server URL. +func WithServerURL(serverURL string) Option { + return func(opts *Options, supportedOptions ...string) error { + if !utils.Contains(supportedOptions, SupportedOptionServerURL) { + return ErrUnsupportedOption + } + + opts.ServerURL = &serverURL + return nil + } +} + +// WithTemplatedServerURL allows providing an alternative server URL with templated parameters. +func WithTemplatedServerURL(serverURL string, params map[string]string) Option { + return func(opts *Options, supportedOptions ...string) error { + if !utils.Contains(supportedOptions, SupportedOptionServerURL) { + return ErrUnsupportedOption + } + + if params != nil { + serverURL = utils.ReplaceParameters(serverURL, params) + } + + opts.ServerURL = &serverURL + return nil + } +} + +// WithRetries allows customizing the default retry configuration. +func WithRetries(config utils.RetryConfig) Option { + return func(opts *Options, supportedOptions ...string) error { + if !utils.Contains(supportedOptions, SupportedOptionRetries) { + return ErrUnsupportedOption + } + + opts.Retries = &config + return nil + } +} diff --git a/internal/sdk/plex.go b/internal/sdk/plex.go new file mode 100644 index 0000000..a8de7ef --- /dev/null +++ b/internal/sdk/plex.go @@ -0,0 +1,16 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package sdk + +type Plex struct { + Tv *Tv + + sdkConfiguration sdkConfiguration +} + +func newPlex(sdkConfig sdkConfiguration) *Plex { + return &Plex{ + sdkConfiguration: sdkConfig, + Tv: newTv(sdkConfig), + } +} diff --git a/internal/sdk/sdk.go b/internal/sdk/sdk.go index b665a4b..b708f5c 100644 --- a/internal/sdk/sdk.go +++ b/internal/sdk/sdk.go @@ -96,7 +96,8 @@ type PlexAPI struct { Library *Library // Submit logs to the Log Handler for Plex Media Server // - Log *Log + Log *Log + Plex *Plex // 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. @@ -259,9 +260,9 @@ func New(opts ...SDKOption) *PlexAPI { sdkConfiguration: sdkConfiguration{ Language: "go", OpenAPIDocVersion: "0.0.3", - SDKVersion: "0.3.3", - GenVersion: "2.237.3", - UserAgent: "speakeasy-sdk/go 0.3.3 2.237.3 0.0.3 PlexAPI", + SDKVersion: "0.4.0", + GenVersion: "2.239.4", + UserAgent: "speakeasy-sdk/go 0.4.0 2.239.4 0.0.3 PlexAPI", ServerDefaults: []map[string]string{ { "protocol": "http", @@ -303,6 +304,8 @@ func New(opts ...SDKOption) *PlexAPI { sdk.Log = newLog(sdk.sdkConfiguration) + sdk.Plex = newPlex(sdk.sdkConfiguration) + sdk.Playlists = newPlaylists(sdk.sdkConfiguration) sdk.Security = newSecurity(sdk.sdkConfiguration) diff --git a/internal/sdk/tv.go b/internal/sdk/tv.go new file mode 100644 index 0000000..a616b5f --- /dev/null +++ b/internal/sdk/tv.go @@ -0,0 +1,187 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package sdk + +import ( + "bytes" + "context" + "fmt" + "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/models/operations" + "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/models/sdkerrors" + "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/utils" + "io" + "net/http" + "strings" +) + +type Tv struct { + sdkConfiguration sdkConfiguration +} + +func newTv(sdkConfig sdkConfiguration) *Tv { + return &Tv{ + sdkConfiguration: sdkConfig, + } +} + +// GetPin - Get a Pin +// Retrieve a Pin from Plex.tv for authentication flows +func (s *Tv) GetPin(ctx context.Context, request operations.GetPinRequest, opts ...operations.Option) (*operations.GetPinResponse, error) { + o := operations.Options{} + supportedOptions := []string{ + operations.SupportedOptionServerURL, + } + + for _, opt := range opts { + if err := opt(&o, supportedOptions...); err != nil { + return nil, fmt.Errorf("error applying option: %w", err) + } + } + baseURL := utils.ReplaceParameters(operations.GetPinServerList[0], map[string]string{}) + if o.ServerURL != nil { + baseURL = *o.ServerURL + } + + url := strings.TrimSuffix(baseURL, "/") + "/pins" + + req, err := http.NewRequestWithContext(ctx, "POST", 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) + + utils.PopulateHeaders(ctx, req, request) + + 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") + } + + 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)) + + contentType := httpRes.Header.Get("Content-Type") + + res := &operations.GetPinResponse{ + StatusCode: httpRes.StatusCode, + ContentType: contentType, + RawResponse: httpRes, + } + switch { + case httpRes.StatusCode == 200: + switch { + case utils.MatchContentType(contentType, `application/json`): + var out operations.GetPinResponseBody + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + res.TwoHundredApplicationJSONObject = &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 400: + switch { + case utils.MatchContentType(contentType, `application/json`): + var out operations.GetPinPlexTvResponseBody + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + res.FourHundredApplicationJSONObject = &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) + } + } + + return res, nil +} + +// GetToken - Get Access Token +// Retrieve an Access Token from Plex.tv after the Pin has already been authenticated +func (s *Tv) GetToken(ctx context.Context, request operations.GetTokenRequest, opts ...operations.Option) (*operations.GetTokenResponse, error) { + o := operations.Options{} + supportedOptions := []string{ + operations.SupportedOptionServerURL, + } + + for _, opt := range opts { + if err := opt(&o, supportedOptions...); err != nil { + return nil, fmt.Errorf("error applying option: %w", err) + } + } + baseURL := utils.ReplaceParameters(operations.GetTokenServerList[0], map[string]string{}) + if o.ServerURL != nil { + baseURL = *o.ServerURL + } + + url, err := utils.GenerateURL(ctx, baseURL, "/pins/{pinID}", 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) + + utils.PopulateHeaders(ctx, req, request) + + 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") + } + + 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)) + + contentType := httpRes.Header.Get("Content-Type") + + res := &operations.GetTokenResponse{ + StatusCode: httpRes.StatusCode, + ContentType: contentType, + RawResponse: httpRes, + } + switch { + case httpRes.StatusCode == 200: + case httpRes.StatusCode == 400: + switch { + case utils.MatchContentType(contentType, `application/json`): + var out operations.GetTokenResponseBody + 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) + } + } + + return res, nil +}