Documentation ¶
Overview ¶
Package instance allows making instance scoped requests.
Instance provides a higher level abstraction over Client and exposes the Authenticator interface. It is the primary entrypoint that should be used to interact with the platform. Instances are tied to their instance locators that can be found in the dashboard at https://dash.pusher.com.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseInstanceLocator ¶
ParseInstanceLocator splits the instance locator string to retrieve the service version, cluster and instance id which is returned as an instanceLocatorComponents type.
Example ¶
package main import ( "fmt" "github.com/pusher/pusher-platform-go/instance" ) func main() { components, err := instance.ParseInstanceLocator("version:cluster:instance-id") if err != nil { // Do something with error } fmt.Printf("Platform version: %s", components.PlatformVersion) fmt.Printf("Cluster: %s", components.Cluster) fmt.Printf("Instance ID: %s", components.InstanceID) }
Output:
func ParseKey ¶
ParseKey splits the key to retrieve the public key and secret which is returned as part of `keyComponents`.
Example ¶
package main import ( "fmt" "github.com/pusher/pusher-platform-go/instance" ) func main() { components, err := instance.ParseKey("key:secret") if err != nil { // Do something with error } fmt.Printf("Key: %s", components.Key) fmt.Printf("Secret: %s", components.Secret) }
Output:
Types ¶
type Instance ¶
type Instance interface { Request(ctx context.Context, options client.RequestOptions) (*http.Response, error) Authenticate(payload auth.Payload, options auth.Options) (*auth.Response, error) GenerateAccessToken(options auth.Options) (auth.TokenWithExpiry, error) }
Instance allows making HTTP requests to a service. It also allows access to the authenticator interface.
func New ¶
New creates a new instance satisfying the Instance interface.
Instance locator, key, service name and service version are all required. It will return an error if any of these are not provided.
Example ¶
package main import ( "github.com/pusher/pusher-platform-go/instance" ) func main() { instance.New(instance.Options{ Locator: "version:cluster:instance-id", Key: "key:secret", ServiceName: "service-name", ServiceVersion: "service-version", }) }
Output:
Example (Authenticate) ¶
package main import ( "fmt" "github.com/pusher/pusher-platform-go/auth" "github.com/pusher/pusher-platform-go/instance" ) func main() { serviceInstance, err := instance.New(instance.Options{ Locator: "version:cluster:instance-id", Key: "key:secret", ServiceName: "service-name", ServiceVersion: "service-version", }) if err != nil { // Do something with error } // For a more detailed example: check out the auth package userID := "test-user" authResponse, err := serviceInstance.Authenticate(auth.Payload{ GrantType: "client_credentials", }, auth.Options{ UserID: &userID, }) if err != nil { // Do something with err } err = authResponse.Error() if err != nil { // Do someting with response error // This should usually be a write to an HTTP stream with the headers and status fmt.Printf("Response status: %v", authResponse.Status) fmt.Printf("Response headers: %v", authResponse.Headers) fmt.Printf("Response error: %v", err.Error()) } tokenResponse := authResponse.TokenResponse() if tokenResponse != nil { // Send the token back to a client fmt.Printf("Token: %v", tokenResponse.AccessToken) fmt.Printf("Token expiry: %v", tokenResponse.ExpiresIn) fmt.Printf("Token type: %v", tokenResponse.TokenType) } }
Output:
Example (GenerateAccessToken) ¶
package main import ( "fmt" "github.com/pusher/pusher-platform-go/auth" "github.com/pusher/pusher-platform-go/instance" ) func main() { serviceInstance, err := instance.New(instance.Options{ Locator: "version:cluster:instance-id", Key: "key:secret", ServiceName: "service-name", ServiceVersion: "service-version", }) if err != nil { // Do something with error } userID := "test-user" token, err := serviceInstance.GenerateAccessToken(auth.Options{ UserID: &userID, }) if err != nil { // Do something with error } fmt.Println(token.Token) }
Output:
Example (Request) ¶
package main import ( "context" "github.com/pusher/pusher-platform-go/client" "github.com/pusher/pusher-platform-go/instance" ) func main() { serviceInstance, err := instance.New(instance.Options{ Locator: "version:cluster1:instance-id1", Key: "key1:secret1", ServiceName: "service-name1", ServiceVersion: "service-version1", }) if err != nil { // Do something with error } ctx := context.Background() response, err := serviceInstance.Request(ctx, client.RequestOptions{ Method: "GET", Path: "/foo/bar", }) if err != nil { // Do something with error } if response.StatusCode == 200 { // Do something with response } }
Output:
type Options ¶
type Options struct { Locator string // Instance locator unique to an instance Key string // Key unique to an instance ServiceName string // Service name to connect to ServiceVersion string // Version of service to connect to Client client.Client // Optional Client, if not provided will be constructed }
Options to initialize a new instance.