メインコンテンツまでスキップ

Get started with the Extend SDK

Last updated on December 1, 2024

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 Extend SDK in several different languages (Go, Python, C#, and Java).

Goals

In this guide you will:

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

Prerequisites

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

  • Access to the AccelByte Gaming Services (AGS) (demo environment):
    • Use <your environment's domain URL> for AB_BASE_URL environment variable.
      • Example for AGS Shared Cloud customer: https://spaceshooter.prod.gamingservices.accelbyte.io
      • Example for AGS Private Cloud customer: https://dev.customer.accelbyte.io
    • Create an OAuth Client with client type Confidential.
      • Use Client ID value for AB_CLIENT_ID environment variable.
      • Use Client Secret value for AB_CLIENT_SECRET environment variable.
  • AccelByte Python Extend SDK
  • Access to the following tools:
    • Git
    • Python 3.10
    • Python IDE

Create a project

Create a folder and use venv to create a Python virtual environment.

For macOS or Linux (Bash) user:

$ mkdir myproject
$ cd myproject/
$ python -m venv venv
$ source venv/bin/activate
$ python -c "import sys; print(sys.executable)" # Check which Python executable is active

For Windows(PowerShell) user:

C:\> mkdir myproject
C:\> cd myproject/
C:\> python -m venv venv
C:\> venv\Scripts\Activate.ps1
C:\> python -c "import sys; print(sys.executable)" # Check which Python executable is active

Add to project dependency

  1. Install the SDK dependencies.

    $ pip install requests httpx websockets pyyaml
  2. Install the SDK.

     $ pip install git+https://github.com/AccelByte/accelbyte-python-sdk.git@{VERSION}#egg=accelbyte_py_sdk
ヒント

We recommended using the Python Extend SDK version that matches your AGS version.

Use in code

  1. Create an SDK instance, log in using client credentials, and call an AGS API in app.py.
  2. The EnvironmentConfigRepository gets its values from AB_BASE_URL, AB_CLIENT_ID, and AB_CLIENT_SECRET environment variables.
# app.py

import accelbyte_py_sdk
from accelbyte_py_sdk.core import (
RequestsHttpClient,
EnvironmentConfigRepository,
InMemoryTokenRepository,
)
import accelbyte_py_sdk.services.auth as auth_service
import accelbyte_py_sdk.api.iam as iam_service


def main():
# Create default HTTP client, token repository, and config repository instances
http_client = RequestsHttpClient()
config_repository = EnvironmentConfigRepository()
token_repository = InMemoryTokenRepository()

# Initialize the SDK
accelbyte_py_sdk.initialize(
options={
"config": config_repository,
"token": token_repository,
"http": http_client,
}
)

# Login using client credentials
token, error = auth_service.login_client()
if error:
exit(1) # Login failed

# Call an AGS API e.g. GetCountryLocationV3
response, error = iam_service.get_country_location_v3()
if error:
exit(1) # Response error

print(response.country_name)


if __name__ == "__main__":
main()

Run the code

Set the required environment variables and run the code using the Python interpreter.

$ export AB_BASE_URL="<your environment's domain URL>"          # AGS Base URL e.g. demo environment
$ export AB_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # AGS OAuth Client ID
$ export AB_CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # AGS OAuth Client Secret
$ python app.py

Additional resources