// Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. package plexgo import ( "bytes" "context" "fmt" "github.com/LukeHagar/plexgo/internal/hooks" "github.com/LukeHagar/plexgo/internal/utils" "github.com/LukeHagar/plexgo/models/operations" "github.com/LukeHagar/plexgo/models/sdkerrors" "github.com/cenkalti/backoff/v4" "io" "net/http" "net/url" ) // Authentication - API Calls regarding authentication for Plex Media Server type Authentication struct { sdkConfiguration sdkConfiguration } func newAuthentication(sdkConfig sdkConfiguration) *Authentication { return &Authentication{ sdkConfiguration: sdkConfig, } } // GetTransientToken - Get a Transient Token // This endpoint provides the caller with a temporary token with the same access level as the caller's token. These tokens are valid for up to 48 hours and are destroyed if the server instance is restarted. func (s *Authentication) GetTransientToken(ctx context.Context, type_ operations.GetTransientTokenQueryParamType, scope operations.Scope, opts ...operations.Option) (*operations.GetTransientTokenResponse, error) { hookCtx := hooks.HookContext{ Context: ctx, OperationID: "getTransientToken", OAuth2Scopes: []string{}, SecuritySource: s.sdkConfiguration.Security, } request := operations.GetTransientTokenRequest{ Type: type_, Scope: scope, } o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, operations.SupportedOptionTimeout, } for _, opt := range opts { if err := opt(&o, supportedOptions...); err != nil { return nil, fmt.Errorf("error applying option: %w", err) } } baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/security/token") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } timeout := o.Timeout if timeout == nil { timeout = s.sdkConfiguration.Timeout } if timeout != nil { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, *timeout) defer cancel() } req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) if err := utils.PopulateQueryParams(ctx, req, request, nil); err != nil { return nil, fmt.Errorf("error populating query params: %w", err) } if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil { return nil, err } globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { if globalRetryConfig != nil { retryConfig = globalRetryConfig } } var httpRes *http.Response if retryConfig != nil { httpRes, err = utils.Retry(ctx, utils.Retries{ Config: retryConfig, StatusCodes: []string{ "429", "500", "502", "503", "504", }, }, func() (*http.Response, error) { if req.Body != nil { copyBody, err := req.GetBody() if err != nil { return nil, err } req.Body = copyBody } req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { return nil, backoff.Permanent(err) } httpRes, err := s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) } else { err = fmt.Errorf("error sending request: no response") } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) } return httpRes, err }) if err != nil { return nil, err } else { httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } } } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { return nil, err } httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) } else { err = fmt.Errorf("error sending request: no response") } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) return nil, err } else if utils.MatchStatusCodes([]string{"400", "401", "4XX", "5XX"}, httpRes.StatusCode) { _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) if err != nil { return nil, err } else if _httpRes != nil { httpRes = _httpRes } } else { httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } } } res := &operations.GetTransientTokenResponse{ StatusCode: httpRes.StatusCode, ContentType: httpRes.Header.Get("Content-Type"), RawResponse: httpRes, } getRawBody := func() ([]byte, error) { rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) return rawBody, nil } switch { case httpRes.StatusCode == 200: case httpRes.StatusCode == 400: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out sdkerrors.GetTransientTokenBadRequest if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } out.RawResponse = httpRes return nil, &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode == 401: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out sdkerrors.GetTransientTokenUnauthorized if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } out.RawResponse = httpRes return nil, &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: fallthrough case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) } return res, nil } // GetSourceConnectionInformation - Get Source Connection Information // If a caller requires connection details and a transient token for a source that is known to the server, for example a cloud media provider or shared PMS, then this endpoint can be called. This endpoint is only accessible with either an admin token or a valid transient token generated from an admin token. // Note: requires Plex Media Server >= 1.15.4. func (s *Authentication) GetSourceConnectionInformation(ctx context.Context, source string, opts ...operations.Option) (*operations.GetSourceConnectionInformationResponse, error) { hookCtx := hooks.HookContext{ Context: ctx, OperationID: "getSourceConnectionInformation", OAuth2Scopes: []string{}, SecuritySource: s.sdkConfiguration.Security, } request := operations.GetSourceConnectionInformationRequest{ Source: source, } o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, operations.SupportedOptionTimeout, } for _, opt := range opts { if err := opt(&o, supportedOptions...); err != nil { return nil, fmt.Errorf("error applying option: %w", err) } } baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/security/resources") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } timeout := o.Timeout if timeout == nil { timeout = s.sdkConfiguration.Timeout } if timeout != nil { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, *timeout) defer cancel() } req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) if err := utils.PopulateQueryParams(ctx, req, request, nil); err != nil { return nil, fmt.Errorf("error populating query params: %w", err) } if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil { return nil, err } globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { if globalRetryConfig != nil { retryConfig = globalRetryConfig } } var httpRes *http.Response if retryConfig != nil { httpRes, err = utils.Retry(ctx, utils.Retries{ Config: retryConfig, StatusCodes: []string{ "429", "500", "502", "503", "504", }, }, func() (*http.Response, error) { if req.Body != nil { copyBody, err := req.GetBody() if err != nil { return nil, err } req.Body = copyBody } req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { return nil, backoff.Permanent(err) } httpRes, err := s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) } else { err = fmt.Errorf("error sending request: no response") } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) } return httpRes, err }) if err != nil { return nil, err } else { httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } } } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { return nil, err } httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) } else { err = fmt.Errorf("error sending request: no response") } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) return nil, err } else if utils.MatchStatusCodes([]string{"400", "401", "4XX", "5XX"}, httpRes.StatusCode) { _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) if err != nil { return nil, err } else if _httpRes != nil { httpRes = _httpRes } } else { httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } } } res := &operations.GetSourceConnectionInformationResponse{ StatusCode: httpRes.StatusCode, ContentType: httpRes.Header.Get("Content-Type"), RawResponse: httpRes, } getRawBody := func() ([]byte, error) { rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) return rawBody, nil } switch { case httpRes.StatusCode == 200: case httpRes.StatusCode == 400: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out sdkerrors.GetSourceConnectionInformationBadRequest if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } out.RawResponse = httpRes return nil, &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode == 401: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out sdkerrors.GetSourceConnectionInformationUnauthorized if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } out.RawResponse = httpRes return nil, &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: fallthrough case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) } return res, nil } // GetTokenDetails - Get Token Details // Get the User data from the provided X-Plex-Token func (s *Authentication) GetTokenDetails(ctx context.Context, opts ...operations.Option) (*operations.GetTokenDetailsResponse, error) { hookCtx := hooks.HookContext{ Context: ctx, OperationID: "getTokenDetails", OAuth2Scopes: []string{}, SecuritySource: s.sdkConfiguration.Security, } o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionServerURL, operations.SupportedOptionRetries, operations.SupportedOptionTimeout, } for _, opt := range opts { if err := opt(&o, supportedOptions...); err != nil { return nil, fmt.Errorf("error applying option: %w", err) } } baseURL := utils.ReplaceParameters(operations.GetTokenDetailsServerList[0], map[string]string{}) if o.ServerURL != nil { baseURL = *o.ServerURL } opURL, err := url.JoinPath(baseURL, "/user") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } timeout := o.Timeout if timeout == nil { timeout = s.sdkConfiguration.Timeout } if timeout != nil { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, *timeout) defer cancel() } req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil { return nil, err } globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { if globalRetryConfig != nil { retryConfig = globalRetryConfig } } var httpRes *http.Response if retryConfig != nil { httpRes, err = utils.Retry(ctx, utils.Retries{ Config: retryConfig, StatusCodes: []string{ "429", "500", "502", "503", "504", }, }, func() (*http.Response, error) { if req.Body != nil { copyBody, err := req.GetBody() if err != nil { return nil, err } req.Body = copyBody } req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { return nil, backoff.Permanent(err) } httpRes, err := s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) } else { err = fmt.Errorf("error sending request: no response") } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) } return httpRes, err }) if err != nil { return nil, err } else { httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } } } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { return nil, err } httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) } else { err = fmt.Errorf("error sending request: no response") } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) return nil, err } else if utils.MatchStatusCodes([]string{"400", "401", "4XX", "5XX"}, httpRes.StatusCode) { _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) if err != nil { return nil, err } else if _httpRes != nil { httpRes = _httpRes } } else { httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } } } res := &operations.GetTokenDetailsResponse{ StatusCode: httpRes.StatusCode, ContentType: httpRes.Header.Get("Content-Type"), RawResponse: httpRes, } getRawBody := func() ([]byte, error) { rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) return rawBody, nil } switch { case httpRes.StatusCode == 200: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out operations.GetTokenDetailsUserPlexAccount if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.UserPlexAccount = &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode == 400: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out sdkerrors.GetTokenDetailsBadRequest if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } out.RawResponse = httpRes return nil, &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode == 401: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out sdkerrors.GetTokenDetailsUnauthorized if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } out.RawResponse = httpRes return nil, &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: fallthrough case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) } return res, nil } // PostUsersSignInData - Get User Sign In Data // Sign in user with username and password and return user data with Plex authentication token func (s *Authentication) PostUsersSignInData(ctx context.Context, xPlexClientIdentifier *string, requestBody *operations.PostUsersSignInDataRequestBody, opts ...operations.Option) (*operations.PostUsersSignInDataResponse, error) { hookCtx := hooks.HookContext{ Context: ctx, OperationID: "post-users-sign-in-data", OAuth2Scopes: []string{}, SecuritySource: nil, } request := operations.PostUsersSignInDataRequest{ XPlexClientIdentifier: xPlexClientIdentifier, RequestBody: requestBody, } globals := operations.PostUsersSignInDataGlobals{ XPlexClientIdentifier: s.sdkConfiguration.Globals.XPlexClientIdentifier, } o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionServerURL, operations.SupportedOptionRetries, operations.SupportedOptionTimeout, } for _, opt := range opts { if err := opt(&o, supportedOptions...); err != nil { return nil, fmt.Errorf("error applying option: %w", err) } } baseURL := utils.ReplaceParameters(operations.PostUsersSignInDataServerList[0], map[string]string{}) if o.ServerURL != nil { baseURL = *o.ServerURL } opURL, err := url.JoinPath(baseURL, "/users/signin") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, true, "RequestBody", "form", `request:"mediaType=application/x-www-form-urlencoded"`) if err != nil { return nil, err } timeout := o.Timeout if timeout == nil { timeout = s.sdkConfiguration.Timeout } if timeout != nil { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, *timeout) defer cancel() } req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) req.Header.Set("Content-Type", reqContentType) if err := utils.PopulateQueryParams(ctx, req, request, globals); err != nil { return nil, fmt.Errorf("error populating query params: %w", err) } globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { if globalRetryConfig != nil { retryConfig = globalRetryConfig } } var httpRes *http.Response if retryConfig != nil { httpRes, err = utils.Retry(ctx, utils.Retries{ Config: retryConfig, StatusCodes: []string{ "429", "500", "502", "503", "504", }, }, func() (*http.Response, error) { if req.Body != nil { copyBody, err := req.GetBody() if err != nil { return nil, err } req.Body = copyBody } req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { return nil, backoff.Permanent(err) } httpRes, err := s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) } else { err = fmt.Errorf("error sending request: no response") } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) } return httpRes, err }) if err != nil { return nil, err } else { httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } } } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { return nil, err } httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) } else { err = fmt.Errorf("error sending request: no response") } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) return nil, err } else if utils.MatchStatusCodes([]string{"400", "401", "4XX", "5XX"}, httpRes.StatusCode) { _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) if err != nil { return nil, err } else if _httpRes != nil { httpRes = _httpRes } } else { httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } } } res := &operations.PostUsersSignInDataResponse{ StatusCode: httpRes.StatusCode, ContentType: httpRes.Header.Get("Content-Type"), RawResponse: httpRes, } getRawBody := func() ([]byte, error) { rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) return rawBody, nil } switch { case httpRes.StatusCode == 201: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out operations.PostUsersSignInDataUserPlexAccount if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.UserPlexAccount = &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode == 400: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out sdkerrors.PostUsersSignInDataBadRequest if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } out.RawResponse = httpRes return nil, &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode == 401: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): rawBody, err := getRawBody() if err != nil { return nil, err } var out sdkerrors.PostUsersSignInDataUnauthorized if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } out.RawResponse = httpRes return nil, &out default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: fallthrough case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) default: rawBody, err := getRawBody() if err != nil { return nil, err } return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) } return res, nil }