Skip to content

LukeHagar/plexgo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

github.com/LukeHagar/plexgo

Summary

Table of Contents

SDK Installation

To add the SDK as a dependency to your project:

go get github.com/LukeHagar/plexgo

SDK Example Usage

Example

package main

import (
	"context"
	"github.com/LukeHagar/plexgo"
	"github.com/LukeHagar/plexgo/models/components"
	"github.com/LukeHagar/plexgo/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := plexgo.New(
		plexgo.WithAccepts(components.AcceptsApplicationXML),
		plexgo.WithClientIdentifier("abc123"),
		plexgo.WithProduct("Plex for Roku"),
		plexgo.WithVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithPlatformVersion("4.3 build 1057"),
		plexgo.WithDevice("Roku 3"),
		plexgo.WithModel("4200X"),
		plexgo.WithDeviceVendor("Roku"),
		plexgo.WithDeviceName("Living Room TV"),
		plexgo.WithMarketplace("googlePlay"),
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.Transcoder.StartTranscodeSession(ctx, operations.StartTranscodeSessionRequest{
		TranscodeType:             components.TranscodeTypeMusic,
		Extension:                 operations.ExtensionMpd,
		AdvancedSubtitles:         components.AdvancedSubtitlesBurn.ToPointer(),
		AudioBoost:                plexgo.Pointer[int64](50),
		AudioChannelCount:         plexgo.Pointer[int64](5),
		AutoAdjustQuality:         components.BoolIntOne.ToPointer(),
		AutoAdjustSubtitle:        components.BoolIntOne.ToPointer(),
		DirectPlay:                components.BoolIntOne.ToPointer(),
		DirectStream:              components.BoolIntOne.ToPointer(),
		DirectStreamAudio:         components.BoolIntOne.ToPointer(),
		DisableResolutionRotation: components.BoolIntOne.ToPointer(),
		HasMDE:                    components.BoolIntOne.ToPointer(),
		Location:                  operations.StartTranscodeSessionQueryParamLocationWan.ToPointer(),
		MediaBufferSize:           plexgo.Pointer[int64](102400),
		MediaIndex:                plexgo.Pointer[int64](0),
		MusicBitrate:              plexgo.Pointer[int64](5000),
		Offset:                    plexgo.Pointer[float64](90.5),
		PartIndex:                 plexgo.Pointer[int64](0),
		Path:                      plexgo.Pointer("/library/metadata/151671"),
		PeakBitrate:               plexgo.Pointer[int64](12000),
		PhotoResolution:           plexgo.Pointer("1080x1080"),
		Protocol:                  operations.StartTranscodeSessionQueryParamProtocolDash.ToPointer(),
		SecondsPerSegment:         plexgo.Pointer[int64](5),
		SubtitleSize:              plexgo.Pointer[int64](50),
		VideoBitrate:              plexgo.Pointer[int64](12000),
		VideoQuality:              plexgo.Pointer[int64](50),
		VideoResolution:           plexgo.Pointer("1080x1080"),
		XPlexClientProfileExtra:   plexgo.Pointer("add-limitation(scope=videoCodec&scopeName=*&type=upperBound&name=video.frameRate&value=60&replace=true)+append-transcode-target-codec(type=videoProfile&context=streaming&videoCodec=h264%2Chevc&audioCodec=aac&protocol=dash)"),
		XPlexClientProfileName:    plexgo.Pointer("generic"),
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.ResponseStream != nil {
		// handle response
	}
}

Available Resources and Operations

Available methods
  • WriteLog - Logging a multi-line message to the Plex Media Server log
  • WriteMessage - Logging a single-line message to the Plex Media Server log
  • EnablePapertrail - Enabling Papertrail

Retries

Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.

To change the default retry strategy for a single API call, simply provide a retry.Config object to the call by using the WithRetries option:

package main

import (
	"context"
	"github.com/LukeHagar/plexgo"
	"github.com/LukeHagar/plexgo/models/components"
	"github.com/LukeHagar/plexgo/models/operations"
	"github.com/LukeHagar/plexgo/retry"
	"log"
	"models/operations"
)

func main() {
	ctx := context.Background()

	s := plexgo.New(
		plexgo.WithAccepts(components.AcceptsApplicationXML),
		plexgo.WithClientIdentifier("abc123"),
		plexgo.WithProduct("Plex for Roku"),
		plexgo.WithVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithPlatformVersion("4.3 build 1057"),
		plexgo.WithDevice("Roku 3"),
		plexgo.WithModel("4200X"),
		plexgo.WithDeviceVendor("Roku"),
		plexgo.WithDeviceName("Living Room TV"),
		plexgo.WithMarketplace("googlePlay"),
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.General.GetServerInfo(ctx, operations.GetServerInfoRequest{}, operations.WithRetries(
		retry.Config{
			Strategy: "backoff",
			Backoff: &retry.BackoffStrategy{
				InitialInterval: 1,
				MaxInterval:     50,
				Exponent:        1.1,
				MaxElapsedTime:  100,
			},
			RetryConnectionErrors: false,
		}))
	if err != nil {
		log.Fatal(err)
	}
	if res.MediaContainerWithDirectory != nil {
		// handle response
	}
}

If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:

package main

import (
	"context"
	"github.com/LukeHagar/plexgo"
	"github.com/LukeHagar/plexgo/models/components"
	"github.com/LukeHagar/plexgo/models/operations"
	"github.com/LukeHagar/plexgo/retry"
	"log"
)

func main() {
	ctx := context.Background()

	s := plexgo.New(
		plexgo.WithRetryConfig(
			retry.Config{
				Strategy: "backoff",
				Backoff: &retry.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		plexgo.WithAccepts(components.AcceptsApplicationXML),
		plexgo.WithClientIdentifier("abc123"),
		plexgo.WithProduct("Plex for Roku"),
		plexgo.WithVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithPlatformVersion("4.3 build 1057"),
		plexgo.WithDevice("Roku 3"),
		plexgo.WithModel("4200X"),
		plexgo.WithDeviceVendor("Roku"),
		plexgo.WithDeviceName("Living Room TV"),
		plexgo.WithMarketplace("googlePlay"),
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.General.GetServerInfo(ctx, operations.GetServerInfoRequest{})
	if err != nil {
		log.Fatal(err)
	}
	if res.MediaContainerWithDirectory != nil {
		// handle response
	}
}

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both.

By Default, an API error will return sdkerrors.SDKError. When custom error responses are specified for an operation, the SDK may also return their associated error. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation.

For example, the GetServerInfo function may return the following errors:

Error Type Status Code Content Type
sdkerrors.SDKError 4XX, 5XX */*

Example

package main

import (
	"context"
	"errors"
	"github.com/LukeHagar/plexgo"
	"github.com/LukeHagar/plexgo/models/components"
	"github.com/LukeHagar/plexgo/models/operations"
	"github.com/LukeHagar/plexgo/models/sdkerrors"
	"log"
)

func main() {
	ctx := context.Background()

	s := plexgo.New(
		plexgo.WithAccepts(components.AcceptsApplicationXML),
		plexgo.WithClientIdentifier("abc123"),
		plexgo.WithProduct("Plex for Roku"),
		plexgo.WithVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithPlatformVersion("4.3 build 1057"),
		plexgo.WithDevice("Roku 3"),
		plexgo.WithModel("4200X"),
		plexgo.WithDeviceVendor("Roku"),
		plexgo.WithDeviceName("Living Room TV"),
		plexgo.WithMarketplace("googlePlay"),
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.General.GetServerInfo(ctx, operations.GetServerInfoRequest{})
	if err != nil {

		var e *sdkerrors.SDKError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}
	}
}

Server Selection

Select Server by Index

You can override the default server globally using the WithServerIndex(serverIndex int) option when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:

# Server Variables Description
0 https://{IP-description}.{identifier}.plex.direct:{port} identifier
IP-description
port
1 {protocol}://{host}:{port} protocol
host
port
2 https://{full_server_url} server_url

If the selected server has variables, you may override its default values using the associated option(s):

Variable Option Default Description
identifier WithIdentifier(identifier string) "0123456789abcdef0123456789abcdef" The unique identifier of this particular PMS
IP-description WithIPDescription(ipDescription string) "1-2-3-4" A - separated string of the IPv4 or IPv6 address components
port WithPort(port string) "32400" The Port number configured on the PMS. Typically (32400).
If using a reverse proxy, this would be the port number configured on the proxy.
protocol WithProtocol(protocol string) "http" The network protocol to use. Typically (http or https)
host WithHost(host string) "localhost" The Host of the PMS.
If using on a local network, this is the internal IP address of the server hosting the PMS.
If using on an external network, this is the external IP address for your network, and requires port forwarding.
If using a reverse proxy, this would be the external DNS domain for your network, and requires the proxy handle port forwarding.
server_url WithGlobalServerURL(serverURL string) "http://localhost:32400" The full manual URL to access the PMS

Example

package main

import (
	"context"
	"github.com/LukeHagar/plexgo"
	"github.com/LukeHagar/plexgo/models/components"
	"github.com/LukeHagar/plexgo/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := plexgo.New(
		plexgo.WithServerIndex(1),
		plexgo.WithProtocol("<value>"),
		plexgo.WithHost("electric-excess.name"),
		plexgo.WithPort("36393"),
		plexgo.WithAccepts(components.AcceptsApplicationXML),
		plexgo.WithClientIdentifier("abc123"),
		plexgo.WithProduct("Plex for Roku"),
		plexgo.WithVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithPlatformVersion("4.3 build 1057"),
		plexgo.WithDevice("Roku 3"),
		plexgo.WithModel("4200X"),
		plexgo.WithDeviceVendor("Roku"),
		plexgo.WithDeviceName("Living Room TV"),
		plexgo.WithMarketplace("googlePlay"),
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.General.GetServerInfo(ctx, operations.GetServerInfoRequest{})
	if err != nil {
		log.Fatal(err)
	}
	if res.MediaContainerWithDirectory != nil {
		// handle response
	}
}

Override Server URL Per-Client

The default server can also be overridden globally using the WithServerURL(serverURL string) option when initializing the SDK client instance. For example:

package main

import (
	"context"
	"github.com/LukeHagar/plexgo"
	"github.com/LukeHagar/plexgo/models/components"
	"github.com/LukeHagar/plexgo/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := plexgo.New(
		plexgo.WithServerURL("https://{full_server_url}"),
		plexgo.WithAccepts(components.AcceptsApplicationXML),
		plexgo.WithClientIdentifier("abc123"),
		plexgo.WithProduct("Plex for Roku"),
		plexgo.WithVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithPlatformVersion("4.3 build 1057"),
		plexgo.WithDevice("Roku 3"),
		plexgo.WithModel("4200X"),
		plexgo.WithDeviceVendor("Roku"),
		plexgo.WithDeviceName("Living Room TV"),
		plexgo.WithMarketplace("googlePlay"),
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
	)

	res, err := s.General.GetServerInfo(ctx, operations.GetServerInfoRequest{})
	if err != nil {
		log.Fatal(err)
	}
	if res.MediaContainerWithDirectory != nil {
		// handle response
	}
}

Custom HTTP Client

The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.

import (
	"net/http"
	"time"

	"github.com/LukeHagar/plexgo"
)

var (
	httpClient = &http.Client{Timeout: 30 * time.Second}
	sdkClient  = plexgo.New(plexgo.WithClient(httpClient))
)

This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
Token apiKey API key

You can configure it using the WithSecurity option when initializing the SDK client instance. For example:

package main

import (
	"context"
	"github.com/LukeHagar/plexgo"
	"github.com/LukeHagar/plexgo/models/components"
	"github.com/LukeHagar/plexgo/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := plexgo.New(
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		plexgo.WithAccepts(components.AcceptsApplicationXML),
		plexgo.WithClientIdentifier("abc123"),
		plexgo.WithProduct("Plex for Roku"),
		plexgo.WithVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithPlatformVersion("4.3 build 1057"),
		plexgo.WithDevice("Roku 3"),
		plexgo.WithModel("4200X"),
		plexgo.WithDeviceVendor("Roku"),
		plexgo.WithDeviceName("Living Room TV"),
		plexgo.WithMarketplace("googlePlay"),
	)

	res, err := s.General.GetServerInfo(ctx, operations.GetServerInfoRequest{})
	if err != nil {
		log.Fatal(err)
	}
	if res.MediaContainerWithDirectory != nil {
		// handle response
	}
}

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Feel free to open a PR or a Github issue as a proof of concept and we'll do our best to include it in a future release!

SDK Created by Speakeasy

About

An open source Plex Media Server Golang SDK

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages