kraken

package
v0.0.0-...-51e4ec8 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: MIT Imports: 10 Imported by: 0

README

kraken

kraken is a Go client library for the Kraken API. The library provides an implementation of the Image Compressor interface for Kraken service. The library can be used to compress images using Kraken API.

Usage

package main

import (
	"context"
	"fmt"
	"net/http"
	"os"

	"github.com/moemoe89/go-helpers/imagecompressor/kraken"
)

func main() {
	// Create a kraken client using an API key.
	client, err := kraken.New(
		// Your API key.
		kraken.WithAPIKey("<API_KEY>"),
		// Your API secret.
		kraken.WithAPISecret("<API_SECRET>"),
		// Optionally customize the http.Client.
		// If not specify, will use http.DefaultClient.
		kraken.WithHTTPClient(
			&http.Client{
				Transport:     nil,
				CheckRedirect: nil,
				Jar:           nil,
				Timeout:       0,
			},
		),
	)
	if err != nil {
		panic(err)
	}

	// Open an image file for compression.
	file, err := os.Open("test.jpg")
	if err != nil {
		panic(err)
	}

	// Compress the image and get the compressed file.
	// filename is optional for Kraken,
	// leave it as empty string should be fine.
	compressedFile, err := client.Upload(context.Background(), file, "")
	if err != nil {
		panic(err)
	}

	// Print the URL of the compressed file.
	fmt.Println(compressedFile.URL)
}

Options

The New function accepts the following options:

  • WithAPIKey: sets the API key for kraken API.
  • WithAPISecret: sets the API secret for kraken API.
  • WithHTTPClient: sets the HTTPClient interface to make HTTP requests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) (imagecompressor.Client, error)

New returns Image Compressor interface implementations.

Types

type Data

type Data struct {
	Message        string `json:"message"`
	FileName       string `json:"file_name"`
	OriginalSize   int    `json:"original_size"`
	KrakedSize     int    `json:"kraked_size"`
	SavedBytes     int    `json:"saved_bytes"`
	KrakedURL      string `json:"kraked_url"`
	OriginalWidth  int    `json:"original_width"`
	OriginalHeight int    `json:"original_height"`
	KrakedWidth    int    `json:"kraked_width"`
	KrakedHeight   int    `json:"kraked_height"`
	Success        bool   `json:"success"`
}

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

type Option

type Option func(k *krakenClient) error

Option configures Kraken.

func WithAPIKey

func WithAPIKey(str string) Option

WithAPIKey returns an option that set the api key.

func WithAPISecret

func WithAPISecret(str string) Option

WithAPISecret returns an option that set the api secret.

func WithHTTPClient

func WithHTTPClient(httpClient HTTPClient) Option

WithHTTPClient returns an option that set the http client.

Jump to

Keyboard shortcuts

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