client

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2022 License: BSD-3-Clause Imports: 14 Imported by: 1

Documentation

Overview

Package client provides a common interface for accessing the Mastodon API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Post

func Post(ctx context.Context, cl Client, status string, visibility string, media ...string) (string, []string, error)

Post is a helper method to use 'cl' to post a message to Mastodon with 'post' and 'visibility' and zero or more media files. It returns the ID of the post and of the IDs of any media uploads. If you need to post a message with more options you should use the `Client.ExecuteMethod` method instead.

func RegisterClient

func RegisterClient(ctx context.Context, scheme string, f ClientInitializeFunc) error

Register a new URI scheme and ClientInitializeFunc function for a implementation of the Client interface.

func Schemes

func Schemes() []string

Return a list of URI schemes for registered implementations of the Client interface.

func Upload

func Upload(ctx context.Context, cl Client, media ...string) ([]string, error)

Upload is a helper method to use 'cl' to upload 'media' to a Mastodon server. It returns the media IDs of the the files that were uploaded. If you need to post a message with more options you should use the `Client.ExecuteMethod` method instead.

Types

type Client

type Client interface {
	// Execute a Mastodon API method.
	ExecuteMethod(context.Context, string, string, *url.Values) (io.ReadSeekCloser, error)
	// Upload an io.Reader instance using the Mastodon API.
	UploadMedia(context.Context, io.Reader, *url.Values) (io.ReadSeekCloser, error)
	// Assign a specific log.Logger instance for logging events.
	SetLogger(context.Context, *log.Logger) error
}

Client is the interface that defines common methods for all Mastodon API Client implementations. Currently there is only a single implementation that calls the Mastodon API using the OAuth2 authentication and authorization scheme but it is assumed that eventually there will be others.

func NewClient

func NewClient(ctx context.Context, uri string) (Client, error)

Create a new instance of the Client interface. Client instances are created by passing in a context.Context instance and a URI string. The form and substance of URI strings are specific to their implementations. For example to create a OAuth2Client you would write: cl, err := client.NewClient(ctx, "oauth2://:{ACCESS_TOKEN}@{MASTODON_HOST}")

func NewOAuth2Client

func NewOAuth2Client(ctx context.Context, uri string) (Client, error)

NewOAuth2Client returns a new `OAuth2Client` instance configured by 'uri' which is expected to take the form of:

oauth2://:{OAUTH2_ACCESS_TOKEN}@{MASTODON_HOST}

type ClientInitializeFunc

type ClientInitializeFunc func(context.Context, string) (Client, error)

The initialization function signature for implementation of the Client interface.

type OAuth2Client

type OAuth2Client struct {
	// contains filtered or unexported fields
}

OAuth2Client implements the `Client` interface using OAuth2 access tokens for authentication and authorization.

func (*OAuth2Client) ExecuteMethod

func (cl *OAuth2Client) ExecuteMethod(ctx context.Context, http_method string, api_method string, args *url.Values) (io.ReadSeekCloser, error)

ExecuteMethod will execute a Mastodon API method where 'api_method' is expected to be the relative URI for a given Mastodon API method.

func (*OAuth2Client) SetLogger

func (cl *OAuth2Client) SetLogger(ctx context.Context, logger *log.Logger) error

SetLogger assigns 'logger' to 'cl'

func (*OAuth2Client) UploadMedia

func (cl *OAuth2Client) UploadMedia(ctx context.Context, r io.Reader, args *url.Values) (io.ReadSeekCloser, error)

UploadMedia will upload the contents of 'r' as a media element using the Mastodon API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL