Documentation
¶
Index ¶
- func IsRetryableError(err error) bool
- func IsRetryableHTTPStatus(statusCode int) bool
- type AGSError
- type AuthenticationError
- type BadRequestError
- type DevMockRewardClient
- func (d *DevMockRewardClient) GrantItemReward(ctx context.Context, namespace, userID, itemID string, quantity int) error
- func (d *DevMockRewardClient) GrantReward(ctx context.Context, namespace, userID string, reward domain.Reward) error
- func (d *DevMockRewardClient) GrantWalletReward(ctx context.Context, namespace, userID, currencyCode string, amount int) error
- type ForbiddenError
- type HTTPStatusCodeError
- type MockRewardClient
- func (m *MockRewardClient) GrantItemReward(ctx context.Context, namespace, userID, itemID string, quantity int) error
- func (m *MockRewardClient) GrantReward(ctx context.Context, namespace, userID string, reward domain.Reward) error
- func (m *MockRewardClient) GrantWalletReward(ctx context.Context, namespace, userID, currencyCode string, amount int) error
- type NotFoundError
- type RewardClient
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsRetryableError ¶
IsRetryableError determines if an error from RewardClient should be retried.
Classification strategy: 1. If error implements HTTPStatusCodeError, check status code (most reliable) 2. If error is a known typed error, use its status code 3. Fallback to error message pattern matching (for generic errors)
Non-retryable errors (fail immediately):
- HTTP 400 Bad Request - invalid item/currency configuration
- HTTP 401 Unauthorized - invalid credentials
- HTTP 403 Forbidden - insufficient permissions
- HTTP 404 Not Found - item/currency doesn't exist
- HTTP 409 Conflict - resource conflict
- HTTP 422 Unprocessable Entity - validation failed
Retryable errors (retry with exponential backoff):
- HTTP 408 Request Timeout
- HTTP 429 Too Many Requests
- HTTP 500 Internal Server Error
- HTTP 502 Bad Gateway
- HTTP 503 Service Unavailable
- HTTP 504 Gateway Timeout
- Network timeouts, connection refused, DNS failures
Usage in retry logic:
err := rewardClient.GrantReward(...)
if err != nil && !IsRetryableError(err) {
return err // Fail immediately
}
func IsRetryableHTTPStatus ¶
IsRetryableHTTPStatus determines if an HTTP status code should be retried.
Non-retryable status codes (4xx client errors):
- 400 Bad Request - invalid parameters
- 401 Unauthorized - authentication failed
- 403 Forbidden - insufficient permissions
- 404 Not Found - resource doesn't exist
- 409 Conflict - resource conflict
- 422 Unprocessable Entity - validation failed
Retryable status codes:
- 408 Request Timeout
- 429 Too Many Requests
- 500 Internal Server Error
- 502 Bad Gateway
- 503 Service Unavailable
- 504 Gateway Timeout
Types ¶
type AGSError ¶
AGSError represents an error response from AGS Platform Service. It includes the HTTP status code for proper error classification.
func (*AGSError) HTTPStatusCode ¶
HTTPStatusCode returns the HTTP status code from the AGS response.
type AuthenticationError ¶
type AuthenticationError struct {
Message string
}
AuthenticationError indicates authentication failure (401). Examples: invalid/expired token, invalid client credentials
func (*AuthenticationError) Error ¶
func (e *AuthenticationError) Error() string
func (*AuthenticationError) HTTPStatusCode ¶
func (e *AuthenticationError) HTTPStatusCode() int
type BadRequestError ¶
type BadRequestError struct {
Message string
}
BadRequestError indicates invalid request parameters (400). Examples: invalid item ID, invalid currency code, invalid quantity
func (*BadRequestError) Error ¶
func (e *BadRequestError) Error() string
func (*BadRequestError) HTTPStatusCode ¶
func (e *BadRequestError) HTTPStatusCode() int
type DevMockRewardClient ¶ added in v0.1.2
type DevMockRewardClient struct{}
DevMockRewardClient is a simple mock implementation for local development. Unlike MockRewardClient (testify/mock), this doesn't require explicit setup and always succeeds with logged output.
Use this for local development when REWARD_CLIENT_MODE=mock. For tests, use MockRewardClient instead.
func NewDevMockRewardClient ¶ added in v0.1.2
func NewDevMockRewardClient() *DevMockRewardClient
NewDevMockRewardClient creates a new development mock reward client.
func (*DevMockRewardClient) GrantItemReward ¶ added in v0.1.2
func (d *DevMockRewardClient) GrantItemReward(ctx context.Context, namespace, userID, itemID string, quantity int) error
GrantItemReward logs the reward grant and returns success.
func (*DevMockRewardClient) GrantReward ¶ added in v0.1.2
func (d *DevMockRewardClient) GrantReward(ctx context.Context, namespace, userID string, reward domain.Reward) error
GrantReward logs the reward grant and returns success.
func (*DevMockRewardClient) GrantWalletReward ¶ added in v0.1.2
func (d *DevMockRewardClient) GrantWalletReward(ctx context.Context, namespace, userID, currencyCode string, amount int) error
GrantWalletReward logs the reward grant and returns success.
type ForbiddenError ¶
type ForbiddenError struct {
Message string
}
ForbiddenError indicates insufficient permissions (403). Examples: namespace mismatch, service account lacks permissions
func (*ForbiddenError) Error ¶
func (e *ForbiddenError) Error() string
func (*ForbiddenError) HTTPStatusCode ¶
func (e *ForbiddenError) HTTPStatusCode() int
type HTTPStatusCodeError ¶
HTTPStatusCodeError is an interface for errors that include HTTP status codes.
type MockRewardClient ¶
MockRewardClient is a mock implementation of RewardClient for testing. It uses testify/mock to allow test assertions on method calls.
func NewMockRewardClient ¶
func NewMockRewardClient() *MockRewardClient
NewMockRewardClient creates a new mock reward client.
func (*MockRewardClient) GrantItemReward ¶
func (m *MockRewardClient) GrantItemReward(ctx context.Context, namespace, userID, itemID string, quantity int) error
GrantItemReward mocks granting an item reward.
func (*MockRewardClient) GrantReward ¶
func (m *MockRewardClient) GrantReward(ctx context.Context, namespace, userID string, reward domain.Reward) error
GrantReward mocks the convenience method for granting rewards.
func (*MockRewardClient) GrantWalletReward ¶
func (m *MockRewardClient) GrantWalletReward(ctx context.Context, namespace, userID, currencyCode string, amount int) error
GrantWalletReward mocks granting a wallet reward.
type NotFoundError ¶
type NotFoundError struct {
Resource string
}
NotFoundError indicates resource not found (404). Examples: item doesn't exist, currency not configured
func (*NotFoundError) Error ¶
func (e *NotFoundError) Error() string
func (*NotFoundError) HTTPStatusCode ¶
func (e *NotFoundError) HTTPStatusCode() int
type RewardClient ¶
type RewardClient interface {
// GrantItemReward grants an item entitlement to a user.
// This is used for rewards of type "ITEM".
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - namespace: AGS namespace
// - userID: User's unique identifier
// - itemID: Item code from AGS inventory
// - quantity: Number of items to grant
//
// Returns error if grant fails after retries.
GrantItemReward(ctx context.Context, namespace, userID, itemID string, quantity int) error
// GrantWalletReward credits a user's wallet with virtual currency.
// This is used for rewards of type "WALLET".
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - namespace: AGS namespace
// - userID: User's unique identifier
// - currencyCode: Currency code from AGS wallet (e.g., "GOLD", "GEMS")
// - amount: Amount of currency to credit
//
// Returns error if grant fails after retries.
GrantWalletReward(ctx context.Context, namespace, userID, currencyCode string, amount int) error
// GrantReward is a convenience method that dispatches to the appropriate grant method
// based on the reward type (ITEM or WALLET).
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - namespace: AGS namespace
// - userID: User's unique identifier
// - reward: Reward configuration from goal
//
// Returns error if reward type is unsupported or grant fails after retries.
GrantReward(ctx context.Context, namespace, userID string, reward domain.Reward) error
}
RewardClient integrates with AccelByte Gaming Services (AGS) Platform Service to grant rewards to users when they claim completed goals.
This client abstracts the AGS SDK calls and provides retry logic for reliability.