// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. package sdk import ( "bytes" "context" "fmt" "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/models/operations" "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/models/sdkerrors" "github.com/LukeHagar/terraform-provider-PlexAPI/internal/sdk/pkg/utils" "io" "net/http" "strings" ) // Activities are awesome. They provide a way to monitor and control asynchronous operations on the server. In order to receive real-time updates for activities, a client would normally subscribe via either EventSource or Websocket endpoints. // Activities are associated with HTTP replies via a special `X-Plex-Activity` header which contains the UUID of the activity. // Activities are optional cancellable. If cancellable, they may be cancelled via the `DELETE` endpoint. Other details: // - They can contain a `progress` (from 0 to 100) marking the percent completion of the activity. // - They must contain an `type` which is used by clients to distinguish the specific activity. // - They may contain a `Context` object with attributes which associate the activity with various specific entities (items, libraries, etc.) // - The may contain a `Response` object which attributes which represent the result of the asynchronous operation. type Activities struct { sdkConfiguration sdkConfiguration } func newActivities(sdkConfig sdkConfiguration) *Activities { return &Activities{ sdkConfiguration: sdkConfig, } } // GetServerActivities - Get Server Activities // Get Server Activities func (s *Activities) GetServerActivities(ctx context.Context) (*operations.GetServerActivitiesResponse, error) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url := strings.TrimSuffix(baseURL, "/") + "/activities" req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("user-agent", s.sdkConfiguration.UserAgent) client := s.sdkConfiguration.SecurityClient httpRes, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error sending request: %w", err) } if httpRes == nil { return nil, fmt.Errorf("error sending request: no response") } rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) contentType := httpRes.Header.Get("Content-Type") res := &operations.GetServerActivitiesResponse{ StatusCode: httpRes.StatusCode, ContentType: contentType, RawResponse: httpRes, } switch { case httpRes.StatusCode == 200: switch { case utils.MatchContentType(contentType, `application/json`): var out operations.GetServerActivitiesResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.TwoHundredApplicationJSONObject = &out default: return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } case httpRes.StatusCode == 400: case httpRes.StatusCode == 401: switch { case utils.MatchContentType(contentType, `application/json`): var out operations.GetServerActivitiesActivitiesResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.FourHundredAndOneApplicationJSONObject = &out default: return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } } return res, nil } // CancelServerActivities - Cancel Server Activities // Cancel Server Activities func (s *Activities) CancelServerActivities(ctx context.Context, request operations.CancelServerActivitiesRequest) (*operations.CancelServerActivitiesResponse, error) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url, err := utils.GenerateURL(ctx, baseURL, "/activities/{activityUUID}", request, nil) if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } req, err := http.NewRequestWithContext(ctx, "DELETE", url, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("user-agent", s.sdkConfiguration.UserAgent) client := s.sdkConfiguration.SecurityClient httpRes, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error sending request: %w", err) } if httpRes == nil { return nil, fmt.Errorf("error sending request: no response") } rawBody, err := io.ReadAll(httpRes.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %w", err) } httpRes.Body.Close() httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) contentType := httpRes.Header.Get("Content-Type") res := &operations.CancelServerActivitiesResponse{ StatusCode: httpRes.StatusCode, ContentType: contentType, RawResponse: httpRes, } switch { case httpRes.StatusCode == 200: fallthrough case httpRes.StatusCode == 400: case httpRes.StatusCode == 401: switch { case utils.MatchContentType(contentType, `application/json`): var out operations.CancelServerActivitiesResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } res.Object = &out default: return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes) } } return res, nil }