ucare

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package ucare provides the binding for the Uploadcare API.

import (
	"github.com/uploadcare/uploadcare-go/ucare"
	"github.com/uploadcare/uploadcare-go/file"
	"github.com/uploadcare/uploadcare-go/group"
	"github.com/uploadcare/uploadcare-go/upload"
	"github.com/uploadcare/uploadcare-go/conversion"
)

Construct a new Uploadcare client, then use the various domain services to access different parts of the Uploadcare API:

creds := ucare.APICreds{
	SecretKey: "your_secret_key",
	PublicKey: "your_public_key",
}

conf := &ucare.Config{
	SignBasedAuthentication: true,
}

client, err := ucare.NewClient(creds, conf)
if err != nil {
	// handle error
}

Getting a list of files:

// creating a file operations service
fileSvc := file.NewService(client)

listParams := file.ListParams{
	Stored:  ucare.String(true),
	OrderBy: ucare.String(file.OrderBySizeAsc),
}

fileList, err := fileSvc.List(context.Background(), listParams)
if err != nil {
	// handle error
}

// getting IDs for the first 100 files
ids := make([]string, 0, 100)
for fileList.Next() {
	finfo, err :=  fileList.ReadResult()
	if err != nil {
		// handle error
	}

	ids = append(ids, finfo.ID)
}

Acquiring file-specific info:

fileID := ids[0]
file, err := fileSvc.Info(context.Background(), fileID)
if err != nil {
	// handle error
}

if file.IsImage {
	h := file.ImageInfo.Height
	w := file.ImageInfo.Width
	fmt.Printf("image size: %dx%d\n", h, w)
}

Deleting a file

_, err := fileSvc.Delete(context.Background(), fileID)
if err != nil {
	// handle error
}

Storing a single file by ID:

_, err := fileSvc.Store(context.Background(), fileID)
if err != nil {
	// handle error
}

Getting a list of groups:

groupSvc := group.New(client)

listParams := file.ListParams{
	OrderBy:      ucare.String(group.OrderByCreatedAtAsc),
	Limit:        ucare.String(20),
}

groupList, err := groupSvc.List(context.Backgroud(), listParams)
if err != nil {
	// handle error
}

// getting group IDs
groupIDs = make([]string, 0, 100)
for groupList.Next() {
	groupList, err := groupList.ReadResult()
	if err != nil {
		// handle error
	}
	groupIDs = append(groupIDs, groupList.ID)
}

Getting a file group by ID:

groupID := groupIDs[0]
group, err := groupSvc.Info(context.Background(), groupID)
if err != nil {
	// handle error
}

fmt.Printf("group %s contains %d files\n", group.ID, group.FileCount)

Marking all files in a group as stored:

_, err := groupSvc.Store(context.Background(), groupID)
if err != nil {
	// handle error
}

Uploading a file

uploadSvc := upload.NewService(client)

file, err := os.Open("somefile.png")
if err != nil {
	// handle error
}

fileParams := upload.FileParams{
	Data:        file,
	Name:        file.Name(),
	ToStore:     ucare.String(upload.ToStoreTrue),
}

fileID, err := uploadSvc.File(context.Background(), fileParams)
if err != nil {
	// handle error
}

Index

Constants

View Source
const (
	APIv05 = "v0.5"
	APIv06 = "v0.6"
)

Public configuration constants

Variables

View Source
var (
	ErrInvalidAuthCreds = errors.New("Incorrect authentication credentials")
	ErrAuthForbidden    = errors.New("Simple authentication over HTTP is " +
		"forbidden. Please, use HTTPS or signed requests instead")
	ErrInvalidVersion = errors.New("This feature is not support. " +
		"Try to change the version (refer to " +
		"https://uploadcare.com/api-refs/rest-api/v0.6.0/ for " +
		"more information on what methods belongs to what version).")
	ErrFileTooLarge = errors.New("Direct uploads only support " +
		"files smaller than 100MB")
)

API response errors

Functions

func Bool

func Bool(v bool) *bool

Bool returns a pointer to the bool value passed in.

func BoolVal

func BoolVal(v *bool) bool

BoolVal returns the value of the bool pointer passed in or false if the pointer is nil.

func DisableLog

func DisableLog()

DisableLog does what you expect

func EnableLog

func EnableLog(lvl uclog.Level)

EnableLog enables package scoped logging

func Int64

func Int64(v int64) *int64

Int64 returns a pointer to the int value passed in

func Int64Val

func Int64Val(v *int64) int64

Int64Val returns the value of the int pointer passed in or 0 if the pointer is nil.

func String

func String(v string) *string

String returns a pointer to the string value passed in

func StringVal

func StringVal(v *string) string

StringVal returns the value of the string pointer passed in or "" if the pointer is nil.

func Time

func Time(t time.Time) *time.Time

Time returns a pointer to the time.Time value passed it

func Uint64

func Uint64(v uint64) *uint64

Uint64 returns a pointer to the int value passed in

func Uint64Val

func Uint64Val(v *uint64) uint64

Uint64Val returns the value of the int pointer passed in or 0 if the pointer is nil.

Types

type APICreds

type APICreds struct {
	SecretKey string
	PublicKey string
}

APICreds holds per project API credentials. You can find your credentials on the uploadcare dashboard.

type Client

type Client interface {
	NewRequest(
		ctx context.Context,
		endpoint config.Endpoint,
		method string,
		requrl string,
		data ReqEncoder,
	) (*http.Request, error)
	Do(req *http.Request, resdata interface{}) error
}

Client describes API client behaviour

func NewClient

func NewClient(creds APICreds, conf *Config) (Client, error)

NewClient initializes and configures new client for the high level API.

type Config

type Config struct {
	// HTTPClient allowes you to set custom http client for the calls
	HTTPClient *http.Client
	// APIVersion specifies REST API version to be used
	APIVersion string
	// SignBasedAuthentication should be true if you want to use
	// signed uploads and signature based authentication for the
	// REST API calls.
	SignBasedAuthentication bool
}

Config holds configuration for the client

type ReqEncoder

type ReqEncoder interface {
	EncodeReq(*http.Request) error
}

ReqEncoder exists to encode data into the prepared request. It may encode part of the data to the query string and other part into the request body. It may also set request headers for some payload types (multipart/form-data).

type UploadAPIAuthFunc

type UploadAPIAuthFunc func() (pubkey string, sign *string, exp *int64)

UploadAPIAuthFunc is for internal use and should not be used by users

Jump to

Keyboard shortcuts

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