Skip to content

Instantly share code, notes, and snippets.

@Jimeux
Last active December 22, 2021 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jimeux/310e2f10b324405f7b99fb4b4425b766 to your computer and use it in GitHub Desktop.
Save Jimeux/310e2f10b324405f7b99fb4b4425b766 to your computer and use it in GitHub Desktop.
// APIClient is a simple wrapper around APIGatewayManagementAPI.
type APIClient struct {
client awsiface.APIGatewayManagementAPI
}
func NewAPIGatewayClient(client awsiface.APIGatewayManagementAPI) *APIClient {
return &APIClient{client: client}
}
// NewAPIClientFromConfig create a APIClient instance from a given aws.Config instance.
// stage and domain are used to construct the required endpoint resolver.
func NewAPIClientFromConfig(cfg aws.Config, stage, domainName string) *APIClient {
var endpoint url.URL
endpoint.Scheme = "https"
endpoint.Path = stage
endpoint.Host = domainName
endpointResolver := apigatewaymanagementapi.EndpointResolverFromURL(endpoint.String())
return NewAPIGatewayClient(apigatewaymanagementapi.NewFromConfig(
cfg, apigatewaymanagementapi.WithEndpointResolver(endpointResolver)))
}
// Publish posts data to the connection identified by connID.
func (c *APIClient) Publish(ctx context.Context, connID string, data []byte) error {
_, err := c.client.PostToConnection(ctx, &apigatewaymanagementapi.PostToConnectionInput{
Data: data,
ConnectionId: aws.String(connID),
})
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment