Documentation ¶
Overview ¶
Package tinypng is `tinypng.com` API client implementation.
Index ¶
- Constants
- type Client
- func (c *Client) Compress(src io.Reader, dest io.Writer, timeouts ...time.Duration) (*CompressionResult, error)
- func (c *Client) CompressImage(src io.Reader, timeout ...time.Duration) (*CompressionResult, error)
- func (c *Client) CompressionCount(timeout ...time.Duration) (uint64, error)
- func (c *Client) DownloadImage(url string, dest io.Writer, timeout ...time.Duration) (int64, error)
- func (c *Client) SetAPIKey(key string)
- type ClientOption
- type CompressionResult
- type Error
Examples ¶
Constants ¶
const (
WithoutTimeout = time.Duration(0) // 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 ¶
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 ¶
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 ¶
DownloadImage from remote server and write to the passed destination. It returns the number of written bytes.
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
WithContext setups default HTTP request timeouts.
Example ¶
NewClient("YOUR-API-KEY", WithDefaultTimeout(time.Second*60))
Output:
func WithHTTPClient ¶
func WithHTTPClient(httpClient httpClient) ClientOption
WithContext 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 }