Skip to main content

Get started with the Go Extend SDK

Last updated on 9/21/2023, 11:56:45 AM
note

Customization is not available on the AGS Starter tier yet. It's coming soon!

Overview

The AccelByte Extend SDK is provided to help developers invoke AccelByte Gaming Services (AGS) endpoints from within their backend code. The Extend SDK is generated from AGS OpenAPI specs and provides required functions such as login, refreshing tokens, validating tokens, and retrying HTTP requests. It is easy to use so developers can focus on what they want to do instead of spending time thinking on how to invoke endpoints.

The AccelByte Extend SDK is available in multiple selected programming languages. We will show you the basics using the Go Extend SDK.

Goals

In this guide you will:

  • Create a Go application project.
  • Add the Go Extend SDK as a project dependency.
  • Use the Go Extend SDK to invoke an AGS endpoint.
  • Run the application.

Prerequisites

In order to start on this guide, you should have:

  • Installed Go 1.16 or later.
  • Gained access to the AGS demo environment:
    • Use https://demo.accelbyte.io for the AB_BASE_URL environment variable.
    • Follow the steps in the Create an OAuth client guide, using the Confidential client type. Use the Client ID and Client Secret for AB_CLIENT_ID and AB_CLIENT_SECRET environment variables respectively.

Create and run a Go application

  1. Create a Go application project.

    Create a folder and use go mod init to create a new Go application project:

    $ mkdir getting-started
    $ cd getting-started/
    $ go mod init golang-application
  2. Add the Go Extend SDK as a project dependency.

    Add the Go Extend SDK in the newly created go.mod file.

    module golang-application

    go 1.16

    require (
    github.com/AccelByte/accelbyte-go-sdk {VERSION}
    )

    Replace {VERSION} with a specific release version tag from releases, then run go mod tidy.

    It is recommended that you use the matching Go Extend SDK version for the given AGS version.

  3. Use the Go Extend SDK to invoke an AGS endpoint.

    To invoke an AGS endpoint using the Go Extend SDK, you will have to create a Go Extend SDK instance, log in using Oauth Client credentials, and invoke the AGS endpoint GetCountryLocationV3. To do these, create a new main.go file with the following contents.

    package main

    import (
    "github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclient/o_auth2_0_extension"
    "github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/utils/auth"
    "github.com/sirupsen/logrus"

    "github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/factory"
    "github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/service/iam"
    )

    var (
    // Use the default Go Extend SDK config and token repository implementation
    configRepo = *auth.DefaultConfigRepositoryImpl()
    tokenRepo = *auth.DefaultTokenRepositoryImpl()
    )

    func main() {
    // Prepare the IAM Oauth service which contains the `LoginClient` function
    oauth := &iam.OAuth20Service{
    Client: factory.NewIamClient(&configRepo),
    ConfigRepository: &configRepo,
    TokenRepository: &tokenRepo,
    }
    clientId := oauth.ConfigRepository.GetClientId()
    clientSecret := oauth.ConfigRepository.GetClientSecret()

    // Login using Oauth Client credentials
    err := oauth.LoginClient(&clientId, &clientSecret)
    if err != nil {
    logrus.Error("failed login client")
    } else {
    logrus.Info("successful login")
    }

    // Print out access token we get after login
    token, _ := oauth.TokenRepository.GetToken()
    logrus.Infof("print %v", *token.AccessToken)

    // Prepare the IAM's Oauth 2.0 Extension service which contains the `GetCountryLocationV3` function
    oAuth20ExtensionService := &iam.OAuth20ExtensionService{
    Client: factory.NewIamClient(&configRepo),
    TokenRepository: &tokenRepo,
    }
    input := &o_auth2_0_extension.GetCountryLocationV3Params{}

    // Call the AGS endpoint `GetCountryLocationV3`
    ok, errLoc := oAuth20ExtensionService.GetCountryLocationV3Short(input)
    if errLoc != nil {
    logrus.Error(errLoc.Error())
    } else {
    logrus.Infof("Country name: %s", *ok.CountryName)
    }
    }

    The ConfigRepositoryImpl gets its values from AB_BASE_URL, AB_CLIENT_ID, and AB_CLIENT_SECRET environment variables.

  4. Run the application.

    Set the required environment variables and run the application, using go run main.go.

    $ export AB_BASE_URL="https://demo.accelbyte.io"              # Demo environment Base URL
    $ export AB_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Put your Client ID here
    $ export AB_CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Put your Client Secret here
    $ go run main.go

Next steps

  • Consider trying to add the Go Extend SDK as a project dependency and using it in your own project.

Resources

  • For more advanced usage of Go Extend SDK, see the README.
  • For examples of common use cases using the Go Extend SDK, see the common use cases docs.
  • If you know which AGS endpoints you need to invoke and you want to invoke them using the Go Extend SDK, refer to the operations docs.