tinypng

package
v3.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package tinypng is `tinypng.com` API client implementation.

Index

Examples

Constants

View Source
const WithoutTimeout = time.Duration(0)

WithoutTimeout is special value for timeouts disabling.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(apiKey string, options ...ClientOption) *Client

NewClient creates new tinypng client instance. Options can be used to fine client tuning.

Example
myContext := context.TODO()

NewClient("YOUR-API-KEY", WithContext(myContext), WithDefaultTimeout(time.Second*60))
Output:

func (*Client) Compress

func (c *Client) Compress(src io.Reader, dest io.Writer, timeouts ...time.Duration) (*CompressionResult, error)

Compress reads image from passed source and compress them on tinypng side. Compressed result will be wrote to the passed destination (additional information about compressed image will be returned too). You can use two timeouts - first for image uploading and response waiting, and second - for image downloading. If the provided src is also an io.Closer - it will be closed automatically by HTTP client (if default HTTP client is used).

Example
c := NewClient("YOUR-API-KEY")

srcFile, err := os.OpenFile("/tmp/image.png", os.O_RDONLY, 0)
if err != nil {
	panic(err)
}
defer srcFile.Close()

destFile, err := os.OpenFile("/tmp/image_compressed.png", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
	panic(err)
}
defer destFile.Close()

info, err := c.Compress(srcFile, destFile, time.Second*60, time.Second*30)
if err != nil {
	panic(err)
}

fmt.Printf("%+v\n", info)
Output:

func (*Client) CompressImage

func (c *Client) CompressImage(src io.Reader, timeout ...time.Duration) (*CompressionResult, error)

CompressImage uploads image content from passed source to the tinypng server for compression. When process is done - compression result (just information, not compressed image content) will be returned. If the provided src is also an io.Closer - it will be closed automatically by HTTP client (if default HTTP client is used).

Example
c := NewClient("YOUR-API-KEY")

srcFile, err := os.OpenFile("/tmp/image.png", os.O_RDONLY, 0)
if err != nil {
	panic(err)
}
defer srcFile.Close()

info, err := c.CompressImage(srcFile, time.Second*60)
if err != nil {
	panic(err)
}

fmt.Printf("%+v\n", info)
Output:

func (*Client) CompressionCount

func (c *Client) CompressionCount(timeout ...time.Duration) (uint64, error)

CompressionCount returns compressions count for current API key (used quota value). By default, for free API keys quota is equals to 500.

func (*Client) DownloadImage

func (c *Client) DownloadImage(url string, dest io.Writer, timeout ...time.Duration) (int64, error)

DownloadImage from remote server and write to the passed destination. It returns the number of written bytes.

func (*Client) SetAPIKey

func (c *Client) SetAPIKey(key string)

SetAPIKey sets API key for requests making.

Example
c := NewClient("WRONG-KEY")

c.SetAPIKey("CORRECT-KEY")
Output:

type ClientOption

type ClientOption func(*Client)

ClientOption allows to setup some internal client properties from outside.

func WithContext

func WithContext(ctx context.Context) ClientOption

WithContext setups client context.

Example
NewClient("YOUR-API-KEY", WithContext(context.TODO()))
Output:

func WithDefaultTimeout

func WithDefaultTimeout(timeout time.Duration) ClientOption

WithDefaultTimeout setups default HTTP request timeouts.

Example
NewClient("YOUR-API-KEY", WithDefaultTimeout(time.Second*60))
Output:

func WithHTTPClient

func WithHTTPClient(httpClient httpClient) ClientOption

WithHTTPClient setups allows to pass custom HTTP client implementation.

Example
NewClient("YOUR-API-KEY", WithHTTPClient(&http.Client{Timeout: time.Second * 5}))
Output:

type CompressionResult

type CompressionResult struct {
	Input            compressionInput  `json:"input"`
	Output           compressionOutput `json:"output"`
	CompressionCount uint64            // used quota value
}

type Error

type Error uint8

Special type for package-specific errors.

const (
	ErrTooManyRequests Error = iota + 1 // too many requests (limit has been exceeded)
	ErrUnauthorized                     // unauthorized (invalid credentials)
	ErrBadRequest                       // bad request (empty file or wrong format)
)

Package-specific error constants.

func (Error) Error

func (err Error) Error() string

Error returns error in a string representation.

Jump to

Keyboard shortcuts

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