Skip to content

livepeer/livepeer-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Livepeer Go SDK

The Livepeer Go library provides convenient access to the Livepeer Studio API from applications written in Golang.

Documentation

For full documentation and examples, please visit docs.livepeer.org.

Installation

Install the package:

go get github.com/livepeer/livepeer-go

Usage

The library needs to be configured with your Livepeer Studio account's API key, which is available in the Studio Dashboard.

package main

import(
 "context"
 "log"
 livepeer "github.com/livepeer/livepeer-go"
 "github.com/livepeer/livepeer-go/models/components"
)

func main() {
    s := livepeer.New(
        livepeer.WithSecurity(""),
    )

    ctx := context.Background()
    res, err := s.Stream.GetAll(ctx)
    if err != nil {
        log.Fatal(err)
    }

    if res.Data != nil {
        // handle response
    }
}

Resources and Operations

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. When specified by the OpenAPI spec document, the SDK will return the appropriate subclass.

Error Object Status Code Content Type
sdkerrors.Error 404 application/json
sdkerrors.SDKError 4xx-5xx /

Example

package main

import (
 "context"
 "errors"
 livepeer "github.com/livepeer/livepeer-go"
 "github.com/livepeer/livepeer-go/models/components"
 "github.com/livepeer/livepeer-go/models/sdkerrors"
 "log"
)

func main() {
 s := livepeer.New(
  livepeer.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
 )

 var id string = "<value>"

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

  var e *sdkerrors.Error
  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())
  }
 }
}

Override Server URL Per-Client

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

package main

import (
 "context"
 livepeer "github.com/livepeer/livepeer-go"
 "github.com/livepeer/livepeer-go/models/components"
 "log"
)

func main() {
 s := livepeer.New(
  livepeer.WithServerURL("https://livepeer.studio/api"),
  livepeer.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
 )

 ctx := context.Background()
 res, err := s.PostStream(ctx, components.NewStreamPayload{
  Name:   "test_stream",
  Record: livepeer.Bool(false),
 })
 if err != nil {
  log.Fatal(err)
 }
 if res.Stream != 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.

About

Go library for the Livepeer API.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages