From 9ca8da152b355f0171e3b93455328b8781780eee Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Mon, 21 Oct 2024 12:08:33 +0400 Subject: [PATCH] Update go.mdx --- docs/sdks/languages/go.mdx | 60 +++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/docs/sdks/languages/go.mdx b/docs/sdks/languages/go.mdx index 2d5164b36..fb9a90081 100644 --- a/docs/sdks/languages/go.mdx +++ b/docs/sdks/languages/go.mdx @@ -17,16 +17,17 @@ If you're working with Go Lang, the official [Infisical Go SDK](https://github.c package main import ( - "fmt" - "os" - - infisical "github.com/infisical/go-sdk" + "fmt" + "os" + "context" + infisical "github.com/infisical/go-sdk" ) func main() { - client := infisical.NewInfisicalClient(infisical.Config{ + client := infisical.NewInfisicalClient(context.Background(), infisical.Config{ SiteUrl: "https://app.infisical.com", // Optional, default is https://app.infisical.com + AutoTokenRefresh: true, // Wether or not to let the SDK handle the access token lifecycle. Defaults to true if not specified. }) _, err = client.Auth().UniversalAuthLogin("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET") @@ -64,12 +65,41 @@ This example demonstrates how to use the Infisical Go SDK in a simple Go applica ```console $ go get github.com/infisical/go-sdk ``` + +## Automatic token refreshing + +The Infisical Go SDK supports automatic token refreshing. After using one of the auth methods such as Universal Auth, the SDK will automatically renew and re-authenticate when needed. +This behavior is enabled by default, but you can opt-out by setting `AutoTokenRefresh` to `false` in the client settings. + +```go + client := infisical.NewInfisicalClient(context.Background(), infisical.Config{ + AutoTokenRefresh: false, // <- Disable automatic token refreshing + }) +``` + +When using automatic token refreshing it's important to understand how your application uses the Infiiscal client. If you are instantiating new instances of the client often, it's important to cancel the context when the client is no longer needed to avoid the token refreshing process from running indefinitely. + +```go + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Cancel the context when the client is no longer needed + + client := infisical.NewInfisicalClient(ctx, infisical.Config{ + AutoTokenRefresh: true, + }) + + // Use the client +``` + +This is only necessary if you are creating multiple instances of the client, and those instances are deleted or otherwise removed throughout the application lifecycle. +If you are only creating one instance of the client, and it will be used throughout the lifetime of your application, you don't need to worry about this. + + # Configuration Import the SDK and create a client instance. ```go -client := infisical.NewInfisicalClient(infisical.Config{ +client := infisical.NewInfisicalClient(context.Background(), infisical.Config{ SiteUrl: "https://app.infisical.com", // Optional, default is https://api.infisical.com }) ``` @@ -78,13 +108,21 @@ client := infisical.NewInfisicalClient(infisical.Config{ - - The URL of the Infisical API. Default is `https://api.infisical.com`. + + The URL of the Infisical API.. - + Optionally set the user agent that will be used for HTTP requests. _(Not recommended)_ + + + Whether or not to let the SDK handle the access token lifecycle. Defaults to true if not specified. + + + + Whether or not to suppress logs such as warnings from the token refreshing process. Defaults to false if not specified. + @@ -567,4 +605,6 @@ Delete a folder in Infisical. The path from where the folder should be deleted. - \ No newline at end of file + + +