inbucket: github.com/inbucket/inbucket/pkg/rest/client Index | Examples | Files

package client

import "github.com/inbucket/inbucket/pkg/rest/client"

Package client provides a basic REST client for Inbucket

Example demonstrates basic usage for the Inbucket REST client.

Code:

package main

import (
    "fmt"
    "log"
    "net/http"
    "net/http/httptest"

    "github.com/gorilla/mux"
    "github.com/inbucket/inbucket/pkg/rest/client"
)

// Example demonstrates basic usage for the Inbucket REST client.
func main() {
    // Setup a fake Inbucket server for this example.
    baseURL, teardown := exampleSetup()
    defer teardown()

    // Begin by creating a new client using the base URL of your Inbucket server, i.e.
    // `localhost:9000`.
    restClient, err := client.New(baseURL)
    if err != nil {
        log.Fatal(err)
    }

    // Get a slice of message headers for the mailbox named `user1`.
    headers, err := restClient.ListMailbox("user1")
    if err != nil {
        log.Fatal(err)
    }
    for _, header := range headers {
        fmt.Printf("ID: %v, Subject: %v\n", header.ID, header.Subject)
    }

    // Get the content of the first message.
    message, err := headers[0].GetMessage()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("\nFrom: %v\n", message.From)
    fmt.Printf("Text body:\n%v", message.Body.Text)

    // Delete the second message.
    err = headers[1].Delete()
    if err != nil {
        log.Fatal(err)
    }

}

// exampleSetup creates a fake Inbucket server to power Example() below.
func exampleSetup() (baseURL string, teardown func()) {
    router := mux.NewRouter()
    server := httptest.NewServer(router)

    // Handle ListMailbox request.
    router.HandleFunc("/api/v1/mailbox/user1", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte(`[
			{
				"mailbox": "user1",
				"id": "20180107T224128-0000",
				"subject": "First subject"
			},
			{
				"mailbox": "user1",
				"id": "20180108T121212-0123",
				"subject": "Second subject"
			}
		]`))
    })

    // Handle GetMessage request.
    router.HandleFunc("/api/v1/mailbox/user1/20180107T224128-0000",
        func(w http.ResponseWriter, r *http.Request) {
            w.Write([]byte(`{
				"mailbox": "user1",
				"id": "20180107T224128-0000",
				"from": "admin@inbucket.org",
				"subject": "First subject",
				"body": {
					"text": "This is the plain text body"
				}
			}`))
        })

    // Handle Delete request.
    router.HandleFunc("/api/v1/mailbox/user1/20180108T121212-0123",
        func(w http.ResponseWriter, r *http.Request) {
            // Nop.
        })

    return server.URL, func() {
        server.Close()
    }
}

Index

Examples

Package Files

apiv1_client.go rest.go

type Client Uses

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

Client accesses the Inbucket REST API v1

func New Uses

func New(baseURL string) (*Client, error)

New creates a new v1 REST API client given the base URL of an Inbucket server, ex: "http://localhost:9000"

func (*Client) DeleteMessage Uses

func (c *Client) DeleteMessage(name, id string) error

DeleteMessage deletes a single message given the mailbox name and message ID.

func (*Client) GetMessage Uses

func (c *Client) GetMessage(name, id string) (message *Message, err error)

GetMessage returns the message details given a mailbox name and message ID.

func (*Client) GetMessageSource Uses

func (c *Client) GetMessageSource(name, id string) (*bytes.Buffer, error)

GetMessageSource returns the message source given a mailbox name and message ID.

func (*Client) ListMailbox Uses

func (c *Client) ListMailbox(name string) (headers []*MessageHeader, err error)

ListMailbox returns a list of messages for the requested mailbox

func (*Client) MarkSeen Uses

func (c *Client) MarkSeen(name, id string) error

MarkSeen marks the specified message as having been read.

func (*Client) PurgeMailbox Uses

func (c *Client) PurgeMailbox(name string) error

PurgeMailbox deletes all messages in the given mailbox

type Message Uses

type Message struct {
    *model.JSONMessageV1
    // contains filtered or unexported fields
}

Message represents an Inbucket message including content

func (*Message) Delete Uses

func (m *Message) Delete() error

Delete deletes this message from the mailbox

func (*Message) GetSource Uses

func (m *Message) GetSource() (*bytes.Buffer, error)

GetSource returns the source for this message

type MessageHeader Uses

type MessageHeader struct {
    *model.JSONMessageHeaderV1
    // contains filtered or unexported fields
}

MessageHeader represents an Inbucket message sans content

func (*MessageHeader) Delete Uses

func (h *MessageHeader) Delete() error

Delete deletes this message from the mailbox

func (*MessageHeader) GetMessage Uses

func (h *MessageHeader) GetMessage() (message *Message, err error)

GetMessage returns this message with content

func (*MessageHeader) GetSource Uses

func (h *MessageHeader) GetSource() (*bytes.Buffer, error)

GetSource returns the source for this message

Package client imports 8 packages (graph). Updated 2019-03-16. Refresh now. Tools for package owners.