Skip to content

hathora/cloud-sdk-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

github.com/hathora/cloud-sdk-go

Developer-friendly & type-safe Go SDK specifically catered to leverage github.com/hathora/cloud-sdk-go API.



Important

This SDK is not yet ready for production use. To complete setup please follow the steps outlined in your workspace. Delete this section before > publishing to a package manager.

Summary

Hathora Cloud API: Welcome to the Hathora Cloud API documentation! Learn how to use the Hathora Cloud APIs to build and scale your game servers globally.

Table of Contents

SDK Installation

To add the SDK as a dependency to your project:

go get hathoracloud

SDK Example Usage

Example

package main

import (
	"context"
	"hathoracloud"
	"log"
)

func main() {
	ctx := context.Background()

	s := hathoracloud.New(
		hathoracloud.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
		hathoracloud.WithOrgID("org-6f706e83-0ec1-437a-9a46-7d4281eb2f39"),
		hathoracloud.WithAppID("app-af469a92-5b45-4565-b3c4-b79878de67d2"),
	)

	res, err := s.TokensV1.GetOrgTokens(ctx, "org-6f706e83-0ec1-437a-9a46-7d4281eb2f39")
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
HathoraDevToken http HTTP Bearer

You can configure it using the WithSecurity option when initializing the SDK client instance. For example:

package main

import (
	"context"
	"hathoracloud"
	"log"
)

func main() {
	ctx := context.Background()

	s := hathoracloud.New(
		hathoracloud.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
		hathoracloud.WithOrgID("org-6f706e83-0ec1-437a-9a46-7d4281eb2f39"),
		hathoracloud.WithAppID("app-af469a92-5b45-4565-b3c4-b79878de67d2"),
	)

	res, err := s.TokensV1.GetOrgTokens(ctx, "org-6f706e83-0ec1-437a-9a46-7d4281eb2f39")
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

Per-Operation Security Schemes

Some operations in this SDK require the security scheme to be specified at the request level. For example:

package main

import (
	"context"
	"hathoracloud"
	"hathoracloud/models/components"
	"hathoracloud/models/operations"
	"log"
)

func main() {
	ctx := context.Background()

	s := hathoracloud.New(
		hathoracloud.WithOrgID("org-6f706e83-0ec1-437a-9a46-7d4281eb2f39"),
		hathoracloud.WithAppID("app-af469a92-5b45-4565-b3c4-b79878de67d2"),
	)

	res, err := s.LobbiesV3.CreateLobby(ctx, operations.CreateLobbySecurity{
		PlayerAuth: "<YOUR_BEARER_TOKEN_HERE>",
	}, components.CreateLobbyV3Params{
		Visibility: components.LobbyVisibilityPrivate,
		RoomConfig: hathoracloud.String("{\"name\":\"my-room\"}"),
		Region:     components.RegionSeattle,
	}, hathoracloud.String("app-af469a92-5b45-4565-b3c4-b79878de67d2"), hathoracloud.String("LFG4"), hathoracloud.String("2swovpy1fnunu"))
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

Available Resources and Operations

Available methods

Global Parameters

Certain parameters are configured globally. These parameters may be set on the SDK client instance itself during initialization. When configured as an option during SDK initialization, These global values will be used as defaults on the operations that use them. When such operations are called, there is a place in each to override the global value, if needed.

For example, you can set orgId to "org-6f706e83-0ec1-437a-9a46-7d4281eb2f39" at SDK initialization and then you do not have to pass the same value on calls to operations like GetOrgTokens. But if you want to do so you may, which will locally override the global setting. See the example code below for a demonstration.

Available Globals

The following global parameters are available.

Name Type Description
OrgID string The OrgID parameter.
AppID string The AppID parameter.

Example

package main

import (
	"context"
	"hathoracloud"
	"log"
)

func main() {
	ctx := context.Background()

	s := hathoracloud.New(
		hathoracloud.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
		hathoracloud.WithOrgID("org-6f706e83-0ec1-437a-9a46-7d4281eb2f39"),
		hathoracloud.WithAppID("app-af469a92-5b45-4565-b3c4-b79878de67d2"),
	)

	res, err := s.TokensV1.GetOrgTokens(ctx, "org-6f706e83-0ec1-437a-9a46-7d4281eb2f39")
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

Retries

Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.

To change the default retry strategy for a single API call, simply provide a retry.Config object to the call by using the WithRetries option:

package main

import (
	"context"
	"hathoracloud"
	"hathoracloud/retry"
	"log"
	"models/operations"
)

func main() {
	ctx := context.Background()

	s := hathoracloud.New(
		hathoracloud.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
		hathoracloud.WithOrgID("org-6f706e83-0ec1-437a-9a46-7d4281eb2f39"),
		hathoracloud.WithAppID("app-af469a92-5b45-4565-b3c4-b79878de67d2"),
	)

	res, err := s.TokensV1.GetOrgTokens(ctx, "org-6f706e83-0ec1-437a-9a46-7d4281eb2f39", operations.WithRetries(
		retry.Config{
			Strategy: "backoff",
			Backoff: &retry.BackoffStrategy{
				InitialInterval: 1,
				MaxInterval:     50,
				Exponent:        1.1,
				MaxElapsedTime:  100,
			},
			RetryConnectionErrors: false,
		}))
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:

package main

import (
	"context"
	"hathoracloud"
	"hathoracloud/retry"
	"log"
)

func main() {
	ctx := context.Background()

	s := hathoracloud.New(
		hathoracloud.WithRetryConfig(
			retry.Config{
				Strategy: "backoff",
				Backoff: &retry.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		hathoracloud.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
		hathoracloud.WithOrgID("org-6f706e83-0ec1-437a-9a46-7d4281eb2f39"),
		hathoracloud.WithAppID("app-af469a92-5b45-4565-b3c4-b79878de67d2"),
	)

	res, err := s.TokensV1.GetOrgTokens(ctx, "org-6f706e83-0ec1-437a-9a46-7d4281eb2f39")
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both.

By Default, an API error will return errors.SDKError. When custom error responses are specified for an operation, the SDK may also return their associated error. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation.

For example, the GetOrgTokens function may return the following errors:

Error Type Status Code Content Type
errors.APIError 401, 404, 429 application/json
errors.SDKError 4XX, 5XX */*

Example

package main

import (
	"context"
	"errors"
	"hathoracloud"
	"hathoracloud/models/errors"
	"log"
)

func main() {
	ctx := context.Background()

	s := hathoracloud.New(
		hathoracloud.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
		hathoracloud.WithOrgID("org-6f706e83-0ec1-437a-9a46-7d4281eb2f39"),
		hathoracloud.WithAppID("app-af469a92-5b45-4565-b3c4-b79878de67d2"),
	)

	res, err := s.TokensV1.GetOrgTokens(ctx, "org-6f706e83-0ec1-437a-9a46-7d4281eb2f39")
	if err != nil {

		var e *errors.APIError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *errors.SDKError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}
	}
}

Server Selection

Select Server by Index

You can override the default server globally using the WithServerIndex(serverIndex int) option when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:

# Server
0 https://api.hathora.dev
1 https:///

Example

package main

import (
	"context"
	"hathoracloud"
	"log"
)

func main() {
	ctx := context.Background()

	s := hathoracloud.New(
		hathoracloud.WithServerIndex(1),
		hathoracloud.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
		hathoracloud.WithOrgID("org-6f706e83-0ec1-437a-9a46-7d4281eb2f39"),
		hathoracloud.WithAppID("app-af469a92-5b45-4565-b3c4-b79878de67d2"),
	)

	res, err := s.TokensV1.GetOrgTokens(ctx, "org-6f706e83-0ec1-437a-9a46-7d4281eb2f39")
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

Override Server URL Per-Client

The default server can also be overridden globally using the WithServerURL(serverURL string) option when initializing the SDK client instance. For example:

package main

import (
	"context"
	"hathoracloud"
	"log"
)

func main() {
	ctx := context.Background()

	s := hathoracloud.New(
		hathoracloud.WithServerURL("https://api.hathora.dev"),
		hathoracloud.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
		hathoracloud.WithOrgID("org-6f706e83-0ec1-437a-9a46-7d4281eb2f39"),
		hathoracloud.WithAppID("app-af469a92-5b45-4565-b3c4-b79878de67d2"),
	)

	res, err := s.TokensV1.GetOrgTokens(ctx, "org-6f706e83-0ec1-437a-9a46-7d4281eb2f39")
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response
	}
}

Custom HTTP Client

The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.

import (
	"net/http"
	"time"
	"github.com/myorg/your-go-sdk"
)

var (
	httpClient = &http.Client{Timeout: 30 * time.Second}
	sdkClient  = sdk.New(sdk.WithClient(httpClient))
)

This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

SDK Created by Speakeasy

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages