Last Updated: 11/4/2021, 3:15:17 AM

# Golang SDK Getting Started Guide

# Overview

You can use the AccelByte Golang SDK to implement our backend services within your game. The SDK acts as a bridge between your game and our services. Follow the tutorials below to learn how to set up our Golang SDK.

# Prerequisites

# Additional Resources

# Tutorials

# Install the AccelByte Golang SDK

To install the AccelByte Golang SDK into your project directory, follow these steps:

  1. Run the following command in Terminal:

    $ go mod init <project-name>
    $ go get github.com/AccelByte/accelbyte-go-sdk
    
  2. Open the go.mod file, then change the SDK to the latest version as shown below:

    module main
    go 1.16
    
    require (
      github.com/AccelByte/accelbyte-go-sdk v0.6.1
    )
    
  3. Then run this command in the Terminal.

    $ go mod tidy
    

# Log in as a Client using the SDK

To log in as the client you created earlier, follow these steps:

  1. Create empty structs for TokenRepositoryImpl and ConfigRepositoryImpl that will be used for the following actions:

    • The TokenRepositoryImpl struct is used to store, get, and remove tokens.

      type TokenRepositoryImpl struct {}
      
      func (t *TokenRepositoryImpl) Store(accessToken iamclientmodels.OauthmodelTokenResponseV3) error {
        clientTokenV3 = accessToken
        return nil
      }
      
      func (t *TokenRepositoryImpl) GetToken() (*iamclientmodels.OauthmodelTokenResponseV3, error) {
        return &clientTokenV3, nil
      }
      
      func (t *TokenRepositoryImpl) RemoveToken() error {
        return nil
      }
      
    • The TokenRepositoryImpl struct to get the Client ID and Client Secret.

      type ConfigRepositoryImpl struct {}
      
      
      func (c *ConfigRepositoryImpl) GetClientId() string {
        return os.Getenv("APP_CLIENT_ID")
      }
      
      func (c *ConfigRepositoryImpl) GetClientSecret() string {
        return os.Getenv("APP_CLIENT_SECRET")
      }
      
      func (c *ConfigRepositoryImpl) GetJusticeBaseUrl() string {
        return os.Getenv("ACCELBYTE_BASE_URL")
      }
      
  2. Initialize the service SDK.

    oauthService := service.OauthService{
      IamService:       factory.NewIamClient(&repository.ConfigRepositoryImpl{}),
      ConfigRepository: &repository.ConfigRepositoryImpl{},
      TokenRepository:  &repository.TokenRepositoryImpl{},
    }
    
  3. Call the GrantTokenCredentials wrapper.

    err := oauthService.GrantTokenCredentials("", "")
    if err != nil {
      return err
    }
    
    return nil
    

    This wrapper provides a straightforward way to retrieve client credentials.

    param := &o_auth2_0.TokenGrantV3Params{
      Code:         &code,
      CodeVerifier: &codeVerifier,
      GrantType:    "client_credentials",
    }
    
    accessToken, _, _, _, err :=
      oauthService.IamService.OAuth20.TokenGrantV3(param, client.BasicAuth(clientId, clientSecret))
    if err != nil {
      return err
    }
    
    return nil
    

# Import AccelByte Services

Now you’re ready to integrate any of the following AccelByte services into your application. All services have already been imported in .../pkg/service.<your-service> will be changed to the service name you want to use automatically during the import.

IAM

API Docs (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclientmodels"
)
Basic

API Docs (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/basic-sdk/pkg/basicclientmodels"
)
Social

API Docs (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/social-sdk/pkg/socialclientmodels"
)
Platform

API Docs (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/platform-sdk/pkg/platformclientmodels"
)
Group

API Docs (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/group-sdk/pkg/groupclientmodels"
)
Cloud Save

API Docs (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/cloudsave-sdk/pkg/cloudsaveclientmodels"
)
DSM Controller

API Docs (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/dsmc-sdk/pkg/dsmcclientmodels"
)
Session Browser

API Docs (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/sessionbrowser-sdk/pkg/sessionbrowserclientmodels"
)
Lobby

API Docs (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/lobby-sdk/pkg/lobbyclientmodels"
)

# Implement the Statistics Service using the SDK

In this section, you’ll learn how to implement our Statistics service using the Golang SDK. Make sure the client you created earlier has the necessary permissions to access the Statistics service.

First, initialize the Statistics service.

socialService := &service.StatisticConfigService{
  SocialServiceClient: factory.NewSocialClient(&repository.ConfigRepositoryImpl{}),
  TokenRepository:     &repository.TokenRepositoryImpl{},
}

After that, you can use the SDK to create, read, update, and delete statistics for a player from your game client.

# Create a Statistic Configuration

POST /social/v1/admin/namespaces/{namespace}/stats.

stat, err := socialService.CreateStat(namespace, body)
if err != nil {
  return err
}

return nil

# Delete a Statistic Configuration

DELETE /social/v1/admin/namespaces/{namespace}/stats/{statCode}

err := socialService.DeleteStat(namespace, statCode)
if err != nil {
  return err
}

return nil

# Retrieve a Statistic Configuration by Stat Code

GET /social/v1/admin/namespaces/{namespace}/stats/{statCode}

stat, err := socialService.GetStat(namespace, statCode)
if err != nil {
  return err
}

return nil

# Retrieve All Statistic Configurations

GET /social/v1/admin/namespaces/{namespace}/stats

stats, err := socialService.GetStats(namespace, &limit, &offset)
if err != nil {
  return err
}

return nil

# Search for a Statistic Configuration by Keyword

GET /social/v1/admin/namespaces/{namespace}/stats/search

stats, err := socialService.QueryStats(namespace, keyword, &limit, &offset)
if err != nil {
  return err
}

return nil

# Update a Statistic Configuration

PATCH /social/v1/admin/namespaces/{namespace}/stats/{statCode}

stat, err := socialService.UpdateStat(namespace, statCode, body)
if err != nil {
  return err
}

return nil