mirror of
https://github.com/LukeHagar/plexgo.git
synced 2025-12-06 04:20:46 +00:00
* `PlexApi.LibraryPlaylists.AddPlaylistItems()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Hubs.GetMetadataHubs()`: * `request.Request.OnlyTransient` **Changed** * `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LibraryCollections.MoveCollectionItem()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LibraryCollections.DeleteCollectionItem()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LibraryCollections.AddCollectionItems()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Content.GetSonicallySimilar()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Butler.StopTask()`: `request.Request` **Changed** **Breaking** ⚠️ * `PlexApi.Butler.StartTask()`: `request.Request` **Changed** **Breaking** ⚠️ * `PlexApi.Content.GetSonicPath()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.DownloadQueue.GetItemDecision()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Hubs.GetAllHubs()`: * `request.Request.OnlyTransient` **Changed** * `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Hubs.GetContinueWatching()`: `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Hubs.GetPromotedHubs()`: `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Content.GetAllLeaves()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Hubs.GetPostplayHubs()`: * `request.Request.OnlyTransient` **Changed** * `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Hubs.GetRelatedHubs()`: * `request.Request.OnlyTransient` **Changed** * `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Hubs.GetSectionHubs()`: * `request.Request.OnlyTransient` **Changed** * `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Content.ListContent()`: * `request.Request` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Content.GetAlbums()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Search.SearchHubs()`: `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Search.VoiceSearchHubs()`: * `request.Request.Type` **Changed** **Breaking** ⚠️ * `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetLibraryItems()`: * `request.Request.MediaQuery` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.IngestTransientItem()`: * `request.Request` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetLibraryMatches()`: * `request.Request` **Changed** **Breaking** ⚠️ * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Content.GetMetadataItem()`: * `request.Request` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetSections()`: `response.MediaContainer.Directory.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.AddSection()`: * `request.Request` **Changed** * `response.MediaContainer.Directory.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetTags()`: * `request.Request.Type` **Changed** **Breaking** ⚠️ * `PlexApi.Content.GetCollectionItems()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetAllItemLeaves()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Status.ListSessions()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.PlayQueue.MovePlayQueueItem()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetExtras()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.PlayQueue.DeletePlayQueueItem()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.PlayQueue.Unshuffle()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.ListMatches()`: * `request.Request.Manual` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.ListSonicallySimilar()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.PlayQueue.ResetPlayQueue()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetRelatedItems()`: `response.MediaContainer.Hub.[].Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.ListSimilar()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.PlayQueue.ClearPlayQueue()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetItemTree()`: `response.MediaContainer.MetadataItem.[]` **Changed** **Breaking** ⚠️ * `PlexApi.PlayQueue.AddToPlayQueue()`: * `request.Request.Next` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.PlayQueue.GetPlayQueue()`: * `request.Request` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LibraryPlaylists.MovePlaylistItem()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetPerson()`: `response.MediaContainer.Directory.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.ListPersonMedia()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LibraryPlaylists.GetPlaylistGeneratorItems()`: `response.MediaContainer.Metadata` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetLibraryDetails()`: * `request.Request.IncludeDetails` **Changed** * `response.MediaContainer.Directory.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LibraryPlaylists.ModifyPlaylistGenerator()`: * `request.Request.Item` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.Autocomplete()`: * `request.Request.MediaQuery` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetCollections()`: * `request.Request.MediaQuery` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.GetCommon()`: * `request.Request.MediaQuery` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LibraryPlaylists.DeletePlaylistItem()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LibraryPlaylists.ClearPlaylistItems()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.PlayQueue.Shuffle()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LibraryPlaylists.CreatePlaylist()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Playlist.GetPlaylistItems()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Playlist.GetPlaylist()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Collections.CreateCollection()`: * `request.Request.Type` **Changed** **Breaking** ⚠️ * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.DvRs.TuneChannel()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LiveTv.GetSessions()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.LiveTv.GetLiveTvSession()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Playlist.ListPlaylists()`: `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Subscriptions.GetAllSubscriptions()`: * `request.Request` **Changed** * `response.MediaContainer.MediaSubscription.[].MediaGrabOperation.[].Metadata` **Changed** **Breaking** ⚠️ * `PlexApi.Subscriptions.CreateSubscription()`: `response.MediaContainer.MediaSubscription.[].MediaGrabOperation.[].Metadata` **Changed** **Breaking** ⚠️ * `PlexApi.Subscriptions.GetScheduledRecordings()`: `response.MediaContainer.MediaGrabOperation.[].Metadata` **Changed** **Breaking** ⚠️ * `PlexApi.Subscriptions.GetTemplate()`: `response.MediaContainer.SubscriptionTemplate.[].MediaSubscription.[].MediaGrabOperation.[].Metadata` **Changed** **Breaking** ⚠️ * `PlexApi.Subscriptions.GetSubscription()`: * `request.Request` **Changed** * `response.MediaContainer.MediaSubscription.[].MediaGrabOperation.[].Metadata` **Changed** **Breaking** ⚠️ * `PlexApi.Subscriptions.EditSubscriptionPreferences()`: `response.MediaContainer.MediaSubscription.[].MediaGrabOperation.[].Metadata` **Changed** **Breaking** ⚠️ * `PlexApi.Subscriptions.ReorderSubscription()`: `response.MediaContainer.MediaSubscription.[].MediaGrabOperation.[].Metadata` **Changed** **Breaking** ⚠️ * `PlexApi.Transcoder.MakeDecision()`: * `request.Request` **Changed** * `response.MediaContainer.Metadata.[]` **Changed** **Breaking** ⚠️ * `PlexApi.Library.RefreshItemsMetadata()`: * `request.Request.MarkUpdated` **Changed** * `PlexApi.Authentication.Post-Users-Sign-In-Data()`: **Added** * `PlexApi.Transcoder.StartTranscodeSession()`: `request.Request` **Changed** * `PlexApi.Devices.ModifyDevice()`: * `request.Request.Enabled` **Changed** * `PlexApi.Library.GetMediaPart()`: * `request.Request.Download` **Changed** * `PlexApi.Library.DetectIntros()`: * `request.Request.Force` **Changed** * `PlexApi.Library.RefreshSection()`: * `request.Request.Force` **Changed** * `PlexApi.LibraryPlaylists.UploadPlaylist()`: * `request.Request.Force` **Changed** * `PlexApi.Library.DeleteMediaItem()`: * `request.Request.Proxy` **Changed** * `PlexApi.Authentication.GetTokenDetails()`: **Added** * `PlexApi.Library.GetFirstCharacters()`: * `request.Request.MediaQuery` **Changed** * `PlexApi.Library.UpdateItems()`: * `request.Request.Field.locked` **Changed** * `PlexApi.Library.DeleteLibrarySection()`: * `request.Request.Async` **Changed** * `PlexApi.Library.SetStreamSelection()`: * `request.Request.AllParts` **Changed** * `PlexApi.PlayQueue.CreatePlayQueue()`: `request.Request` **Changed** * `PlexApi.Library.GetAugmentationStatus()`: * `request.Request.Wait` **Changed** * `PlexApi.Library.DetectVoiceActivity()`: `request.Request` **Changed** * `PlexApi.Transcoder.TranscodeImage()`: `request.Request` **Changed** * `PlexApi.Transcoder.TranscodeSubtitles()`: `request.Request` **Changed** * `PlexApi.Library.AddSubtitles()`: `request.Request` **Changed** * `PlexApi.Library.GetStream()`: * `request.Request.AutoAdjustSubtitle` **Changed** * `PlexApi.Library.StartBifGeneration()`: * `request.Request.Force` **Changed** * `PlexApi.Library.DetectCredits()`: `request.Request` **Changed** * `PlexApi.UltraBlur.GetImage()`: * `request.Request.Noise` **Changed** * `PlexApi.Library.GenerateThumbs()`: * `request.Request.Force` **Changed** * `PlexApi.Updater.ApplyUpdates()`: `request.Request` **Changed** * `PlexApi.Updater.CheckUpdates()`: * `request.Request.Download` **Changed** * `PlexApi.Library.DeleteMetadataItem()`: * `request.Request.Proxy` **Changed** * `PlexApi.Library.OptimizeDatabase()`: * `request.Request.Async` **Changed** * `PlexApi.Hubs.UpdateHubVisibility()`: `request.Request` **Changed** * `PlexApi.Hubs.CreateCustomHub()`: `request.Request` **Changed** * `PlexApi.Library.GetSectionImage()`: * `request.Request.MediaQuery` **Changed** * `PlexApi.DownloadQueue.AddDownloadQueueItems()`: `request.Request` **Changed** * `PlexApi.Timeline.Report()`: `request.Request` **Changed** * `PlexApi.General.GetSourceConnectionInformation()`: * `request.Request.Refresh` **Changed** * `PlexApi.Plex.Get-Server-Resources()`: **Added** * `PlexApi.Users.Get-Users()`: **Added**
2919 lines
84 KiB
Go
2919 lines
84 KiB
Go
// Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
|
|
package plexgo
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"github.com/LukeHagar/plexgo/internal/config"
|
|
"github.com/LukeHagar/plexgo/internal/hooks"
|
|
"github.com/LukeHagar/plexgo/internal/utils"
|
|
"github.com/LukeHagar/plexgo/models/components"
|
|
"github.com/LukeHagar/plexgo/models/operations"
|
|
"github.com/LukeHagar/plexgo/models/sdkerrors"
|
|
"github.com/LukeHagar/plexgo/retry"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// Devices - Media grabbers provide ways for media to be obtained for a given protocol. The simplest ones are `stream` and `download`. More complex grabbers can have associated devices
|
|
//
|
|
// Network tuners can present themselves on the network using the Simple Service Discovery Protocol and Plex Media Server will discover them. The following XML is an example of the data returned from SSDP. The `deviceType`, `serviceType`, and `serviceId` values must remain as they are in the example in order for PMS to properly discover the device. Other less-obvious fields are described in the parameters section below.
|
|
//
|
|
// Example SSDP output
|
|
// ```
|
|
// <root xmlns="urn:schemas-upnp-org:device-1-0">
|
|
//
|
|
// <specVersion>
|
|
// <major>1</major>
|
|
// <minor>0</minor>
|
|
// </specVersion>
|
|
// <device>
|
|
// <deviceType>urn:plex-tv:device:Media:1</deviceType>
|
|
// <friendlyName>Turing Hopper 3000</friendlyName>
|
|
// <manufacturer>Plex, Inc.</manufacturer>
|
|
// <manufacturerURL>https://plex.tv/</manufacturerURL>
|
|
// <modelDescription>Turing Hopper 3000 Media Grabber</modelDescription>
|
|
// <modelName>Plex Media Grabber</modelName>
|
|
// <modelNumber>1</modelNumber>
|
|
// <modelURL>https://plex.tv</modelURL>
|
|
// <UDN>uuid:42fde8e4-93b6-41e5-8a63-12d848655811</UDN>
|
|
// <serviceList>
|
|
// <service>
|
|
// <URLBase>http://10.0.0.5:8088</URLBase>
|
|
// <serviceType>urn:plex-tv:service:MediaGrabber:1</serviceType>
|
|
// <serviceId>urn:plex-tv:serviceId:MediaGrabber</serviceId>
|
|
// </service>
|
|
// </serviceList>
|
|
// </device>
|
|
//
|
|
// </root>
|
|
// ```
|
|
//
|
|
// - UDN: (string) A UUID for the device. This should be unique across models of a device at minimum.
|
|
// - URLBase: (string) The base HTTP URL for the device from which all of the other endpoints are hosted.
|
|
type Devices struct {
|
|
rootSDK *PlexAPI
|
|
sdkConfiguration config.SDKConfiguration
|
|
hooks *hooks.Hooks
|
|
}
|
|
|
|
func newDevices(rootSDK *PlexAPI, sdkConfig config.SDKConfiguration, hooks *hooks.Hooks) *Devices {
|
|
return &Devices{
|
|
rootSDK: rootSDK,
|
|
sdkConfiguration: sdkConfig,
|
|
hooks: hooks,
|
|
}
|
|
}
|
|
|
|
// GetAvailableGrabbers - Get available grabbers
|
|
// Get available grabbers visible to the server
|
|
func (s *Devices) GetAvailableGrabbers(ctx context.Context, request operations.GetAvailableGrabbersRequest, opts ...operations.Option) (*operations.GetAvailableGrabbersResponse, error) {
|
|
globals := operations.GetAvailableGrabbersGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := url.JoinPath(baseURL, "/media/grabbers")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "getAvailableGrabbers",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, globals, 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
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.GetAvailableGrabbersResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
res.Headers = httpRes.Header
|
|
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetAvailableGrabbersResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// ListDevices - Get all devices
|
|
// Get the list of all devices present
|
|
func (s *Devices) ListDevices(ctx context.Context, opts ...operations.Option) (*operations.ListDevicesResponse, error) {
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := url.JoinPath(baseURL, "/media/grabbers/devices")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "listDevices",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.ListDevicesResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
res.Headers = httpRes.Header
|
|
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out components.MediaContainerWithDevice
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.MediaContainerWithDevice = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// AddDevice - Add a device
|
|
// This endpoint adds a device to an existing grabber. The device is identified, and added to the correct grabber.
|
|
func (s *Devices) AddDevice(ctx context.Context, request operations.AddDeviceRequest, opts ...operations.Option) (*operations.AddDeviceResponse, error) {
|
|
globals := operations.AddDeviceGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := url.JoinPath(baseURL, "/media/grabbers/devices")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "addDevice",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, globals, 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
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"400", "4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.AddDeviceResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out components.MediaContainerWithDevice
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.MediaContainerWithDevice = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// DiscoverDevices - Tell grabbers to discover devices
|
|
// Tell grabbers to discover devices
|
|
func (s *Devices) DiscoverDevices(ctx context.Context, opts ...operations.Option) (*operations.DiscoverDevicesResponse, error) {
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := url.JoinPath(baseURL, "/media/grabbers/devices/discover")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "discoverDevices",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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, 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
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.DiscoverDevicesResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out components.MediaContainerWithDevice
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.MediaContainerWithDevice = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// RemoveDevice - Remove a device
|
|
// Remove a devices by its id along with its channel mappings
|
|
func (s *Devices) RemoveDevice(ctx context.Context, request operations.RemoveDeviceRequest, opts ...operations.Option) (*operations.RemoveDeviceResponse, error) {
|
|
globals := operations.RemoveDeviceGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/media/grabbers/devices/{deviceId}", request, globals)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "removeDevice",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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, "DELETE", 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)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"404", "4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.RemoveDeviceResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
res.Headers = httpRes.Header
|
|
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.RemoveDeviceResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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 == 404:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetDeviceDetails - Get device details
|
|
// Get a device's details by its id
|
|
func (s *Devices) GetDeviceDetails(ctx context.Context, request operations.GetDeviceDetailsRequest, opts ...operations.Option) (*operations.GetDeviceDetailsResponse, error) {
|
|
globals := operations.GetDeviceDetailsGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/media/grabbers/devices/{deviceId}", request, globals)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "getDeviceDetails",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"404", "4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.GetDeviceDetailsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out components.MediaContainerWithDevice
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.MediaContainerWithDevice = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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 == 404:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// ModifyDevice - Enable or disable a device
|
|
// Enable or disable a device by its id
|
|
func (s *Devices) ModifyDevice(ctx context.Context, request operations.ModifyDeviceRequest, opts ...operations.Option) (*operations.ModifyDeviceResponse, error) {
|
|
globals := operations.ModifyDeviceGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/media/grabbers/devices/{deviceId}", request, globals)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "modifyDevice",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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, "PUT", 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)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, globals, 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
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"404", "4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.ModifyDeviceResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
res.Headers = httpRes.Header
|
|
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.ModifyDeviceResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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 == 404:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// SetChannelmap - Set a device's channel mapping
|
|
// Set a device's channel mapping
|
|
func (s *Devices) SetChannelmap(ctx context.Context, request operations.SetChannelmapRequest, opts ...operations.Option) (*operations.SetChannelmapResponse, error) {
|
|
globals := operations.SetChannelmapGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/media/grabbers/devices/{deviceId}/channelmap", request, globals)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "setChannelmap",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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, "PUT", 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)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, globals, 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
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.SetChannelmapResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out components.MediaContainerWithDevice
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.MediaContainerWithDevice = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetDevicesChannels - Get a device's channels
|
|
// Get a device's channels by its id
|
|
func (s *Devices) GetDevicesChannels(ctx context.Context, request operations.GetDevicesChannelsRequest, opts ...operations.Option) (*operations.GetDevicesChannelsResponse, error) {
|
|
globals := operations.GetDevicesChannelsGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/media/grabbers/devices/{deviceId}/channels", request, globals)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "getDevicesChannels",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"404", "4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.GetDevicesChannelsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
res.Headers = httpRes.Header
|
|
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out operations.GetDevicesChannelsResponseBody
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Object = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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 == 404:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// SetDevicePreferences - Set device preferences
|
|
// Set device preferences by its id
|
|
func (s *Devices) SetDevicePreferences(ctx context.Context, request operations.SetDevicePreferencesRequest, opts ...operations.Option) (*operations.SetDevicePreferencesResponse, error) {
|
|
globals := operations.SetDevicePreferencesGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/media/grabbers/devices/{deviceId}/prefs", request, globals)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "setDevicePreferences",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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, "PUT", opURL, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "*/*")
|
|
req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, globals, 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
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.SetDevicePreferencesResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// StopScan - Tell a device to stop scanning for channels
|
|
// Tell a device to stop scanning for channels
|
|
func (s *Devices) StopScan(ctx context.Context, request operations.StopScanRequest, opts ...operations.Option) (*operations.StopScanResponse, error) {
|
|
globals := operations.StopScanGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/media/grabbers/devices/{deviceId}/scan", request, globals)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "stopScan",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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, "DELETE", 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)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.StopScanResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out components.MediaContainerWithDevice
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.MediaContainerWithDevice = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// Scan - Tell a device to scan for channels
|
|
// Tell a device to scan for channels
|
|
func (s *Devices) Scan(ctx context.Context, request operations.ScanRequest, opts ...operations.Option) (*operations.ScanResponse, error) {
|
|
globals := operations.ScanGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/media/grabbers/devices/{deviceId}/scan", request, globals)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "scan",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, globals, 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
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.ScanResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
res.Headers = httpRes.Header
|
|
|
|
switch {
|
|
case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`):
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var out components.MediaContainerWithDevice
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.MediaContainerWithDevice = &out
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
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:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// GetThumb - Get device thumb
|
|
// Get a device's thumb for display to the user
|
|
func (s *Devices) GetThumb(ctx context.Context, request operations.GetThumbRequest, opts ...operations.Option) (*operations.GetThumbResponse, error) {
|
|
globals := operations.GetThumbGlobals{
|
|
Accepts: s.sdkConfiguration.Globals.Accepts,
|
|
ClientIdentifier: s.sdkConfiguration.Globals.ClientIdentifier,
|
|
Product: s.sdkConfiguration.Globals.Product,
|
|
Version: s.sdkConfiguration.Globals.Version,
|
|
Platform: s.sdkConfiguration.Globals.Platform,
|
|
PlatformVersion: s.sdkConfiguration.Globals.PlatformVersion,
|
|
Device: s.sdkConfiguration.Globals.Device,
|
|
Model: s.sdkConfiguration.Globals.Model,
|
|
DeviceVendor: s.sdkConfiguration.Globals.DeviceVendor,
|
|
DeviceName: s.sdkConfiguration.Globals.DeviceName,
|
|
Marketplace: s.sdkConfiguration.Globals.Marketplace,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
var baseURL string
|
|
if o.ServerURL == nil {
|
|
baseURL = utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
} else {
|
|
baseURL = *o.ServerURL
|
|
}
|
|
opURL, err := utils.GenerateURL(ctx, baseURL, "/media/grabbers/devices/{deviceId}/thumb/{version}", request, globals)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
hookCtx := hooks.HookContext{
|
|
SDK: s.rootSDK,
|
|
SDKConfiguration: s.sdkConfiguration,
|
|
BaseURL: baseURL,
|
|
Context: ctx,
|
|
OperationID: "getThumb",
|
|
OAuth2Scopes: nil,
|
|
SecuritySource: s.sdkConfiguration.Security,
|
|
}
|
|
|
|
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", "*/*")
|
|
req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent)
|
|
|
|
utils.PopulateHeaders(ctx, req, request, globals)
|
|
|
|
if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for k, v := range o.SetHeaders {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
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 && req.Body != http.NoBody && req.GetBody != nil {
|
|
copyBody, err := req.GetBody()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = copyBody
|
|
}
|
|
|
|
req, err = s.hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req)
|
|
if err != nil {
|
|
if retry.IsPermanentError(err) || retry.IsTemporaryError(err) {
|
|
return nil, err
|
|
}
|
|
|
|
return nil, retry.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
}
|
|
return httpRes, err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else {
|
|
req, err = s.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.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err)
|
|
return nil, err
|
|
} else if utils.MatchStatusCodes([]string{"404", "4XX", "5XX"}, httpRes.StatusCode) {
|
|
_httpRes, err := s.hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if _httpRes != nil {
|
|
httpRes = _httpRes
|
|
}
|
|
} else {
|
|
httpRes, err = s.hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
res := &operations.GetThumbResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: httpRes.Header.Get("Content-Type"),
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
case httpRes.StatusCode == 301:
|
|
case httpRes.StatusCode == 404:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
rawBody, err := utils.ConsumeRawBody(httpRes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|