Skip to main content

Getting Started with the Extend SDK

Last updated on 3/16/2023, 1:53:46 AM
note

Customization is not available on 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 functions required 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.

Learning Objectives

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 Create an OAuth Client, with the confidential client type. Use the Client ID and Client Secret for AB_CLIENT_ID and AB_CLIENT_SECRET environment variables respectively.
  • An internet connection

Create and Run a Go Application

Before starting, make sure that all the prerequisites are completed.

  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, login using Oauth Client credentials, and finally, 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 use it in your own project.

Resources

  • For more advanced usage of Go Extend SDK, see the README.
  • For examples on how to achieve some 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.