mailchimp

package module
v0.0.0-...-3140857 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2016 License: MPL-2.0 Imports: 12 Imported by: 0

README

Codeship Status for RichardKnop/go-mailchimp

Mailchimp

A Golang SDK for Mailchimp API v3.

Usage

package main

import (
	"log"

	mailchimp "github.com/nhocki/go-mailchimp"
)

func main() {
	client, err := mailchimp.NewClient("the_api_key-us13", nil)
	if err != nil {
		log.Fatal(err)
	}

	// Check if the email is already subscribed
	memberResponse, err := client.CheckSubscription(
		"listID",
		"john@reese.com",
	)

	// User is already subscribed, update the subscription
	if err == nil {
		memberResponse, err = client.UpdateSubscription(
			"listID",
			"john@reese.com",
			"subscribed",
			map[string]interface{}{},
		)

		if err != nil {
			// Check the error response
			errResponse, ok := err.(*mailchimp.ErrorResponse)

			// Could not type assert error response
			if !ok {
				log.Fatal(err)
			}

			log.Fatal(errResponse)
		}

		log.Printf(
			"%s's subscription has been updated. Status: %s",
			memberResponse.EmailAddress,
			memberResponse.Status,
		)
		return
	}

	if err != nil {
		// Check the error response
		errResponse, ok := err.(*mailchimp.ErrorResponse)

		// Could not type assert error response
		if !ok {
			log.Fatal(err)
		}

		// 404 means we can process and subscribe user,
		// error other than 404 means we return error
		if errResponse.Status != http.StatusNotFound {
			log.Fatal(errResponse)
		}
	}

	// Subscribe the email
	memberResponse, err = client.Subscribe(
		"listID",
		"john@reese.com",
		map[string]interface{}{},
	)

	if err != nil {
		// Check the error response
		errResponse, ok := err.(*mailchimp.ErrorResponse)

		// Could not type assert error response
		if !ok {
			log.Fatal(err)
		}

		log.Fatal(errResponse)
	}

	log.Printf(
		"%s has been subscribed successfully. Status: %s",
		memberResponse.EmailAddress,
		memberResponse.Status,
	)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client manages communication with the Mailchimp API.

func (*Client) CheckSubscription

func (c *Client) CheckSubscription(listID string, email string) (*MemberResponse, error)

CheckSubscription ...

func (*Client) GetBaseURL

func (c *Client) GetBaseURL() *url.URL

GetBaseURL ...

func (*Client) SetBaseURL

func (c *Client) SetBaseURL(baseURL *url.URL)

SetBaseURL ...

func (*Client) Subscribe

func (c *Client) Subscribe(listID string, email string, mergeFields map[string]interface{}) (*MemberResponse, error)

Subscribe ...

func (*Client) UpdateSubscription

func (c *Client) UpdateSubscription(listID string, email string, status string, mergeFields map[string]interface{}) (*MemberResponse, error)

UpdateSubscription ...

type ClientInterface

type ClientInterface interface {
	// Exported methods
	CheckSubscription(listID string, email string) (*MemberResponse, error)
	Subscribe(listID string, email string, mergeFields map[string]interface{}) (*MemberResponse, error)
	UpdateSubscription(listID string, email string, status string, mergeFields map[string]interface{}) (*MemberResponse, error)
	SetBaseURL(baseURL *url.URL)
	GetBaseURL() *url.URL
}

ClientInterface defines exported methods

func NewClient

func NewClient(apiKey string, httpClient *http.Client) (ClientInterface, error)

NewClient returns a new Mailchimp API client. If a nil httpClient is provided, http.DefaultClient will be used. The apiKey must be in the format xyz-us11.

type ClientMock

type ClientMock struct {
	mock.Mock
}

ClientMock is a mocked object implementing MailchimpInterface

func (*ClientMock) CheckSubscription

func (_m *ClientMock) CheckSubscription(listID string, email string) (*MemberResponse, error)

CheckSubscription ...

func (*ClientMock) GetBaseURL

func (_m *ClientMock) GetBaseURL() *url.URL

GetBaseURL ...

func (*ClientMock) SetBaseURL

func (_m *ClientMock) SetBaseURL(baseURL *url.URL)

SetBaseURL ...

func (*ClientMock) Subscribe

func (_m *ClientMock) Subscribe(listID string, email string, mergeFields map[string]interface{}) (*MemberResponse, error)

Subscribe ...

func (*ClientMock) UpdateSubscription

func (_m *ClientMock) UpdateSubscription(listID string, email string, status string, mergeFields map[string]interface{}) (*MemberResponse, error)

UpdateSubscription ...

type ErrorResponse

type ErrorResponse struct {
	Type   string `json:"type"`
	Title  string `json:"title"`
	Status int    `json:"status"`
	Detail string `json:"detail"`
}

ErrorResponse ...

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

Error ...

type MemberResponse

type MemberResponse struct {
	ID              string                 `json:"id"` // The MD5 hash of the list member's email address.
	EmailAddress    string                 `json:"email_address"`
	UniqueEmailID   string                 `json:"unique_email_id"` // An identifier for the address across all of MailChimp.
	EmailType       string                 `json:"email_type"`      // Type of email this member asked to get ('html' or 'text').
	Status          string                 `json:"status"`
	VIP             bool                   `json:"vip"`
	IPSignup        string                 `json:"ip_signup"`        // IP address the subscriber signed up from.
	TimestampSignup string                 `json:"timestamp_signup"` // Date and time the subscriber signed up for the list.
	IPOpt           string                 `json:"ip_opt"`           // IP address the subscriber confirmed their opt-in status.
	TimestampOpt    string                 `json:"timestamp_opt"`    // Date and time the subscribe confirmed their opt-in status.
	MemberRating    uint                   `json:"member_rating"`    // Star rating for this member between 1 and 5.
	LastChanged     string                 `json:"last_changed"`     // Date and time the member's info was last changed.
	ListID          string                 `json:"list_id"`          // The id for the list.
	MergeFields     map[string]interface{} `json:"merge_fields"`     // merge fields
}

MemberResponse - see https://api.mailchimp.com/schema/3.0/Lists/Members/Instance.json?_ga=1.216961300.323879299.1464708316

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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