Documentation
¶
Index ¶
- func PointerFrom[T any](v T) *T
- type Client
- func (c *Client) Close()
- func (c *Client) Flush() error
- func (c *Client) SetIdentifiers(userID string, is *Identifiers)
- func (c *Client) StartGame(userID string)
- func (c *Client) StartRound(id string, traits Traits) *Round
- func (c *Client) Track(userID string, eventName string, value *int, traits Traits)
- type ClientBuilder
- func (cb *ClientBuilder) Build() *Client
- func (cb *ClientBuilder) WithBatchSize(batchSize int) *ClientBuilder
- func (cb *ClientBuilder) WithClientID(clientID string) *ClientBuilder
- func (cb *ClientBuilder) WithClientSecret(clientSecret string) *ClientBuilder
- func (cb *ClientBuilder) WithDSN(dsn string) *ClientBuilder
- func (cb *ClientBuilder) WithErrorChannel(ch chan error) *ClientBuilder
- func (cb *ClientBuilder) WithFlushCooldown(cooldown time.Duration) *ClientBuilder
- func (cb *ClientBuilder) WithFlushInterval(interval time.Duration) *ClientBuilder
- func (cb *ClientBuilder) WithGameID(gameID string) *ClientBuilder
- func (cb *ClientBuilder) WithMaxRetryAttempts(maxAttempts int) *ClientBuilder
- type Identifier
- type Identifiers
- type Round
- type Traits
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PointerFrom ¶
func PointerFrom[T any](v T) *T
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the gateway to the Earn Alliance API. It is concurrency safe after CreateClient is called. It contains an event queue which sends batchSize (can be set via options) events to the API. This queue is LIFO.
func (*Client) Close ¶
func (c *Client) Close()
Close closes the open goroutines. It is not concurrency safe.
func (*Client) Flush ¶
Flush flushes the event queue. We can only rely on this returning an error or not in case #1 below. Other cases are asynchronous and won't return an error. Three cases can happen: 1. The cooldown period has passed: - Then we simply send the events to the API. - This case will return an error if the HTTP request fails after max retries. 2. The cooldown period is active & Flush hasn't been called during this period yet: - Then we start a goroutine that will send the events to the API once cooldown is over. - Basically this goroutine is async and the function will return nil immediately. 3. The cooldown is active & Flush has been called during this period: - Then this simply returns nil and the events will be sent by the goroutine that was created in case #2.
func (*Client) SetIdentifiers ¶
func (c *Client) SetIdentifiers(userID string, is *Identifiers)
SetIdentifiers submits an identifier to the event queue. This will call Flush no matter what, but whether it will be sent immediately depends on if the cooldown period is active. However if the event queue hits the batch size limit, the events will be sent to the API.
func (*Client) StartGame ¶
StartGame submits an event with the name "START_GAME" and without any traits or value to the event queue. If the event queue hits the batch size limit, then Flush will be called.
func (*Client) StartRound ¶
StartRound creates a new Round with the given traits. These traits can be overwritten for specific events via passing in traits with the same keys when submitting an event. If id is an empty string we will generate a fresh UUID, and the events sent to this round will have their GroupID set to this UUID.
type ClientBuilder ¶
type ClientBuilder struct {
// contains filtered or unexported fields
}
ClientBuilder builds a new client. It is not concurrency safe. It will panic if required options are missing, or if any invalid options are passed in.
func NewClientBuilder ¶
func NewClientBuilder() *ClientBuilder
NewClientBuilder creates a new ClientBuilder. It also sets the default values in the underlying client. And looks for the environment variables: ALLIANCE_CLIENT_ID, ALLIANCE_CLIENT_SECRET, ALLIANCE_GAME_ID and ALLIANCE_DSN.
func (*ClientBuilder) Build ¶
func (cb *ClientBuilder) Build() *Client
Build returns the client that was created via the builder and starts the internal batch processing goroutine. Ensure that you have the ClientID, ClientSecret and GameID set before you call this.
func (*ClientBuilder) WithBatchSize ¶
func (cb *ClientBuilder) WithBatchSize(batchSize int) *ClientBuilder
WithBatchSize sets the batch size which is the maximum size of the event queue where it will be flushed automatically if reached. Default: 100 This is optional.
func (*ClientBuilder) WithClientID ¶
func (cb *ClientBuilder) WithClientID(clientID string) *ClientBuilder
WithClientID sets the Earn Alliance client ID. Default: N/A This is required.
func (*ClientBuilder) WithClientSecret ¶
func (cb *ClientBuilder) WithClientSecret(clientSecret string) *ClientBuilder
WithClientSecret sets the Earn Alliance client secret. Default: N/A This is required.
func (*ClientBuilder) WithDSN ¶
func (cb *ClientBuilder) WithDSN(dsn string) *ClientBuilder
WithDSN sets the DSN (the URL that requests are sent to) for the Earn Alliance API. Default: https://events.earnalliance.com/v2/custom-events This is optional.
func (*ClientBuilder) WithErrorChannel ¶
func (cb *ClientBuilder) WithErrorChannel(ch chan error) *ClientBuilder
WithErrorChannel sets the error channel where the asynchronous Flush calls will send their errors to. Multiple errors may be sent at once. Default: N/A This is optional.
func (*ClientBuilder) WithFlushCooldown ¶
func (cb *ClientBuilder) WithFlushCooldown(cooldown time.Duration) *ClientBuilder
WithFlushCooldown sets the flush cooldown which is the minimum required time between Flush() calls. During this cooldown period, a Flush() call will start a timer in a goroutine that will flush afterwards. Default: 10 seconds This is optional.
func (*ClientBuilder) WithFlushInterval ¶
func (cb *ClientBuilder) WithFlushInterval(interval time.Duration) *ClientBuilder
WithFlushInterval sets the flush interval which is the time between flushes without the event queue hitting the batch size. Default: 30 seconds This is optional.
func (*ClientBuilder) WithGameID ¶
func (cb *ClientBuilder) WithGameID(gameID string) *ClientBuilder
WithGameID sets the Earn Alliance game ID. Default: N/A This is required.
func (*ClientBuilder) WithMaxRetryAttempts ¶
func (cb *ClientBuilder) WithMaxRetryAttempts(maxAttempts int) *ClientBuilder
WithMaxRetryAttempts sets the maximum number of retries before an HTTP request is considered as failed and the library returns an error. Default: 5 This is optional.
type Identifier ¶ added in v0.0.2
type Identifier string
Identifier represents a user's idenfitier which is a string. To remove the identifier from the user, its value should be an empty string. Use the IdentifierFrom function to create these with ease.
func IdentifierFrom ¶ added in v0.0.2
func IdentifierFrom(s string) *Identifier
IdentifierFrom creates a new Identifier from s.
func RemoveIdentifier ¶ added in v0.0.2
func RemoveIdentifier() *Identifier
RemoveIdentifier creates a new Identifier with empty string. Use this to remove any identifier.
func (Identifier) MarshalJSON ¶ added in v0.0.2
func (s Identifier) MarshalJSON() ([]byte, error)
MarshalJSON marshals a Identifier to JSON. This will marshal a null if string is empty. If it's not, it will marshal the string as normal JSON string.
type Identifiers ¶
type Identifiers struct { AppleID *Identifier `json:"appleId,omitempty"` DiscordID *Identifier `json:"discordId,omitempty"` Email *Identifier `json:"email,omitempty"` EpicGamesID *Identifier `json:"epicGamesId,omitempty"` SteamID *Identifier `json:"steamId,omitempty"` TwitterId *Identifier `json:"twitterId,omitempty"` WalletAddress *Identifier `json:"walletAddress,omitempty"` }
Identifiers contains the current identifiers supported by Earn Alliance. The pointers provide an easy way of omitting values. A nil pointer will be omitted from the JSON when submitting it to the API. If the value is an empty string, that identifier will be removed from the user. We provide the generic helper function PointerFrom() to get the pointer to any value.
type Round ¶
type Round struct {
// contains filtered or unexported fields
}
Round is a nice way of grouping some events together. It sets the GroupID of the events submitted to it. Its ID is a random UUID. You can also create the Round with some traits and these traits will be copied to the events.
func (*Round) Track ¶
Track submits an event to the event queue with its GroupID set to the round's ID. The traits are combined with the round's traits. The traits passed to this function will overwrite the round's traits for this event when they are combined. If the event queue hits the batch size limit, then Flush will be called.