klaviyo

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 18 Imported by: 0

README

Go Klaviyo API Client

This project provides a Go client for interacting with the Klaviyo API. It's a partial implementation, tailored to specific features that were required. It's designed to be simple, efficient, and idiomatic.

Features

  • Profile Management: Fetch, create, update, and delete profiles.
  • Built-in retry mechanisms for better reliability.
  • Structured error handling.
  • Easily extendable for additional endpoints.

Installation

go get -u github.com/monetha/go-klaviyo

Usage

First, create a client:

import "github.com/monetha/go-klaviyo"

client := klaviyo.New(API_KEY, logger)
Fetch Profiles
profiles, err := client.GetProfiles(ctx)
Create Profile
newProfile := &profile.NewProfile{
    // populate your profile data
}
createdProfile, err := client.CreateProfile(ctx, newProfile)
Fetch Profile by ID
fetchedProfile, err := client.GetProfile(ctx, PROFILE_ID)
Update Profile
updates := []updater.Profile{
    // your updaters
}
updatedProfile, err := client.UpdateProfile(ctx, PROFILE_ID, updates...)
Handling Errors

All errors returned by the client are structured. You can inspect the error to get more details:

if errors.Is(err, klaviyo.ErrProfileAlreadyExists) {
    // Handle specific error
}

Contributing

Contributions are welcome! Please feel free to submit a pull request, report an issue, or suggest additional features.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidAPIKey indicates that the provided Klaviyo API key is either not specified or invalid.
	ErrInvalidAPIKey = errors.New("klaviyo: invalid or missing API key")

	// ErrTooManyRequests is returned by the client method when the endpoint is retried
	// the maximum number of times defined by defaultRetryMax and still fails.
	ErrTooManyRequests = errors.New("klaviyo: too many requests for calling endpoint")

	// ErrProfileDoesNotExist indicates that an attempt was made to retrieve a profile
	// that does not exist in Klaviyo.
	ErrProfileDoesNotExist = errors.New("klaviyo: a profile does not exist")
)

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Id     string `json:"id"`
	Status int    `json:"status"`
	Code   string `json:"code"`
	Title  string `json:"title"`
	Detail string `json:"detail"`
	Source struct {
		Pointer string `json:"pointer"`
	} `json:"source"`
	Meta struct {
		DuplicateProfileID string `json:"duplicate_profile_id,omitempty"`
	} `json:"meta,omitempty"`
}

APIError represents an error returned by the Klaviyo API.

func (*APIError) Error

func (e *APIError) Error() string

Error returns a human-readable representation of the APIError.

type BadHTTPResponseError

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

BadHTTPResponseError represents an error due to a bad HTTP response.

func (*BadHTTPResponseError) Body

func (e *BadHTTPResponseError) Body() []byte

Body returns the body of the HTTP response.

func (*BadHTTPResponseError) Cause

func (e *BadHTTPResponseError) Cause() error

Cause returns the underlying cause of the error.

func (*BadHTTPResponseError) Error

func (e *BadHTTPResponseError) Error() string

Error returns a human-readable representation of the BadHTTPResponseError.

func (*BadHTTPResponseError) StatusCode

func (e *BadHTTPResponseError) StatusCode() int

StatusCode returns the HTTP status code of the response.

func (*BadHTTPResponseError) Unwrap

func (e *BadHTTPResponseError) Unwrap() error

Unwrap provides compatibility for Go's errors.Is() and errors.As() functions.

type Client

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

Client represents a Klaviyo client with methods to interact with the Klaviyo API.

func New

func New(apiKey string, logger *zap.Logger) *Client

New initializes a new Klaviyo client with the default http client.

func NewWithClient

func NewWithClient(apiKey string, logger *zap.Logger, httpClient *http.Client) *Client

NewWithClient initializes a new Klaviyo client with a custom http client.

func (*Client) CreateEvent added in v0.0.5

func (c *Client) CreateEvent(ctx context.Context, e *event.NewEvent, ID string, metricName string) error

CreateEvent creates a new event in Klaviyo.

func (*Client) CreateProfile

func (c *Client) CreateProfile(ctx context.Context, p *profile.NewProfile) (*profile.ExistingProfile, error)

CreateProfile creates a new profile in Klaviyo. If a profile with the same identifiers already exists, it will return ErrProfileAlreadyExists.

func (*Client) GetEvents added in v0.0.5

func (c *Client) GetEvents(ctx context.Context, params ...getprofiles.Param) ([]*event.ExistingEvent, error)

GetEvents retrieves a list of created events from Klaviyo.

func (*Client) GetProfile

func (c *Client) GetProfile(ctx context.Context, profileID string) (*profile.ExistingProfile, error)

GetProfile retrieves a specific profile by its ID from Klaviyo. If the profile with the given ID does not exist, it will return ErrProfileDoesNotExist.

func (*Client) GetProfiles

func (c *Client) GetProfiles(ctx context.Context, params ...getprofiles.Param) ([]*profile.ExistingProfile, error)

GetProfiles retrieves a list of created profiles from Klaviyo.

func (*Client) UpdateProfile

func (c *Client) UpdateProfile(ctx context.Context, profileID string, updaters ...updater.Profile) (*profile.ExistingProfile, error)

UpdateProfile updates a specific profile by its ID in Klaviyo.

type ErrProfileAlreadyExists

type ErrProfileAlreadyExists struct {
	DuplicateProfileID string
}

ErrProfileAlreadyExists indicates that an attempt was made to create a profile that already exists in Klaviyo. It holds the ID of the duplicate profile.

func (*ErrProfileAlreadyExists) Error added in v0.0.2

func (e *ErrProfileAlreadyExists) Error() string

Error returns a string representation of the ErrProfileAlreadyExists error. It conforms to the error interface.

Directories

Path Synopsis
internal
log
models
operations

Jump to

Keyboard shortcuts

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