handlers

package
v1.2.160 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2019 License: AGPL-3.0 Imports: 23 Imported by: 77

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeAndValidateForm

func DecodeAndValidateForm(form interface{}, r *http.Request) error

DecodeAndValidateForm takes the passed in form and attempts to parse and validate it from the URL query parameters as well as any POST parameters of the passed in request

func DecodeAndValidateJSON

func DecodeAndValidateJSON(envelope interface{}, r *http.Request) error

DecodeAndValidateJSON takes the passed in envelope and tries to unmarshal it from the body of the passed in request, then validating it

func DecodeAndValidateXML added in v1.1.12

func DecodeAndValidateXML(envelope interface{}, r *http.Request) error

DecodeAndValidateXML takes the passed in envelope and tries to unmarshal it from the body of the passed in request, then validating it

func DecodePossibleBase64

func DecodePossibleBase64(original string) string

DecodePossibleBase64 detects and decodes a possibly base64 encoded messages by doing:

  • check it's at least 60 characters
  • check its length is divisible by 4
  • check that there's no whitespace
  • check the decoded string contains at least 50% ascii

func GetTextAndAttachments added in v1.2.24

func GetTextAndAttachments(m courier.Msg) string

GetTextAndAttachments returns both the text of our message as well as any attachments, newline delimited

func NameFromFirstLastUsername

func NameFromFirstLastUsername(first string, last string, username string) string

NameFromFirstLastUsername is a utility function to build a contact's name from the passed in values, all of which can be empty

func NewExternalIDStatusHandler added in v1.2.29

func NewExternalIDStatusHandler(h *BaseHandler, statuses map[string]courier.MsgStatusValue, externalIDField string, statusField string) courier.ChannelHandleFunc

NewExternalIDStatusHandler creates a new status handler given the passed in status map and fields

func NewTelReceiveHandler added in v1.2.29

func NewTelReceiveHandler(h *BaseHandler, fromField string, bodyField string) courier.ChannelHandleFunc

NewTelReceiveHandler creates a new receive handler given the passed in text and from fields

func RunChannelBenchmarks

func RunChannelBenchmarks(b *testing.B, channels []courier.Channel, handler courier.ChannelHandler, testCases []ChannelHandleTestCase)

RunChannelBenchmarks runs all the passed in test cases for the passed in channels

func RunChannelSendTestCases added in v0.2.0

func RunChannelSendTestCases(t *testing.T, channel courier.Channel, handler courier.ChannelHandler, testCases []ChannelSendTestCase, setupBackend func(*courier.MockBackend))

RunChannelSendTestCases runs all the passed in test cases against the channel

func RunChannelTestCases

func RunChannelTestCases(t *testing.T, channels []courier.Channel, handler courier.ChannelHandler, testCases []ChannelHandleTestCase)

RunChannelTestCases runs all the passed in tests cases for the passed in channel configurations

func Sp

func Sp(str interface{}) *string

Sp is a utility method to get the pointer to the passed in string

func SplitAttachment added in v1.2.24

func SplitAttachment(attachment string) (string, string)

SplitAttachment takes an attachment string and returns the media type and URL for the attachment

func SplitMsg added in v1.0.1

func SplitMsg(text string, max int) []string

SplitMsg splits the passed in string into segments that are at most max length

func StrictTelForCountry added in v1.2.98

func StrictTelForCountry(number string, country string) (urns.URN, error)

StrictTelForCountry wraps urns.NewURNTelForCountry but is stricter in what it accepts. Incoming tels must be numeric or we will return an error. (IE, alphanumeric shortcodes are not ok)

func Tp

func Tp(tm time.Time) *time.Time

Tp is utility method to get the pointer to the passed in time

func Validate added in v0.2.0

func Validate(form interface{}) error

Validate validates the passe din struct using our shared validator instance

func WriteAndLogRequestError added in v1.2.98

func WriteAndLogRequestError(ctx context.Context, h ResponseWriter, channel courier.Channel, w http.ResponseWriter, r *http.Request, err error) error

WriteAndLogRequestError logs the passed in error and writes the response to the response writer

func WriteAndLogRequestIgnored added in v1.2.98

func WriteAndLogRequestIgnored(ctx context.Context, h ResponseWriter, channel courier.Channel, w http.ResponseWriter, r *http.Request, details string) error

WriteAndLogRequestIgnored logs that the passed in request was ignored and writes the response to the response writer

func WriteMsgStatusAndResponse added in v1.2.98

func WriteMsgStatusAndResponse(ctx context.Context, h ResponseWriter, channel courier.Channel, status courier.MsgStatus, w http.ResponseWriter, r *http.Request) ([]courier.Event, error)

WriteMsgStatusAndResponse write the passed in status to our backend

func WriteMsgsAndResponse added in v1.2.98

func WriteMsgsAndResponse(ctx context.Context, h ResponseWriter, msgs []courier.Msg, w http.ResponseWriter, r *http.Request) ([]courier.Event, error)

WriteMsgsAndResponse writes the passed in message to our backend

Types

type BaseHandler

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

BaseHandler is the base class for most handlers, it just stored the server, name and channel type for the handler

func NewBaseHandler

func NewBaseHandler(channelType courier.ChannelType, name string) BaseHandler

NewBaseHandler returns a newly constructed BaseHandler with the passed in parameters

func (*BaseHandler) Backend added in v0.2.0

func (h *BaseHandler) Backend() courier.Backend

Backend returns the backend instance on the BaseHandler

func (*BaseHandler) ChannelName

func (h *BaseHandler) ChannelName() string

ChannelName returns the name of the channel this handler deals with

func (*BaseHandler) ChannelType

func (h *BaseHandler) ChannelType() courier.ChannelType

ChannelType returns the channel type that this handler deals with

func (*BaseHandler) Server

func (h *BaseHandler) Server() courier.Server

Server returns the server instance on the BaseHandler

func (*BaseHandler) SetServer

func (h *BaseHandler) SetServer(server courier.Server)

SetServer can be used to change the server on a BaseHandler

func (*BaseHandler) WriteMsgSuccessResponse added in v1.2.98

func (h *BaseHandler) WriteMsgSuccessResponse(ctx context.Context, w http.ResponseWriter, r *http.Request, msgs []courier.Msg) error

WriteMsgSuccessResponse writes a success response for the messages

func (*BaseHandler) WriteRequestError added in v1.2.98

func (h *BaseHandler) WriteRequestError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) error

WriteRequestError writes the passed in error to our response writer

func (*BaseHandler) WriteRequestIgnored added in v1.2.98

func (h *BaseHandler) WriteRequestIgnored(ctx context.Context, w http.ResponseWriter, r *http.Request, details string) error

WriteRequestIgnored writes an ignored payload to our response writer

func (*BaseHandler) WriteStatusSuccessResponse added in v1.2.98

func (h *BaseHandler) WriteStatusSuccessResponse(ctx context.Context, w http.ResponseWriter, r *http.Request, statuses []courier.MsgStatus) error

WriteStatusSuccessResponse writes a success response for the statuses

type ChannelHandleTestCase added in v0.2.0

type ChannelHandleTestCase struct {
	Label string

	URL      string
	Data     string
	Status   int
	Response string
	Headers  map[string]string

	Name        *string
	Text        *string
	URN         *string
	URNAuth     *string
	Attachment  *string
	Attachments []string
	Date        *time.Time

	MsgStatus *string

	ChannelEvent      *string
	ChannelEventExtra map[string]interface{}

	ExternalID *string
	ID         int64

	PrepRequest RequestPrepFunc
}

ChannelHandleTestCase defines the test values for a particular test case

type ChannelSendTestCase added in v0.2.0

type ChannelSendTestCase struct {
	Label string

	Text                 string
	URN                  string
	URNAuth              string
	Attachments          []string
	QuickReplies         []string
	HighPriority         bool
	ResponseToID         int64
	ResponseToExternalID string
	Metadata             json.RawMessage

	ResponseStatus int
	ResponseBody   string
	Responses      map[MockedRequest]MockedResponse

	Path        string
	URLParams   map[string]string
	PostParams  map[string]string
	RequestBody string
	Headers     map[string]string

	Error      string
	Status     string
	ExternalID string

	Stopped bool

	ContactURNs map[string]bool

	SendPrep SendPrepFunc
}

ChannelSendTestCase defines the test values for a particular test case

type MockedRequest added in v1.2.103

type MockedRequest struct {
	Method       string
	Path         string
	Body         string
	BodyContains string
}

MockedRequest is a fake HTTP request

type MockedResponse added in v1.2.103

type MockedResponse struct {
	Status int
	Body   string
}

MockedResponse is a fake HTTP response

type RequestPrepFunc

type RequestPrepFunc func(*http.Request)

RequestPrepFunc is our type for a hook for tests to use before a request is fired in a test

type ResponseWriter added in v1.2.98

type ResponseWriter interface {
	Backend() courier.Backend
	WriteStatusSuccessResponse(ctx context.Context, w http.ResponseWriter, r *http.Request, statuses []courier.MsgStatus) error
	WriteMsgSuccessResponse(ctx context.Context, w http.ResponseWriter, r *http.Request, msgs []courier.Msg) error
	WriteRequestError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) error
	WriteRequestIgnored(ctx context.Context, w http.ResponseWriter, r *http.Request, msg string) error
}

ResponseWriter interace with response methods for success responses

type SendPrepFunc added in v0.2.0

SendPrepFunc allows test cases to modify the channel, msg or server before a message is sent

Jump to

Keyboard shortcuts

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