github.com/artpar/rclone@v1.67.3/backend/imagekit/client/client.go (about)

     1  // Package client provides a client for interacting with the ImageKit API.
     2  package client
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  
     8  	"github.com/artpar/rclone/fs"
     9  	"github.com/artpar/rclone/fs/fshttp"
    10  	"github.com/artpar/rclone/lib/rest"
    11  )
    12  
    13  // ImageKit main struct
    14  type ImageKit struct {
    15  	Prefix        string
    16  	UploadPrefix  string
    17  	Timeout       int64
    18  	UploadTimeout int64
    19  	PrivateKey    string
    20  	PublicKey     string
    21  	URLEndpoint   string
    22  	HTTPClient    *rest.Client
    23  }
    24  
    25  // NewParams is a struct to define parameters to imagekit
    26  type NewParams struct {
    27  	PrivateKey  string
    28  	PublicKey   string
    29  	URLEndpoint string
    30  }
    31  
    32  // New returns ImageKit object from environment variables
    33  func New(ctx context.Context, params NewParams) (*ImageKit, error) {
    34  
    35  	privateKey := params.PrivateKey
    36  	publicKey := params.PublicKey
    37  	endpointURL := params.URLEndpoint
    38  
    39  	switch {
    40  	case privateKey == "":
    41  		return nil, fmt.Errorf("ImageKit.io URL endpoint is required")
    42  	case publicKey == "":
    43  		return nil, fmt.Errorf("ImageKit.io public key is required")
    44  	case endpointURL == "":
    45  		return nil, fmt.Errorf("ImageKit.io private key is required")
    46  	}
    47  
    48  	cliCtx, cliCfg := fs.AddConfig(ctx)
    49  
    50  	cliCfg.UserAgent = "rclone/imagekit"
    51  	client := rest.NewClient(fshttp.NewClient(cliCtx))
    52  
    53  	client.SetUserPass(privateKey, "")
    54  	client.SetHeader("Accept", "application/json")
    55  
    56  	return &ImageKit{
    57  		Prefix:        "https://api.imagekit.io/v2",
    58  		UploadPrefix:  "https://upload.imagekit.io/api/v2",
    59  		Timeout:       60,
    60  		UploadTimeout: 3600,
    61  		PrivateKey:    params.PrivateKey,
    62  		PublicKey:     params.PublicKey,
    63  		URLEndpoint:   params.URLEndpoint,
    64  		HTTPClient:    client,
    65  	}, nil
    66  }