/plexgo

An open source Plex Media Server Golang SDK

Primary LanguageGoMIT LicenseMIT

github.com/LukeHagar/plexgo

Summary

Plex-API: An Open API Spec for interacting with Plex.tv and Plex Media Server

Plex Media Server OpenAPI Specification

An Open Source OpenAPI Specification for Plex Media Server

Automation and SDKs provided by Speakeasy

Documentation

API Documentation

SDKs

The following SDKs are generated from the OpenAPI Specification. They are automatically generated and may not be fully tested. If you find any issues, please open an issue on the main specification Repository.

Language Repository Releases Other
Python GitHub PyPI -
JavaScript/TypeScript GitHub NPM \ JSR -
Go GitHub Releases GoDoc
Ruby GitHub Releases -
Swift GitHub Releases -
PHP GitHub Releases -
Java GitHub Releases -
C# GitHub Releases -

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"
	"log"
)

func main() {
	s := plexgo.New(
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"),
		plexgo.WithClientName("Plex for Roku"),
		plexgo.WithClientVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithDeviceNickname("Roku 3"),
	)

	ctx := context.Background()
	res, err := s.Server.GetServerCapabilities(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Available Resources and Operations

Available methods

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/retry"
	"log"
	"models/operations"
)

func main() {
	s := plexgo.New(
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"),
		plexgo.WithClientName("Plex for Roku"),
		plexgo.WithClientVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithDeviceNickname("Roku 3"),
	)

	ctx := context.Background()
	res, err := s.Server.GetServerCapabilities(ctx, 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.Object != 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/retry"
	"log"
)

func main() {
	s := plexgo.New(
		plexgo.WithRetryConfig(
			retry.Config{
				Strategy: "backoff",
				Backoff: &retry.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"),
		plexgo.WithClientName("Plex for Roku"),
		plexgo.WithClientVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithDeviceNickname("Roku 3"),
	)

	ctx := context.Background()
	res, err := s.Server.GetServerCapabilities(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

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 GetServerCapabilities function may return the following errors:

Error Type Status Code Content Type
sdkerrors.GetServerCapabilitiesBadRequest 400 application/json
sdkerrors.GetServerCapabilitiesUnauthorized 401 application/json
sdkerrors.SDKError 4XX, 5XX */*

Example

package main

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

func main() {
	s := plexgo.New(
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"),
		plexgo.WithClientName("Plex for Roku"),
		plexgo.WithClientVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithDeviceNickname("Roku 3"),
	)

	ctx := context.Background()
	res, err := s.Server.GetServerCapabilities(ctx)
	if err != nil {

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

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

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

Server Selection

Server Variables

The default server {protocol}://{ip}:{port} contains variables and is set to https://10.10.10.47:32400 by default. To override default values, the following options are available when initializing the SDK client instance:

  • WithProtocol(protocol ServerProtocol)
  • WithIP(ip string)
  • WithPort(port string)

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"
	"log"
)

func main() {
	s := plexgo.New(
		plexgo.WithServerURL("https://10.10.10.47:32400"),
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"),
		plexgo.WithClientName("Plex for Roku"),
		plexgo.WithClientVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithDeviceNickname("Roku 3"),
	)

	ctx := context.Background()
	res, err := s.Server.GetServerCapabilities(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Override Server URL Per-Operation

The server URL can also be overridden on a per-operation basis, provided a server list was specified for the operation. For example:

package main

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

func main() {
	s := plexgo.New(
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"),
		plexgo.WithClientName("Plex for Roku"),
		plexgo.WithClientVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithDeviceNickname("Roku 3"),
	)

	ctx := context.Background()
	res, err := s.Plex.GetCompanionsData(ctx, operations.WithServerURL("https://plex.tv/api/v2"))
	if err != nil {
		log.Fatal(err)
	}
	if res.ResponseBodies != 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/myorg/your-go-sdk"
)

var (
	httpClient = &http.Client{Timeout: 30 * time.Second}
	sdkClient  = sdk.New(sdk.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
AccessToken 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"
	"log"
)

func main() {
	s := plexgo.New(
		plexgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		plexgo.WithClientID("3381b62b-9ab7-4e37-827b-203e9809eb58"),
		plexgo.WithClientName("Plex for Roku"),
		plexgo.WithClientVersion("2.4.1"),
		plexgo.WithPlatform("Roku"),
		plexgo.WithDeviceNickname("Roku 3"),
	)

	ctx := context.Background()
	res, err := s.Server.GetServerCapabilities(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Special Types

This SDK defines the following custom types to assist with marshalling and unmarshalling data.

Date

types.Date is a wrapper around time.Time that allows for JSON marshaling a date string formatted as "2006-01-02".

Usage

d1 := types.NewDate(time.Now()) // returns *types.Date

d2 := types.DateFromTime(time.Now()) // returns types.Date

d3, err := types.NewDateFromString("2019-01-01") // returns *types.Date, error

d4, err := types.DateFromString("2019-01-01") // returns types.Date, error

d5 := types.MustNewDateFromString("2019-01-01") // returns *types.Date and panics on error

d6 := types.MustDateFromString("2019-01-01") // returns types.Date and panics on error

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