Compare commits

..

2 Commits

Author SHA1 Message Date
speakeasybot
da5491e935 ci: regenerated with OpenAPI Doc , Speakeasy CLI 1.438.1 2024-11-12 00:10:16 +00:00
speakeasybot
aeebf438e1 ci: regenerated with OpenAPI Doc , Speakeasy CLI 1.406.0 2024-10-04 00:10:38 +00:00
42 changed files with 1053 additions and 2636 deletions

View File

@@ -1,12 +1,12 @@
lockVersion: 2.0.0 lockVersion: 2.0.0
id: dfa99515-01c0-42eb-9be5-ee212fd03eb3 id: dfa99515-01c0-42eb-9be5-ee212fd03eb3
management: management:
docChecksum: 1ee2e5413af07e886e33659b9b269dd5 docChecksum: af45c1ae70f7fc6589adb3f8075cb275
docVersion: 0.0.3 docVersion: 0.0.3
speakeasyVersion: 1.406.0 speakeasyVersion: 1.438.1
generationVersion: 2.429.0 generationVersion: 2.457.2
releaseVersion: 0.15.0 releaseVersion: 0.16.0
configChecksum: a2c4d09241a2acac8ca19d8c9ee8f2bc configChecksum: 6cf4f4d25c4a33acae96bbafd2c74d9f
repoURL: https://github.com/LukeHagar/plexgo.git repoURL: https://github.com/LukeHagar/plexgo.git
repoSubDirectory: . repoSubDirectory: .
installationURL: https://github.com/LukeHagar/plexgo installationURL: https://github.com/LukeHagar/plexgo
@@ -15,13 +15,13 @@ features:
go: go:
additionalDependencies: 0.1.0 additionalDependencies: 0.1.0
constsAndDefaults: 0.1.6 constsAndDefaults: 0.1.6
core: 3.5.12 core: 3.5.15
defaultEnabledRetries: 0.2.0 defaultEnabledRetries: 0.2.0
deprecations: 2.81.1 deprecations: 2.81.1
downloadStreams: 0.1.2 downloadStreams: 0.1.2
enums: 2.81.1 enums: 2.81.1
envVarSecurityUsage: 0.3.1 envVarSecurityUsage: 0.3.2
errors: 2.81.10 errors: 2.83.0
flattening: 2.81.1 flattening: 2.81.1
globalSecurity: 2.82.10 globalSecurity: 2.82.10
globalSecurityCallbacks: 0.1.0 globalSecurityCallbacks: 0.1.0
@@ -35,6 +35,7 @@ features:
responseFormat: 0.1.2 responseFormat: 0.1.2
retries: 2.83.0 retries: 2.83.0
sdkHooks: 0.1.0 sdkHooks: 0.1.0
tests: 0.6.0
generatedFiles: generatedFiles:
- .gitattributes - .gitattributes
- /models/components/security.go - /models/components/security.go
@@ -2065,7 +2066,7 @@ examples:
speakeasy-default-get-token-details: speakeasy-default-get-token-details:
responses: responses:
"200": "200":
application/json: {"adsConsent": true, "adsConsentReminderAt": "2019-08-24T14:15:22Z", "adsConsentSetAt": "2019-08-24T14:15:22Z", "anonymous": false, "authToken": "CxoUzBTSV5hsxjTpFKaf", "backupCodesCreated": false, "confirmed": false, "country": "US", "email": "username@email.com", "emailOnlyAuth": false, "experimentalFeatures": false, "friendlyName": "friendlyUsername", "entitlements": ["[]"], "guest": false, "hasPassword": true, "home": false, "homeAdmin": false, "homeSize": 1, "id": 13692262, "joinedAt": 1556281940, "locale": null, "mailingListActive": false, "mailingListStatus": "active", "maxHomeSize": 15, "pin": "string", "profile": {"autoSelectAudio": true, "defaultAudioLanguage": "ja", "defaultSubtitleLanguage": "en", "autoSelectSubtitle": 1, "defaultSubtitleAccessibility": 1, "defaultSubtitleForced": 1, "watchedIndicator": 1, "mediaReviewsVisibility": 1}, "protected": false, "rememberExpiresAt": 1556281940, "restricted": false, "roles": ["string"], "scrobbleTypes": "", "services": [{"identifier": "metadata-dev", "endpoint": "https://epg.provider.plex.tv", "token": "DjoMtqFAGRL1uVtCyF1dKIorTbShJeqv", "secret": "string", "status": "online"}], "subscription": {"features": ["Android - Dolby Vision"], "active": true, "subscribedAt": "2021-04-12T18:21:12Z", "status": "Inactive", "paymentService": "string", "plan": "string"}, "subscriptionDescription": "string", "subscriptions": [{"features": ["Android - Dolby Vision"], "active": true, "subscribedAt": "2021-04-12T18:21:12Z", "status": "Inactive", "paymentService": "string", "plan": "string"}], "thumb": "https://plex.tv/users/a4f43c1ebfde43a5/avatar?c=8372075101", "title": "UsernameTitle", "twoFactorEnabled": false, "username": "Username", "uuid": "dae343c1f45beb4f", "attributionPartner": null} application/json: {"adsConsent": true, "adsConsentReminderAt": 1556281940, "adsConsentSetAt": 1556281940, "anonymous": false, "authToken": "CxoUzBTSV5hsxjTpFKaf", "backupCodesCreated": false, "confirmed": false, "country": "US", "email": "username@email.com", "emailOnlyAuth": false, "experimentalFeatures": false, "friendlyName": "friendlyUsername", "entitlements": ["[]"], "guest": false, "hasPassword": true, "home": false, "homeAdmin": false, "homeSize": 1, "id": 13692262, "joinedAt": 1556281940, "locale": null, "mailingListActive": false, "mailingListStatus": "active", "maxHomeSize": 15, "pin": "string", "profile": {"autoSelectAudio": true, "defaultAudioLanguage": "ja", "defaultSubtitleLanguage": "en", "autoSelectSubtitle": 1, "defaultSubtitleAccessibility": 1, "defaultSubtitleForced": 1, "watchedIndicator": 1, "mediaReviewsVisibility": 1}, "protected": false, "rememberExpiresAt": 1556281940, "restricted": false, "roles": ["string"], "scrobbleTypes": "", "services": [{"identifier": "metadata-dev", "endpoint": "https://epg.provider.plex.tv", "token": "DjoMtqFAGRL1uVtCyF1dKIorTbShJeqv", "secret": "string", "status": "online"}], "subscription": {"features": ["Android - Dolby Vision"], "active": true, "subscribedAt": "2021-04-12T18:21:12Z", "status": "Inactive", "paymentService": "string", "plan": "string"}, "subscriptionDescription": "string", "subscriptions": [{"features": ["Android - Dolby Vision"], "active": true, "subscribedAt": "2021-04-12T18:21:12Z", "status": "Inactive", "paymentService": "string", "plan": "string"}], "thumb": "https://plex.tv/users/a4f43c1ebfde43a5/avatar?c=8372075101", "title": "UsernameTitle", "twoFactorEnabled": false, "username": "Username", "uuid": "dae343c1f45beb4f", "attributionPartner": null}
"400": "400":
application/json: {"errors": []} application/json: {"errors": []}
"401": "401":

View File

@@ -11,11 +11,13 @@ generation:
requestResponseComponentNamesFeb2024: true requestResponseComponentNamesFeb2024: true
auth: auth:
oAuth2ClientCredentialsEnabled: true oAuth2ClientCredentialsEnabled: true
oAuth2PasswordEnabled: false
go: go:
version: 0.15.0 version: 0.16.0
additionalDependencies: {} additionalDependencies: {}
allowUnknownFieldsInWeakUnions: false allowUnknownFieldsInWeakUnions: false
clientServerStatusCodesAsErrors: true clientServerStatusCodesAsErrors: true
defaultErrorName: SDKError
flattenGlobalSecurity: true flattenGlobalSecurity: true
imports: imports:
option: openapi option: openapi

View File

@@ -1,4 +1,4 @@
speakeasyVersion: 1.406.0 speakeasyVersion: 1.438.1
sources: sources:
my-source: my-source:
sourceNamespace: my-source sourceNamespace: my-source
@@ -9,8 +9,8 @@ sources:
- main - main
plexapi: plexapi:
sourceNamespace: plexapi sourceNamespace: plexapi
sourceRevisionDigest: sha256:c6ab8f13847c7cdc4ab3752f517164d7da08266726255aa0d614b1eaea66fb0e sourceRevisionDigest: sha256:15e040d800921ded49cf84650ef2bc8fb1acf32c885ee193c42e838d754bf345
sourceBlobDigest: sha256:64c7694915a0828aeb256d10d2fa0ea477681785b818916b6681be8cc5ffb93b sourceBlobDigest: sha256:c178be2b4bfefb37d76c0fdaef37c51f9b6ab1410422d3698a41bb6a8d79a79f
tags: tags:
- latest - latest
- main - main
@@ -18,10 +18,10 @@ targets:
plexgo: plexgo:
source: plexapi source: plexapi
sourceNamespace: plexapi sourceNamespace: plexapi
sourceRevisionDigest: sha256:c6ab8f13847c7cdc4ab3752f517164d7da08266726255aa0d614b1eaea66fb0e sourceRevisionDigest: sha256:15e040d800921ded49cf84650ef2bc8fb1acf32c885ee193c42e838d754bf345
sourceBlobDigest: sha256:64c7694915a0828aeb256d10d2fa0ea477681785b818916b6681be8cc5ffb93b sourceBlobDigest: sha256:c178be2b4bfefb37d76c0fdaef37c51f9b6ab1410422d3698a41bb6a8d79a79f
codeSamplesNamespace: code-samples-go-plexgo codeSamplesNamespace: code-samples-go-plexgo
codeSamplesRevisionDigest: sha256:9ae03dbe12a6438ac7667de48bf864bbeb4b470bd87b38555484879a998889ba codeSamplesRevisionDigest: sha256:7a02eff4211d44db5a95061e3af9e7151629b6539d0898b2f9aa2f9785268505
workflow: workflow:
workflowVersion: 1.0.0 workflowVersion: 1.0.0
speakeasyVersion: latest speakeasyVersion: latest

View File

@@ -336,7 +336,7 @@ By Default, an API error will return `sdkerrors.SDKError`. When custom error res
For example, the `GetServerCapabilities` function may return the following errors: For example, the `GetServerCapabilities` function may return the following errors:
| Error Type | Status Code | Content Type | | Error Type | Status Code | Content Type |
| ------------------------------------------- | ------------------------------------------- | ------------------------------------------- | | ------------------------------------------- | ----------- | ---------------- |
| sdkerrors.GetServerCapabilitiesBadRequest | 400 | application/json | | sdkerrors.GetServerCapabilitiesBadRequest | 400 | application/json |
| sdkerrors.GetServerCapabilitiesUnauthorized | 401 | application/json | | sdkerrors.GetServerCapabilitiesUnauthorized | 401 | application/json |
| sdkerrors.SDKError | 4XX, 5XX | \*/\* | | sdkerrors.SDKError | 4XX, 5XX | \*/\* |
@@ -394,58 +394,16 @@ func main() {
<!-- Start Server Selection [server] --> <!-- Start Server Selection [server] -->
## Server Selection ## Server Selection
### Select Server by Index ### Server Variables
You can override the default server globally using the `WithServerIndex` option when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers: The default server `{protocol}://{ip}:{port}` contains variables and is set to `https://10.10.10.47:32400` by default. To override default values, the following options are available when initializing the SDK client instance:
* `WithProtocol(protocol ServerProtocol)`
| # | Server | Variables | * `WithIP(ip string)`
| - | ------ | --------- | * `WithPort(port string)`
| 0 | `{protocol}://{ip}:{port}` | `protocol` (default is `https`), `ip` (default is `10.10.10.47`), `port` (default is `32400`) |
#### Example
```go
package main
import (
"context"
"github.com/LukeHagar/plexgo"
"log"
)
func main() {
s := plexgo.New(
plexgo.WithServerIndex(0),
plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"),
plexgo.WithClientName("Plex for Roku"),
plexgo.WithClientVersion("2.4.1"),
plexgo.WithPlatform("Roku"),
plexgo.WithDeviceNickname("Roku 3"),
)
ctx := context.Background()
res, err := s.Server.GetServerCapabilities(ctx)
if err != nil {
log.Fatal(err)
}
if res.Object != nil {
// handle response
}
}
```
#### Variables
Some of the server options above contain variables. If you want to set the values of those variables, the following options are provided for doing so:
* `WithProtocol plexgo.ServerProtocol`
* `WithIP string`
* `WithPort string`
### Override Server URL Per-Client ### Override Server URL Per-Client
The default server can also be overridden globally using the `WithServerURL` option when initializing the SDK client instance. For example: The default server can also be overridden globally using the `WithServerURL(serverURL string)` option when initializing the SDK client instance. For example:
```go ```go
package main package main
@@ -457,7 +415,7 @@ import (
func main() { func main() {
s := plexgo.New( s := plexgo.New(
plexgo.WithServerURL("{protocol}://{ip}:{port}"), plexgo.WithServerURL("https://10.10.10.47:32400"),
plexgo.WithSecurity("<YOUR_API_KEY_HERE>"), plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"), plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"),
plexgo.WithClientName("Plex for Roku"), plexgo.WithClientName("Plex for Roku"),
@@ -551,7 +509,7 @@ This can be a convenient way to configure timeouts, cookies, proxies, custom hea
This SDK supports the following security scheme globally: This SDK supports the following security scheme globally:
| Name | Type | Scheme | | Name | Type | Scheme |
| ------------- | ------------- | ------------- | | ------------- | ------ | ------- |
| `AccessToken` | apiKey | API key | | `AccessToken` | apiKey | API key |
You can configure it using the `WithSecurity` option when initializing the SDK client instance. For example: You can configure it using the `WithSecurity` option when initializing the SDK client instance. For example:

View File

@@ -943,3 +943,23 @@ Based on:
- [go v0.15.0] . - [go v0.15.0] .
### Releases ### Releases
- [Go v0.15.0] https://github.com/LukeHagar/plexgo/releases/tag/v0.15.0 - . - [Go v0.15.0] https://github.com/LukeHagar/plexgo/releases/tag/v0.15.0 - .
## 2024-10-04 00:09:04
### Changes
Based on:
- OpenAPI Doc
- Speakeasy CLI 1.406.0 (2.429.0) https://github.com/speakeasy-api/speakeasy
### Generated
- [go v0.15.1] .
### Releases
- [Go v0.15.1] https://github.com/LukeHagar/plexgo/releases/tag/v0.15.1 - .
## 2024-11-12 00:08:55
### Changes
Based on:
- OpenAPI Doc
- Speakeasy CLI 1.438.1 (2.457.2) https://github.com/speakeasy-api/speakeasy
### Generated
- [go v0.16.0] .
### Releases
- [Go v0.16.0] https://github.com/LukeHagar/plexgo/releases/tag/v0.16.0 - .

View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -89,16 +87,6 @@ func (s *Activities) GetServerActivities(ctx context.Context, opts ...operations
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -107,7 +95,11 @@ func (s *Activities) GetServerActivities(ctx context.Context, opts ...operations
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -181,21 +173,11 @@ func (s *Activities) GetServerActivities(ctx context.Context, opts ...operations
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -207,17 +189,16 @@ func (s *Activities) GetServerActivities(ctx context.Context, opts ...operations
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -230,17 +211,16 @@ func (s *Activities) GetServerActivities(ctx context.Context, opts ...operations
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -253,28 +233,25 @@ func (s *Activities) GetServerActivities(ctx context.Context, opts ...operations
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -341,16 +318,6 @@ func (s *Activities) CancelServerActivities(ctx context.Context, activityUUID st
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -359,7 +326,11 @@ func (s *Activities) CancelServerActivities(ctx context.Context, activityUUID st
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -433,22 +404,12 @@ func (s *Activities) CancelServerActivities(ctx context.Context, activityUUID st
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -461,17 +422,16 @@ func (s *Activities) CancelServerActivities(ctx context.Context, activityUUID st
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -484,28 +444,25 @@ func (s *Activities) CancelServerActivities(ctx context.Context, activityUUID st
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -92,16 +90,6 @@ func (s *Authentication) GetTransientToken(ctx context.Context, type_ operations
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -110,7 +98,11 @@ func (s *Authentication) GetTransientToken(ctx context.Context, type_ operations
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -184,22 +176,12 @@ func (s *Authentication) GetTransientToken(ctx context.Context, type_ operations
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -212,17 +194,16 @@ func (s *Authentication) GetTransientToken(ctx context.Context, type_ operations
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -235,28 +216,25 @@ func (s *Authentication) GetTransientToken(ctx context.Context, type_ operations
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -328,16 +306,6 @@ func (s *Authentication) GetSourceConnectionInformation(ctx context.Context, sou
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -346,7 +314,11 @@ func (s *Authentication) GetSourceConnectionInformation(ctx context.Context, sou
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -420,22 +392,12 @@ func (s *Authentication) GetSourceConnectionInformation(ctx context.Context, sou
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -448,17 +410,16 @@ func (s *Authentication) GetSourceConnectionInformation(ctx context.Context, sou
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -471,28 +432,25 @@ func (s *Authentication) GetSourceConnectionInformation(ctx context.Context, sou
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -560,16 +518,6 @@ func (s *Authentication) GetTokenDetails(ctx context.Context, opts ...operations
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -578,7 +526,11 @@ func (s *Authentication) GetTokenDetails(ctx context.Context, opts ...operations
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -652,21 +604,11 @@ func (s *Authentication) GetTokenDetails(ctx context.Context, opts ...operations
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -678,17 +620,16 @@ func (s *Authentication) GetTokenDetails(ctx context.Context, opts ...operations
res.UserPlexAccount = &out res.UserPlexAccount = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -701,17 +642,16 @@ func (s *Authentication) GetTokenDetails(ctx context.Context, opts ...operations
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -724,28 +664,25 @@ func (s *Authentication) GetTokenDetails(ctx context.Context, opts ...operations
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -825,16 +762,6 @@ func (s *Authentication) PostUsersSignInData(ctx context.Context, request operat
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -843,7 +770,11 @@ func (s *Authentication) PostUsersSignInData(ctx context.Context, request operat
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -917,21 +848,11 @@ func (s *Authentication) PostUsersSignInData(ctx context.Context, request operat
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 201: case httpRes.StatusCode == 201:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -943,17 +864,16 @@ func (s *Authentication) PostUsersSignInData(ctx context.Context, request operat
res.UserPlexAccount = &out res.UserPlexAccount = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -966,17 +886,16 @@ func (s *Authentication) PostUsersSignInData(ctx context.Context, request operat
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -989,28 +908,25 @@ func (s *Authentication) PostUsersSignInData(ctx context.Context, request operat
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

217
butler.go
View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -83,16 +81,6 @@ func (s *Butler) GetButlerTasks(ctx context.Context, opts ...operations.Option)
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -101,7 +89,11 @@ func (s *Butler) GetButlerTasks(ctx context.Context, opts ...operations.Option)
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -175,21 +167,11 @@ func (s *Butler) GetButlerTasks(ctx context.Context, opts ...operations.Option)
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -201,17 +183,16 @@ func (s *Butler) GetButlerTasks(ctx context.Context, opts ...operations.Option)
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -224,17 +205,16 @@ func (s *Butler) GetButlerTasks(ctx context.Context, opts ...operations.Option)
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -247,28 +227,25 @@ func (s *Butler) GetButlerTasks(ctx context.Context, opts ...operations.Option)
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -335,16 +312,6 @@ func (s *Butler) StartAllTasks(ctx context.Context, opts ...operations.Option) (
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -353,7 +320,11 @@ func (s *Butler) StartAllTasks(ctx context.Context, opts ...operations.Option) (
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -427,22 +398,12 @@ func (s *Butler) StartAllTasks(ctx context.Context, opts ...operations.Option) (
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -455,17 +416,16 @@ func (s *Butler) StartAllTasks(ctx context.Context, opts ...operations.Option) (
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -478,28 +438,25 @@ func (s *Butler) StartAllTasks(ctx context.Context, opts ...operations.Option) (
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -562,16 +519,6 @@ func (s *Butler) StopAllTasks(ctx context.Context, opts ...operations.Option) (*
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -580,7 +527,11 @@ func (s *Butler) StopAllTasks(ctx context.Context, opts ...operations.Option) (*
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -654,22 +605,12 @@ func (s *Butler) StopAllTasks(ctx context.Context, opts ...operations.Option) (*
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -682,17 +623,16 @@ func (s *Butler) StopAllTasks(ctx context.Context, opts ...operations.Option) (*
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -705,28 +645,25 @@ func (s *Butler) StopAllTasks(ctx context.Context, opts ...operations.Option) (*
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -797,16 +734,6 @@ func (s *Butler) StartTask(ctx context.Context, taskName operations.TaskName, op
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -815,7 +742,11 @@ func (s *Butler) StartTask(ctx context.Context, taskName operations.TaskName, op
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -889,16 +820,6 @@ func (s *Butler) StartTask(ctx context.Context, taskName operations.TaskName, op
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
fallthrough fallthrough
@@ -906,7 +827,7 @@ func (s *Butler) StartTask(ctx context.Context, taskName operations.TaskName, op
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -919,17 +840,16 @@ func (s *Butler) StartTask(ctx context.Context, taskName operations.TaskName, op
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -942,28 +862,25 @@ func (s *Butler) StartTask(ctx context.Context, taskName operations.TaskName, op
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -1030,16 +947,6 @@ func (s *Butler) StopTask(ctx context.Context, taskName operations.PathParamTask
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -1048,7 +955,11 @@ func (s *Butler) StopTask(ctx context.Context, taskName operations.PathParamTask
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -1122,22 +1033,12 @@ func (s *Butler) StopTask(ctx context.Context, taskName operations.PathParamTask
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1150,17 +1051,16 @@ func (s *Butler) StopTask(ctx context.Context, taskName operations.PathParamTask
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1173,11 +1073,10 @@ func (s *Butler) StopTask(ctx context.Context, taskName operations.PathParamTask
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
} }
case httpRes.StatusCode == 404: case httpRes.StatusCode == 404:
@@ -1185,18 +1084,16 @@ func (s *Butler) StopTask(ctx context.Context, taskName operations.PathParamTask
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

View File

@@ -29,5 +29,5 @@ The Meta object is only included in the response if the `includeMeta` parameter
| `ViewGroup` | *string* | :heavy_check_mark: | N/A | movie | | `ViewGroup` | *string* | :heavy_check_mark: | N/A | movie |
| `ViewMode` | **int* | :heavy_minus_sign: | N/A | 65592 | | `ViewMode` | **int* | :heavy_minus_sign: | N/A | 65592 |
| `MixedParents` | **bool* | :heavy_minus_sign: | N/A | true | | `MixedParents` | **bool* | :heavy_minus_sign: | N/A | true |
| `Metadata` | [][operations.GetLibraryItemsMetadata](../../models/operations/getlibraryitemsmetadata.md) | :heavy_check_mark: | N/A | | | `Metadata` | [][operations.GetLibraryItemsMetadata](../../models/operations/getlibraryitemsmetadata.md) | :heavy_minus_sign: | N/A | |
| `Meta` | [*operations.GetLibraryItemsMeta](../../models/operations/getlibraryitemsmeta.md) | :heavy_minus_sign: | The Meta object is only included in the response if the `includeMeta` parameter is set to `1`.<br/> | | | `Meta` | [*operations.GetLibraryItemsMeta](../../models/operations/getlibraryitemsmeta.md) | :heavy_minus_sign: | The Meta object is only included in the response if the `includeMeta` parameter is set to `1`.<br/> | |

View File

@@ -17,3 +17,4 @@ E.g. A movie library will not return anything with type 3 as there are no season
| `GetLibraryItemsQueryParamTypeTvShow` | 2 | | `GetLibraryItemsQueryParamTypeTvShow` | 2 |
| `GetLibraryItemsQueryParamTypeSeason` | 3 | | `GetLibraryItemsQueryParamTypeSeason` | 3 |
| `GetLibraryItemsQueryParamTypeEpisode` | 4 | | `GetLibraryItemsQueryParamTypeEpisode` | 4 |
| `GetLibraryItemsQueryParamTypeAudio` | 8 |

View File

@@ -17,3 +17,4 @@ E.g. A movie library will not return anything with type 3 as there are no season
| `GetPlaylistContentsQueryParamTypeTvShow` | 2 | | `GetPlaylistContentsQueryParamTypeTvShow` | 2 |
| `GetPlaylistContentsQueryParamTypeSeason` | 3 | | `GetPlaylistContentsQueryParamTypeSeason` | 3 |
| `GetPlaylistContentsQueryParamTypeEpisode` | 4 | | `GetPlaylistContentsQueryParamTypeEpisode` | 4 |
| `GetPlaylistContentsQueryParamTypeAudio` | 8 |

View File

@@ -17,3 +17,4 @@ E.g. A movie library will not return anything with type 3 as there are no season
| `GetSearchLibraryQueryParamTypeTvShow` | 2 | | `GetSearchLibraryQueryParamTypeTvShow` | 2 |
| `GetSearchLibraryQueryParamTypeSeason` | 3 | | `GetSearchLibraryQueryParamTypeSeason` | 3 |
| `GetSearchLibraryQueryParamTypeEpisode` | 4 | | `GetSearchLibraryQueryParamTypeEpisode` | 4 |
| `GetSearchLibraryQueryParamTypeAudio` | 8 |

File diff suppressed because one or more lines are too long

View File

@@ -8,8 +8,8 @@ Logged in user details
| Field | Type | Required | Description | Example | | Field | Type | Required | Description | Example |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AdsConsent` | *bool* | :heavy_check_mark: | Unknown | | | `AdsConsent` | *bool* | :heavy_check_mark: | Unknown | |
| `AdsConsentReminderAt` | [time.Time](https://pkg.go.dev/time#Time) | :heavy_check_mark: | Unknown | | | `AdsConsentReminderAt` | *int64* | :heavy_check_mark: | N/A | 1556281940 |
| `AdsConsentSetAt` | [time.Time](https://pkg.go.dev/time#Time) | :heavy_check_mark: | Unknown | | | `AdsConsentSetAt` | *int64* | :heavy_check_mark: | N/A | 1556281940 |
| `Anonymous` | **bool* | :heavy_minus_sign: | Unknown | | | `Anonymous` | **bool* | :heavy_minus_sign: | Unknown | |
| `AuthToken` | *string* | :heavy_check_mark: | The account token | CxoUzBTSV5hsxjTpFKaf | | `AuthToken` | *string* | :heavy_check_mark: | The account token | CxoUzBTSV5hsxjTpFKaf |
| `BackupCodesCreated` | **bool* | :heavy_minus_sign: | If the two-factor authentication backup codes have been created | | | `BackupCodesCreated` | **bool* | :heavy_minus_sign: | If the two-factor authentication backup codes have been created | |

View File

@@ -17,3 +17,4 @@ E.g. A movie library will not return anything with type 3 as there are no season
| `GetTopWatchedContentQueryParamTypeTvShow` | 2 | | `GetTopWatchedContentQueryParamTypeTvShow` | 2 |
| `GetTopWatchedContentQueryParamTypeSeason` | 3 | | `GetTopWatchedContentQueryParamTypeSeason` | 3 |
| `GetTopWatchedContentQueryParamTypeEpisode` | 4 | | `GetTopWatchedContentQueryParamTypeEpisode` | 4 |
| `GetTopWatchedContentQueryParamTypeAudio` | 8 |

File diff suppressed because one or more lines are too long

View File

@@ -8,8 +8,8 @@ Returns the user account data with a valid auth token
| Field | Type | Required | Description | Example | | Field | Type | Required | Description | Example |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AdsConsent` | *bool* | :heavy_check_mark: | Unknown | | | `AdsConsent` | *bool* | :heavy_check_mark: | Unknown | |
| `AdsConsentReminderAt` | [time.Time](https://pkg.go.dev/time#Time) | :heavy_check_mark: | Unknown | | | `AdsConsentReminderAt` | *int64* | :heavy_check_mark: | N/A | 1556281940 |
| `AdsConsentSetAt` | [time.Time](https://pkg.go.dev/time#Time) | :heavy_check_mark: | Unknown | | | `AdsConsentSetAt` | *int64* | :heavy_check_mark: | N/A | 1556281940 |
| `Anonymous` | **bool* | :heavy_minus_sign: | Unknown | | | `Anonymous` | **bool* | :heavy_minus_sign: | Unknown | |
| `AuthToken` | *string* | :heavy_check_mark: | The account token | CxoUzBTSV5hsxjTpFKaf | | `AuthToken` | *string* | :heavy_check_mark: | The account token | CxoUzBTSV5hsxjTpFKaf |
| `BackupCodesCreated` | **bool* | :heavy_minus_sign: | If the two-factor authentication backup codes have been created | | | `BackupCodesCreated` | **bool* | :heavy_minus_sign: | If the two-factor authentication backup codes have been created | |

View File

@@ -17,3 +17,4 @@ E.g. A movie library will not return anything with type 3 as there are no season
| `QueryParamTypeTvShow` | 2 | | `QueryParamTypeTvShow` | 2 |
| `QueryParamTypeSeason` | 3 | | `QueryParamTypeSeason` | 3 |
| `QueryParamTypeEpisode` | 4 | | `QueryParamTypeEpisode` | 4 |
| `QueryParamTypeAudio` | 8 |

View File

@@ -17,3 +17,4 @@ E.g. A movie library will not return anything with type 3 as there are no season
| `TypeTvShow` | 2 | | `TypeTvShow` | 2 |
| `TypeSeason` | 3 | | `TypeSeason` | 3 |
| `TypeEpisode` | 4 | | `TypeEpisode` | 4 |
| `TypeAudio` | 8 |

133
hubs.go
View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -92,16 +90,6 @@ func (s *Hubs) GetGlobalHubs(ctx context.Context, count *float64, onlyTransient
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -110,7 +98,11 @@ func (s *Hubs) GetGlobalHubs(ctx context.Context, count *float64, onlyTransient
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -184,21 +176,11 @@ func (s *Hubs) GetGlobalHubs(ctx context.Context, count *float64, onlyTransient
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -210,17 +192,16 @@ func (s *Hubs) GetGlobalHubs(ctx context.Context, count *float64, onlyTransient
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -233,17 +214,16 @@ func (s *Hubs) GetGlobalHubs(ctx context.Context, count *float64, onlyTransient
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -256,28 +236,25 @@ func (s *Hubs) GetGlobalHubs(ctx context.Context, count *float64, onlyTransient
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -344,16 +321,6 @@ func (s *Hubs) GetRecentlyAdded(ctx context.Context, request operations.GetRecen
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -362,7 +329,11 @@ func (s *Hubs) GetRecentlyAdded(ctx context.Context, request operations.GetRecen
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -436,21 +407,11 @@ func (s *Hubs) GetRecentlyAdded(ctx context.Context, request operations.GetRecen
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -462,11 +423,10 @@ func (s *Hubs) GetRecentlyAdded(ctx context.Context, request operations.GetRecen
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
@@ -476,18 +436,16 @@ func (s *Hubs) GetRecentlyAdded(ctx context.Context, request operations.GetRecen
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -560,16 +518,6 @@ func (s *Hubs) GetLibraryHubs(ctx context.Context, sectionID float64, count *flo
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -578,7 +526,11 @@ func (s *Hubs) GetLibraryHubs(ctx context.Context, sectionID float64, count *flo
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -652,21 +604,11 @@ func (s *Hubs) GetLibraryHubs(ctx context.Context, sectionID float64, count *flo
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -678,17 +620,16 @@ func (s *Hubs) GetLibraryHubs(ctx context.Context, sectionID float64, count *flo
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -701,17 +642,16 @@ func (s *Hubs) GetLibraryHubs(ctx context.Context, sectionID float64, count *flo
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -724,28 +664,25 @@ func (s *Hubs) GetLibraryHubs(ctx context.Context, sectionID float64, count *flo
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

View File

@@ -3,10 +3,12 @@
package utils package utils
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"io" "io"
"math/big" "math/big"
"net/http"
"reflect" "reflect"
"regexp" "regexp"
"strconv" "strconv"
@@ -228,3 +230,15 @@ func contains(arr []string, str string) bool {
} }
return false return false
} }
func ConsumeRawBody(res *http.Response) ([]byte, error) {
rawBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
}
res.Body.Close()
res.Body = io.NopCloser(bytes.NewBuffer(rawBody))
return rawBody, nil
}

File diff suppressed because it is too large Load Diff

128
log.go
View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -93,16 +91,6 @@ func (s *Log) LogLine(ctx context.Context, level operations.Level, message strin
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -111,7 +99,11 @@ func (s *Log) LogLine(ctx context.Context, level operations.Level, message strin
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -185,22 +177,12 @@ func (s *Log) LogLine(ctx context.Context, level operations.Level, message strin
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -213,17 +195,16 @@ func (s *Log) LogLine(ctx context.Context, level operations.Level, message strin
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -236,28 +217,25 @@ func (s *Log) LogLine(ctx context.Context, level operations.Level, message strin
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -346,16 +324,6 @@ func (s *Log) LogMultiLine(ctx context.Context, request string, opts ...operatio
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -364,7 +332,11 @@ func (s *Log) LogMultiLine(ctx context.Context, request string, opts ...operatio
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -438,22 +410,12 @@ func (s *Log) LogMultiLine(ctx context.Context, request string, opts ...operatio
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -466,17 +428,16 @@ func (s *Log) LogMultiLine(ctx context.Context, request string, opts ...operatio
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -489,28 +450,25 @@ func (s *Log) LogMultiLine(ctx context.Context, request string, opts ...operatio
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -573,16 +531,6 @@ func (s *Log) EnablePaperTrail(ctx context.Context, opts ...operations.Option) (
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -591,7 +539,11 @@ func (s *Log) EnablePaperTrail(ctx context.Context, opts ...operations.Option) (
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -665,22 +617,12 @@ func (s *Log) EnablePaperTrail(ctx context.Context, opts ...operations.Option) (
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -693,17 +635,16 @@ func (s *Log) EnablePaperTrail(ctx context.Context, opts ...operations.Option) (
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -716,11 +657,10 @@ func (s *Log) EnablePaperTrail(ctx context.Context, opts ...operations.Option) (
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
} }
case httpRes.StatusCode == 403: case httpRes.StatusCode == 403:
@@ -728,18 +668,16 @@ func (s *Log) EnablePaperTrail(ctx context.Context, opts ...operations.Option) (
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

218
media.go
View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -91,16 +89,6 @@ func (s *Media) MarkPlayed(ctx context.Context, key float64, opts ...operations.
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -109,7 +97,11 @@ func (s *Media) MarkPlayed(ctx context.Context, key float64, opts ...operations.
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -183,22 +175,12 @@ func (s *Media) MarkPlayed(ctx context.Context, key float64, opts ...operations.
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -211,17 +193,16 @@ func (s *Media) MarkPlayed(ctx context.Context, key float64, opts ...operations.
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -234,28 +215,25 @@ func (s *Media) MarkPlayed(ctx context.Context, key float64, opts ...operations.
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -326,16 +304,6 @@ func (s *Media) MarkUnplayed(ctx context.Context, key float64, opts ...operation
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -344,7 +312,11 @@ func (s *Media) MarkUnplayed(ctx context.Context, key float64, opts ...operation
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -418,22 +390,12 @@ func (s *Media) MarkUnplayed(ctx context.Context, key float64, opts ...operation
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -446,17 +408,16 @@ func (s *Media) MarkUnplayed(ctx context.Context, key float64, opts ...operation
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -469,28 +430,25 @@ func (s *Media) MarkUnplayed(ctx context.Context, key float64, opts ...operation
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -563,16 +521,6 @@ func (s *Media) UpdatePlayProgress(ctx context.Context, key string, time float64
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -581,7 +529,11 @@ func (s *Media) UpdatePlayProgress(ctx context.Context, key string, time float64
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -655,22 +607,12 @@ func (s *Media) UpdatePlayProgress(ctx context.Context, key string, time float64
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -683,17 +625,16 @@ func (s *Media) UpdatePlayProgress(ctx context.Context, key string, time float64
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -706,28 +647,25 @@ func (s *Media) UpdatePlayProgress(ctx context.Context, key string, time float64
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -796,16 +734,6 @@ func (s *Media) GetBannerImage(ctx context.Context, request operations.GetBanner
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -814,7 +742,11 @@ func (s *Media) GetBannerImage(ctx context.Context, request operations.GetBanner
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -888,16 +820,6 @@ func (s *Media) GetBannerImage(ctx context.Context, request operations.GetBanner
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
res.Headers = httpRes.Header res.Headers = httpRes.Header
@@ -908,17 +830,16 @@ func (s *Media) GetBannerImage(ctx context.Context, request operations.GetBanner
return res, nil return res, nil
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -931,17 +852,16 @@ func (s *Media) GetBannerImage(ctx context.Context, request operations.GetBanner
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -954,28 +874,25 @@ func (s *Media) GetBannerImage(ctx context.Context, request operations.GetBanner
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -1044,16 +961,6 @@ func (s *Media) GetThumbImage(ctx context.Context, request operations.GetThumbIm
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -1062,7 +969,11 @@ func (s *Media) GetThumbImage(ctx context.Context, request operations.GetThumbIm
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -1136,16 +1047,6 @@ func (s *Media) GetThumbImage(ctx context.Context, request operations.GetThumbIm
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
res.Headers = httpRes.Header res.Headers = httpRes.Header
@@ -1156,17 +1057,16 @@ func (s *Media) GetThumbImage(ctx context.Context, request operations.GetThumbIm
return res, nil return res, nil
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1179,17 +1079,16 @@ func (s *Media) GetThumbImage(ctx context.Context, request operations.GetThumbIm
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1202,28 +1101,25 @@ func (s *Media) GetThumbImage(ctx context.Context, request operations.GetThumbIm
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

View File

@@ -128,6 +128,7 @@ const (
GetLibraryItemsQueryParamTypeTvShow GetLibraryItemsQueryParamType = 2 GetLibraryItemsQueryParamTypeTvShow GetLibraryItemsQueryParamType = 2
GetLibraryItemsQueryParamTypeSeason GetLibraryItemsQueryParamType = 3 GetLibraryItemsQueryParamTypeSeason GetLibraryItemsQueryParamType = 3
GetLibraryItemsQueryParamTypeEpisode GetLibraryItemsQueryParamType = 4 GetLibraryItemsQueryParamTypeEpisode GetLibraryItemsQueryParamType = 4
GetLibraryItemsQueryParamTypeAudio GetLibraryItemsQueryParamType = 8
) )
func (e GetLibraryItemsQueryParamType) ToPointer() *GetLibraryItemsQueryParamType { func (e GetLibraryItemsQueryParamType) ToPointer() *GetLibraryItemsQueryParamType {
@@ -146,6 +147,8 @@ func (e *GetLibraryItemsQueryParamType) UnmarshalJSON(data []byte) error {
case 3: case 3:
fallthrough fallthrough
case 4: case 4:
fallthrough
case 8:
*e = GetLibraryItemsQueryParamType(v) *e = GetLibraryItemsQueryParamType(v)
return nil return nil
default: default:
@@ -2596,7 +2599,7 @@ type GetLibraryItemsMediaContainer struct {
ViewGroup string `json:"viewGroup"` ViewGroup string `json:"viewGroup"`
ViewMode *int `json:"viewMode,omitempty"` ViewMode *int `json:"viewMode,omitempty"`
MixedParents *bool `json:"mixedParents,omitempty"` MixedParents *bool `json:"mixedParents,omitempty"`
Metadata []GetLibraryItemsMetadata `json:"Metadata"` Metadata []GetLibraryItemsMetadata `json:"Metadata,omitempty"`
// The Meta object is only included in the response if the `includeMeta` parameter is set to `1`. // The Meta object is only included in the response if the `includeMeta` parameter is set to `1`.
// //
Meta *GetLibraryItemsMeta `json:"Meta,omitempty"` Meta *GetLibraryItemsMeta `json:"Meta,omitempty"`
@@ -2751,7 +2754,7 @@ func (o *GetLibraryItemsMediaContainer) GetMixedParents() *bool {
func (o *GetLibraryItemsMediaContainer) GetMetadata() []GetLibraryItemsMetadata { func (o *GetLibraryItemsMediaContainer) GetMetadata() []GetLibraryItemsMetadata {
if o == nil { if o == nil {
return []GetLibraryItemsMetadata{} return nil
} }
return o.Metadata return o.Metadata
} }

View File

@@ -23,6 +23,7 @@ const (
GetPlaylistContentsQueryParamTypeTvShow GetPlaylistContentsQueryParamType = 2 GetPlaylistContentsQueryParamTypeTvShow GetPlaylistContentsQueryParamType = 2
GetPlaylistContentsQueryParamTypeSeason GetPlaylistContentsQueryParamType = 3 GetPlaylistContentsQueryParamTypeSeason GetPlaylistContentsQueryParamType = 3
GetPlaylistContentsQueryParamTypeEpisode GetPlaylistContentsQueryParamType = 4 GetPlaylistContentsQueryParamTypeEpisode GetPlaylistContentsQueryParamType = 4
GetPlaylistContentsQueryParamTypeAudio GetPlaylistContentsQueryParamType = 8
) )
func (e GetPlaylistContentsQueryParamType) ToPointer() *GetPlaylistContentsQueryParamType { func (e GetPlaylistContentsQueryParamType) ToPointer() *GetPlaylistContentsQueryParamType {
@@ -41,6 +42,8 @@ func (e *GetPlaylistContentsQueryParamType) UnmarshalJSON(data []byte) error {
case 3: case 3:
fallthrough fallthrough
case 4: case 4:
fallthrough
case 8:
*e = GetPlaylistContentsQueryParamType(v) *e = GetPlaylistContentsQueryParamType(v)
return nil return nil
default: default:

View File

@@ -23,6 +23,7 @@ const (
TypeTvShow Type = 2 TypeTvShow Type = 2
TypeSeason Type = 3 TypeSeason Type = 3
TypeEpisode Type = 4 TypeEpisode Type = 4
TypeAudio Type = 8
) )
func (e Type) ToPointer() *Type { func (e Type) ToPointer() *Type {
@@ -41,6 +42,8 @@ func (e *Type) UnmarshalJSON(data []byte) error {
case 3: case 3:
fallthrough fallthrough
case 4: case 4:
fallthrough
case 8:
*e = Type(v) *e = Type(v)
return nil return nil
default: default:

View File

@@ -23,6 +23,7 @@ const (
QueryParamTypeTvShow QueryParamType = 2 QueryParamTypeTvShow QueryParamType = 2
QueryParamTypeSeason QueryParamType = 3 QueryParamTypeSeason QueryParamType = 3
QueryParamTypeEpisode QueryParamType = 4 QueryParamTypeEpisode QueryParamType = 4
QueryParamTypeAudio QueryParamType = 8
) )
func (e QueryParamType) ToPointer() *QueryParamType { func (e QueryParamType) ToPointer() *QueryParamType {
@@ -41,6 +42,8 @@ func (e *QueryParamType) UnmarshalJSON(data []byte) error {
case 3: case 3:
fallthrough fallthrough
case 4: case 4:
fallthrough
case 8:
*e = QueryParamType(v) *e = QueryParamType(v)
return nil return nil
default: default:

View File

@@ -21,6 +21,7 @@ const (
GetSearchLibraryQueryParamTypeTvShow GetSearchLibraryQueryParamType = 2 GetSearchLibraryQueryParamTypeTvShow GetSearchLibraryQueryParamType = 2
GetSearchLibraryQueryParamTypeSeason GetSearchLibraryQueryParamType = 3 GetSearchLibraryQueryParamTypeSeason GetSearchLibraryQueryParamType = 3
GetSearchLibraryQueryParamTypeEpisode GetSearchLibraryQueryParamType = 4 GetSearchLibraryQueryParamTypeEpisode GetSearchLibraryQueryParamType = 4
GetSearchLibraryQueryParamTypeAudio GetSearchLibraryQueryParamType = 8
) )
func (e GetSearchLibraryQueryParamType) ToPointer() *GetSearchLibraryQueryParamType { func (e GetSearchLibraryQueryParamType) ToPointer() *GetSearchLibraryQueryParamType {
@@ -39,6 +40,8 @@ func (e *GetSearchLibraryQueryParamType) UnmarshalJSON(data []byte) error {
case 3: case 3:
fallthrough fallthrough
case 4: case 4:
fallthrough
case 8:
*e = GetSearchLibraryQueryParamType(v) *e = GetSearchLibraryQueryParamType(v)
return nil return nil
default: default:

View File

@@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"net/http" "net/http"
"time"
) )
var GetTokenDetailsServerList = []string{ var GetTokenDetailsServerList = []string{
@@ -499,10 +498,8 @@ func (o *GetTokenDetailsSubscription) GetPlan() *string {
type GetTokenDetailsUserPlexAccount struct { type GetTokenDetailsUserPlexAccount struct {
// Unknown // Unknown
AdsConsent *bool `json:"adsConsent"` AdsConsent *bool `json:"adsConsent"`
// Unknown AdsConsentReminderAt *int64 `json:"adsConsentReminderAt"`
AdsConsentReminderAt *time.Time `json:"adsConsentReminderAt"` AdsConsentSetAt *int64 `json:"adsConsentSetAt"`
// Unknown
AdsConsentSetAt *time.Time `json:"adsConsentSetAt"`
// Unknown // Unknown
Anonymous *bool `default:"false" json:"anonymous"` Anonymous *bool `default:"false" json:"anonymous"`
// The account token // The account token
@@ -597,14 +594,14 @@ func (o *GetTokenDetailsUserPlexAccount) GetAdsConsent() *bool {
return o.AdsConsent return o.AdsConsent
} }
func (o *GetTokenDetailsUserPlexAccount) GetAdsConsentReminderAt() *time.Time { func (o *GetTokenDetailsUserPlexAccount) GetAdsConsentReminderAt() *int64 {
if o == nil { if o == nil {
return nil return nil
} }
return o.AdsConsentReminderAt return o.AdsConsentReminderAt
} }
func (o *GetTokenDetailsUserPlexAccount) GetAdsConsentSetAt() *time.Time { func (o *GetTokenDetailsUserPlexAccount) GetAdsConsentSetAt() *int64 {
if o == nil { if o == nil {
return nil return nil
} }

View File

@@ -23,6 +23,7 @@ const (
GetTopWatchedContentQueryParamTypeTvShow GetTopWatchedContentQueryParamType = 2 GetTopWatchedContentQueryParamTypeTvShow GetTopWatchedContentQueryParamType = 2
GetTopWatchedContentQueryParamTypeSeason GetTopWatchedContentQueryParamType = 3 GetTopWatchedContentQueryParamTypeSeason GetTopWatchedContentQueryParamType = 3
GetTopWatchedContentQueryParamTypeEpisode GetTopWatchedContentQueryParamType = 4 GetTopWatchedContentQueryParamTypeEpisode GetTopWatchedContentQueryParamType = 4
GetTopWatchedContentQueryParamTypeAudio GetTopWatchedContentQueryParamType = 8
) )
func (e GetTopWatchedContentQueryParamType) ToPointer() *GetTopWatchedContentQueryParamType { func (e GetTopWatchedContentQueryParamType) ToPointer() *GetTopWatchedContentQueryParamType {
@@ -41,6 +42,8 @@ func (e *GetTopWatchedContentQueryParamType) UnmarshalJSON(data []byte) error {
case 3: case 3:
fallthrough fallthrough
case 4: case 4:
fallthrough
case 8:
*e = GetTopWatchedContentQueryParamType(v) *e = GetTopWatchedContentQueryParamType(v)
return nil return nil
default: default:

View File

@@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"net/http" "net/http"
"time"
) )
var PostUsersSignInDataServerList = []string{ var PostUsersSignInDataServerList = []string{
@@ -833,10 +832,8 @@ type Trials struct {
type PostUsersSignInDataUserPlexAccount struct { type PostUsersSignInDataUserPlexAccount struct {
// Unknown // Unknown
AdsConsent *bool `json:"adsConsent"` AdsConsent *bool `json:"adsConsent"`
// Unknown AdsConsentReminderAt *int64 `json:"adsConsentReminderAt"`
AdsConsentReminderAt *time.Time `json:"adsConsentReminderAt"` AdsConsentSetAt *int64 `json:"adsConsentSetAt"`
// Unknown
AdsConsentSetAt *time.Time `json:"adsConsentSetAt"`
// Unknown // Unknown
Anonymous *bool `default:"false" json:"anonymous"` Anonymous *bool `default:"false" json:"anonymous"`
// The account token // The account token
@@ -933,14 +930,14 @@ func (o *PostUsersSignInDataUserPlexAccount) GetAdsConsent() *bool {
return o.AdsConsent return o.AdsConsent
} }
func (o *PostUsersSignInDataUserPlexAccount) GetAdsConsentReminderAt() *time.Time { func (o *PostUsersSignInDataUserPlexAccount) GetAdsConsentReminderAt() *int64 {
if o == nil { if o == nil {
return nil return nil
} }
return o.AdsConsentReminderAt return o.AdsConsentReminderAt
} }
func (o *PostUsersSignInDataUserPlexAccount) GetAdsConsentSetAt() *time.Time { func (o *PostUsersSignInDataUserPlexAccount) GetAdsConsentSetAt() *int64 {
if o == nil { if o == nil {
return nil return nil
} }

File diff suppressed because it is too large Load Diff

326
plex.go
View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -88,16 +86,6 @@ func (s *Plex) GetCompanionsData(ctx context.Context, opts ...operations.Option)
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -106,7 +94,11 @@ func (s *Plex) GetCompanionsData(ctx context.Context, opts ...operations.Option)
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -180,21 +172,11 @@ func (s *Plex) GetCompanionsData(ctx context.Context, opts ...operations.Option)
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -206,17 +188,16 @@ func (s *Plex) GetCompanionsData(ctx context.Context, opts ...operations.Option)
res.ResponseBodies = out res.ResponseBodies = out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -229,17 +210,16 @@ func (s *Plex) GetCompanionsData(ctx context.Context, opts ...operations.Option)
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -252,28 +232,25 @@ func (s *Plex) GetCompanionsData(ctx context.Context, opts ...operations.Option)
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -341,16 +318,6 @@ func (s *Plex) GetUserFriends(ctx context.Context, opts ...operations.Option) (*
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -359,7 +326,11 @@ func (s *Plex) GetUserFriends(ctx context.Context, opts ...operations.Option) (*
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -433,21 +404,11 @@ func (s *Plex) GetUserFriends(ctx context.Context, opts ...operations.Option) (*
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -459,17 +420,16 @@ func (s *Plex) GetUserFriends(ctx context.Context, opts ...operations.Option) (*
res.Friends = out res.Friends = out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -482,17 +442,16 @@ func (s *Plex) GetUserFriends(ctx context.Context, opts ...operations.Option) (*
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -505,28 +464,25 @@ func (s *Plex) GetUserFriends(ctx context.Context, opts ...operations.Option) (*
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -590,16 +546,6 @@ func (s *Plex) GetGeoData(ctx context.Context, opts ...operations.Option) (*oper
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -608,7 +554,11 @@ func (s *Plex) GetGeoData(ctx context.Context, opts ...operations.Option) (*oper
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -682,21 +632,11 @@ func (s *Plex) GetGeoData(ctx context.Context, opts ...operations.Option) (*oper
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -708,17 +648,16 @@ func (s *Plex) GetGeoData(ctx context.Context, opts ...operations.Option) (*oper
res.GeoData = &out res.GeoData = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -731,17 +670,16 @@ func (s *Plex) GetGeoData(ctx context.Context, opts ...operations.Option) (*oper
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -754,28 +692,25 @@ func (s *Plex) GetGeoData(ctx context.Context, opts ...operations.Option) (*oper
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -838,16 +773,6 @@ func (s *Plex) GetHomeData(ctx context.Context, opts ...operations.Option) (*ope
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -856,7 +781,11 @@ func (s *Plex) GetHomeData(ctx context.Context, opts ...operations.Option) (*ope
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -930,21 +859,11 @@ func (s *Plex) GetHomeData(ctx context.Context, opts ...operations.Option) (*ope
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -956,17 +875,16 @@ func (s *Plex) GetHomeData(ctx context.Context, opts ...operations.Option) (*ope
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -979,17 +897,16 @@ func (s *Plex) GetHomeData(ctx context.Context, opts ...operations.Option) (*ope
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1002,28 +919,25 @@ func (s *Plex) GetHomeData(ctx context.Context, opts ...operations.Option) (*ope
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -1108,16 +1022,6 @@ func (s *Plex) GetServerResources(ctx context.Context, includeHTTPS *operations.
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -1126,7 +1030,11 @@ func (s *Plex) GetServerResources(ctx context.Context, includeHTTPS *operations.
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -1200,21 +1108,11 @@ func (s *Plex) GetServerResources(ctx context.Context, includeHTTPS *operations.
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1226,17 +1124,16 @@ func (s *Plex) GetServerResources(ctx context.Context, includeHTTPS *operations.
res.PlexDevices = out res.PlexDevices = out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1249,17 +1146,16 @@ func (s *Plex) GetServerResources(ctx context.Context, includeHTTPS *operations.
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1272,28 +1168,25 @@ func (s *Plex) GetServerResources(ctx context.Context, includeHTTPS *operations.
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -1371,16 +1264,6 @@ func (s *Plex) GetPin(ctx context.Context, request operations.GetPinRequest, opt
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -1389,7 +1272,11 @@ func (s *Plex) GetPin(ctx context.Context, request operations.GetPinRequest, opt
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -1463,21 +1350,11 @@ func (s *Plex) GetPin(ctx context.Context, request operations.GetPinRequest, opt
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 201: case httpRes.StatusCode == 201:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1489,17 +1366,16 @@ func (s *Plex) GetPin(ctx context.Context, request operations.GetPinRequest, opt
res.AuthPinContainer = &out res.AuthPinContainer = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1512,28 +1388,25 @@ func (s *Plex) GetPin(ctx context.Context, request operations.GetPinRequest, opt
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -1607,16 +1480,6 @@ func (s *Plex) GetTokenByPinID(ctx context.Context, request operations.GetTokenB
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -1625,7 +1488,11 @@ func (s *Plex) GetTokenByPinID(ctx context.Context, request operations.GetTokenB
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -1699,21 +1566,11 @@ func (s *Plex) GetTokenByPinID(ctx context.Context, request operations.GetTokenB
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1725,17 +1582,16 @@ func (s *Plex) GetTokenByPinID(ctx context.Context, request operations.GetTokenB
res.AuthPinContainer = &out res.AuthPinContainer = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1748,17 +1604,16 @@ func (s *Plex) GetTokenByPinID(ctx context.Context, request operations.GetTokenB
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes)
} }
case httpRes.StatusCode == 404: case httpRes.StatusCode == 404:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1771,28 +1626,25 @@ func (s *Plex) GetTokenByPinID(ctx context.Context, request operations.GetTokenB
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

View File

@@ -335,9 +335,9 @@ func New(opts ...SDKOption) *PlexAPI {
sdkConfiguration: sdkConfiguration{ sdkConfiguration: sdkConfiguration{
Language: "go", Language: "go",
OpenAPIDocVersion: "0.0.3", OpenAPIDocVersion: "0.0.3",
SDKVersion: "0.15.0", SDKVersion: "0.16.0",
GenVersion: "2.429.0", GenVersion: "2.457.2",
UserAgent: "speakeasy-sdk/go 0.15.0 2.429.0 0.0.3 github.com/LukeHagar/plexgo", UserAgent: "speakeasy-sdk/go 0.16.0 2.457.2 0.0.3 github.com/LukeHagar/plexgo",
Globals: globals.Globals{}, Globals: globals.Globals{},
ServerDefaults: []map[string]string{ ServerDefaults: []map[string]string{
{ {

133
search.go
View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -105,16 +103,6 @@ func (s *Search) PerformSearch(ctx context.Context, query string, sectionID *flo
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -123,7 +111,11 @@ func (s *Search) PerformSearch(ctx context.Context, query string, sectionID *flo
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -197,22 +189,12 @@ func (s *Search) PerformSearch(ctx context.Context, query string, sectionID *flo
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -225,17 +207,16 @@ func (s *Search) PerformSearch(ctx context.Context, query string, sectionID *flo
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -248,28 +229,25 @@ func (s *Search) PerformSearch(ctx context.Context, query string, sectionID *flo
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -345,16 +323,6 @@ func (s *Search) PerformVoiceSearch(ctx context.Context, query string, sectionID
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -363,7 +331,11 @@ func (s *Search) PerformVoiceSearch(ctx context.Context, query string, sectionID
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -437,22 +409,12 @@ func (s *Search) PerformVoiceSearch(ctx context.Context, query string, sectionID
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -465,17 +427,16 @@ func (s *Search) PerformVoiceSearch(ctx context.Context, query string, sectionID
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -488,28 +449,25 @@ func (s *Search) PerformVoiceSearch(ctx context.Context, query string, sectionID
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -580,16 +538,6 @@ func (s *Search) GetSearchResults(ctx context.Context, query string, opts ...ope
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -598,7 +546,11 @@ func (s *Search) GetSearchResults(ctx context.Context, query string, opts ...ope
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -672,21 +624,11 @@ func (s *Search) GetSearchResults(ctx context.Context, query string, opts ...ope
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -698,17 +640,16 @@ func (s *Search) GetSearchResults(ctx context.Context, query string, opts ...ope
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -721,17 +662,16 @@ func (s *Search) GetSearchResults(ctx context.Context, query string, opts ...ope
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -744,28 +684,25 @@ func (s *Search) GetSearchResults(ctx context.Context, query string, opts ...ope
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

415
server.go

File diff suppressed because it is too large Load Diff

View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -83,16 +81,6 @@ func (s *Sessions) GetSessions(ctx context.Context, opts ...operations.Option) (
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -101,7 +89,11 @@ func (s *Sessions) GetSessions(ctx context.Context, opts ...operations.Option) (
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -175,21 +167,11 @@ func (s *Sessions) GetSessions(ctx context.Context, opts ...operations.Option) (
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -201,17 +183,16 @@ func (s *Sessions) GetSessions(ctx context.Context, opts ...operations.Option) (
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -224,17 +205,16 @@ func (s *Sessions) GetSessions(ctx context.Context, opts ...operations.Option) (
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -247,28 +227,25 @@ func (s *Sessions) GetSessions(ctx context.Context, opts ...operations.Option) (
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -342,16 +319,6 @@ func (s *Sessions) GetSessionHistory(ctx context.Context, sort *string, accountI
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -360,7 +327,11 @@ func (s *Sessions) GetSessionHistory(ctx context.Context, sort *string, accountI
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -434,21 +405,11 @@ func (s *Sessions) GetSessionHistory(ctx context.Context, sort *string, accountI
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -460,17 +421,16 @@ func (s *Sessions) GetSessionHistory(ctx context.Context, sort *string, accountI
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -483,17 +443,16 @@ func (s *Sessions) GetSessionHistory(ctx context.Context, sort *string, accountI
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -506,28 +465,25 @@ func (s *Sessions) GetSessionHistory(ctx context.Context, sort *string, accountI
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -590,16 +546,6 @@ func (s *Sessions) GetTranscodeSessions(ctx context.Context, opts ...operations.
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -608,7 +554,11 @@ func (s *Sessions) GetTranscodeSessions(ctx context.Context, opts ...operations.
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -682,21 +632,11 @@ func (s *Sessions) GetTranscodeSessions(ctx context.Context, opts ...operations.
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -708,17 +648,16 @@ func (s *Sessions) GetTranscodeSessions(ctx context.Context, opts ...operations.
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -731,17 +670,16 @@ func (s *Sessions) GetTranscodeSessions(ctx context.Context, opts ...operations.
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -754,28 +692,25 @@ func (s *Sessions) GetTranscodeSessions(ctx context.Context, opts ...operations.
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -842,16 +777,6 @@ func (s *Sessions) StopTranscodeSession(ctx context.Context, sessionKey string,
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -860,7 +785,11 @@ func (s *Sessions) StopTranscodeSession(ctx context.Context, sessionKey string,
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -934,22 +863,12 @@ func (s *Sessions) StopTranscodeSession(ctx context.Context, sessionKey string,
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 204: case httpRes.StatusCode == 204:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -962,17 +881,16 @@ func (s *Sessions) StopTranscodeSession(ctx context.Context, sessionKey string,
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -985,28 +903,25 @@ func (s *Sessions) StopTranscodeSession(ctx context.Context, sessionKey string,
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -91,16 +89,6 @@ func (s *Statistics) GetStatistics(ctx context.Context, timespan *int64, opts ..
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -109,7 +97,11 @@ func (s *Statistics) GetStatistics(ctx context.Context, timespan *int64, opts ..
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -183,21 +175,11 @@ func (s *Statistics) GetStatistics(ctx context.Context, timespan *int64, opts ..
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -209,17 +191,16 @@ func (s *Statistics) GetStatistics(ctx context.Context, timespan *int64, opts ..
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -232,17 +213,16 @@ func (s *Statistics) GetStatistics(ctx context.Context, timespan *int64, opts ..
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -255,28 +235,25 @@ func (s *Statistics) GetStatistics(ctx context.Context, timespan *int64, opts ..
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -347,16 +324,6 @@ func (s *Statistics) GetResourcesStatistics(ctx context.Context, timespan *int64
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -365,7 +332,11 @@ func (s *Statistics) GetResourcesStatistics(ctx context.Context, timespan *int64
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -439,21 +410,11 @@ func (s *Statistics) GetResourcesStatistics(ctx context.Context, timespan *int64
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -465,17 +426,16 @@ func (s *Statistics) GetResourcesStatistics(ctx context.Context, timespan *int64
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -488,17 +448,16 @@ func (s *Statistics) GetResourcesStatistics(ctx context.Context, timespan *int64
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -511,28 +470,25 @@ func (s *Statistics) GetResourcesStatistics(ctx context.Context, timespan *int64
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -603,16 +559,6 @@ func (s *Statistics) GetBandwidthStatistics(ctx context.Context, timespan *int64
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -621,7 +567,11 @@ func (s *Statistics) GetBandwidthStatistics(ctx context.Context, timespan *int64
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -695,21 +645,11 @@ func (s *Statistics) GetBandwidthStatistics(ctx context.Context, timespan *int64
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -721,17 +661,16 @@ func (s *Statistics) GetBandwidthStatistics(ctx context.Context, timespan *int64
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -744,17 +683,16 @@ func (s *Statistics) GetBandwidthStatistics(ctx context.Context, timespan *int64
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -767,28 +705,25 @@ func (s *Statistics) GetBandwidthStatistics(ctx context.Context, timespan *int64
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -84,16 +82,6 @@ func (s *Updater) GetUpdateStatus(ctx context.Context, opts ...operations.Option
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -102,7 +90,11 @@ func (s *Updater) GetUpdateStatus(ctx context.Context, opts ...operations.Option
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -176,21 +168,11 @@ func (s *Updater) GetUpdateStatus(ctx context.Context, opts ...operations.Option
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -202,17 +184,16 @@ func (s *Updater) GetUpdateStatus(ctx context.Context, opts ...operations.Option
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -225,17 +206,16 @@ func (s *Updater) GetUpdateStatus(ctx context.Context, opts ...operations.Option
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -248,28 +228,25 @@ func (s *Updater) GetUpdateStatus(ctx context.Context, opts ...operations.Option
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -340,16 +317,6 @@ func (s *Updater) CheckForUpdates(ctx context.Context, download *operations.Down
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -358,7 +325,11 @@ func (s *Updater) CheckForUpdates(ctx context.Context, download *operations.Down
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -432,22 +403,12 @@ func (s *Updater) CheckForUpdates(ctx context.Context, download *operations.Down
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -460,17 +421,16 @@ func (s *Updater) CheckForUpdates(ctx context.Context, download *operations.Down
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -483,28 +443,25 @@ func (s *Updater) CheckForUpdates(ctx context.Context, download *operations.Down
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -576,16 +533,6 @@ func (s *Updater) ApplyUpdates(ctx context.Context, tonight *operations.Tonight,
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -594,7 +541,11 @@ func (s *Updater) ApplyUpdates(ctx context.Context, tonight *operations.Tonight,
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -668,22 +619,12 @@ func (s *Updater) ApplyUpdates(ctx context.Context, tonight *operations.Tonight,
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -696,17 +637,16 @@ func (s *Updater) ApplyUpdates(ctx context.Context, tonight *operations.Tonight,
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -719,11 +659,10 @@ func (s *Updater) ApplyUpdates(ctx context.Context, tonight *operations.Tonight,
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
@@ -731,18 +670,16 @@ func (s *Updater) ApplyUpdates(ctx context.Context, tonight *operations.Tonight,
case httpRes.StatusCode == 500: case httpRes.StatusCode == 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
"net/url" "net/url"
) )
@@ -87,16 +85,6 @@ func (s *Video) GetTimeline(ctx context.Context, request operations.GetTimelineR
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -105,7 +93,11 @@ func (s *Video) GetTimeline(ctx context.Context, request operations.GetTimelineR
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -179,22 +171,12 @@ func (s *Video) GetTimeline(ctx context.Context, request operations.GetTimelineR
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -207,17 +189,16 @@ func (s *Video) GetTimeline(ctx context.Context, request operations.GetTimelineR
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -230,28 +211,25 @@ func (s *Video) GetTimeline(ctx context.Context, request operations.GetTimelineR
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }
@@ -318,16 +296,6 @@ func (s *Video) StartUniversalTranscode(ctx context.Context, request operations.
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -336,7 +304,11 @@ func (s *Video) StartUniversalTranscode(ctx context.Context, request operations.
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -410,22 +382,12 @@ func (s *Video) StartUniversalTranscode(ctx context.Context, request operations.
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
case httpRes.StatusCode == 400: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -438,17 +400,16 @@ func (s *Video) StartUniversalTranscode(ctx context.Context, request operations.
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -461,28 +422,25 @@ func (s *Video) StartUniversalTranscode(ctx context.Context, request operations.
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }

View File

@@ -10,9 +10,7 @@ import (
"github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/internal/utils"
"github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/operations"
"github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/LukeHagar/plexgo/models/sdkerrors"
"github.com/LukeHagar/plexgo/retry"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"io"
"net/http" "net/http"
) )
@@ -93,16 +91,6 @@ func (s *Watchlist) GetWatchList(ctx context.Context, request operations.GetWatc
if retryConfig == nil { if retryConfig == nil {
if globalRetryConfig != nil { if globalRetryConfig != nil {
retryConfig = globalRetryConfig retryConfig = globalRetryConfig
} else {
retryConfig = &retry.Config{
Strategy: "backoff", Backoff: &retry.BackoffStrategy{
InitialInterval: 500,
MaxInterval: 60000,
Exponent: 1.5,
MaxElapsedTime: 3600000,
},
RetryConnectionErrors: true,
}
} }
} }
@@ -111,7 +99,11 @@ func (s *Watchlist) GetWatchList(ctx context.Context, request operations.GetWatc
httpRes, err = utils.Retry(ctx, utils.Retries{ httpRes, err = utils.Retry(ctx, utils.Retries{
Config: retryConfig, Config: retryConfig,
StatusCodes: []string{ StatusCodes: []string{
"5XX", "429",
"500",
"502",
"503",
"504",
}, },
}, func() (*http.Response, error) { }, func() (*http.Response, error) {
if req.Body != nil { if req.Body != nil {
@@ -185,21 +177,11 @@ func (s *Watchlist) GetWatchList(ctx context.Context, request operations.GetWatc
RawResponse: httpRes, 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 { switch {
case httpRes.StatusCode == 200: case httpRes.StatusCode == 200:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -211,17 +193,16 @@ func (s *Watchlist) GetWatchList(ctx context.Context, request operations.GetWatc
res.Object = &out res.Object = &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 400:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -234,17 +215,16 @@ func (s *Watchlist) GetWatchList(ctx context.Context, request operations.GetWatc
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode == 401:
switch { switch {
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -257,28 +237,25 @@ func (s *Watchlist) GetWatchList(ctx context.Context, request operations.GetWatc
out.RawResponse = httpRes out.RawResponse = httpRes
return nil, &out return nil, &out
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) 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: case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
fallthrough fallthrough
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
default: default:
rawBody, err := getRawBody() rawBody, err := utils.ConsumeRawBody(httpRes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
} }