tinypng

package
v5.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package tinypng provides a client implementation for the `tinypng.com` API.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnauthorized indicates that the provided API credentials are invalid.
	ErrUnauthorized = errors.New("unauthorized (invalid credentials)")

	// ErrTooManyRequests indicates that the API request limit has been exceeded.
	ErrTooManyRequests = errors.New("too many requests (limit exceeded)")

	// ErrBadRequest indicates an issue with the request, such as an empty file or an unsupported format.
	ErrBadRequest = errors.New("bad request (empty file or unsupported format)")
)

Functions

This section is empty.

Types

type Client

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

Client represents the TinyPNG API client.

func NewClient

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

NewClient creates a new TinyPNG client instance with the specified API key. Additional options can be provided to customize the client.

func (*Client) ApiKey

func (c *Client) ApiKey() string

ApiKey returns the API key used by the client.

func (*Client) Compress

func (c *Client) Compress(ctx context.Context, src io.Reader) (_ *Compressed, outErr error)

Compress uploads an image to TinyPNG for compression. The function returns metadata about the compressed image, but not the image content itself. If the provided source implements io.Closer, it will be closed automatically by the HTTP client.

func (*Client) UsedQuota

func (c *Client) UsedQuota(ctx context.Context) (_ uint64, outErr error)

UsedQuota retrieves the number of compression requests made using the current API key. Free-tier accounts are limited to 500 requests per month.

type ClientOption

type ClientOption func(*Client)

ClientOption is a functional option used to configure the Client instance.

func WithHTTPClient

func WithHTTPClient(httpClient httpClient) ClientOption

WithHTTPClient allows the use of a custom HTTP client implementation.

type ClientsPool

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

func NewClientsPool

func NewClientsPool(apiKeys []string, opts ...ClientOption) *ClientsPool

NewClientsPool initializes a new pool of clients using the given API keys. Additional options can be provided to customize the clients.

func (*ClientsPool) Get

func (p *ClientsPool) Get() (*Client, func(), bool)

Get retrieves a random client from the pool.

If the pool is empty, it returns nil and false as the last return value. If the client for a key is uninitialized, it creates a new one and returns it. The returned cleanup function should be called when the client is no longer needed, allowing the key to be removed from the pool.

type Compressed

type Compressed struct {
	UsedQuota     uint64 // Number of compressions used in the current billing period.
	Type          string // MIME type of the compressed image, e.g., "image/png".
	Size          uint64 // Size of the compressed image in bytes.
	URL           string // URL of the compressed image.
	Width, Height uint32 // Dimensions of the compressed image.
	// contains filtered or unexported fields
}

Compressed represents the result of a TinyPNG compression operation.

func (Compressed) Download

func (c Compressed) Download(ctx context.Context, to io.Writer, opt ...DownloadOption) (outErr error)

Download retrieves the compressed image from the TinyPNG servers and writes it to the specified destination. If the provided destination implements io.Closer, it will be closed automatically by the HTTP client.

type DownloadOption

type DownloadOption func(*downloadOptions)

func WithDownloadPreserveCopyright

func WithDownloadPreserveCopyright() DownloadOption

WithDownloadPreserveCopyright specifies that the copyright information should be preserved.

func WithDownloadPreserveCreation

func WithDownloadPreserveCreation() DownloadOption

WithDownloadPreserveCreation specifies that the creation date should be preserved.

func WithDownloadPreserveLocation

func WithDownloadPreserveLocation() DownloadOption

WithDownloadPreserveLocation specifies that the GPS location should be preserved.

Jump to

Keyboard shortcuts

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