Merge pull request #2627 from Infisical/daniel/go-sdk-docs-update

docs: go SDK refreshing docs
This commit is contained in:
Maidul Islam
2024-10-21 19:23:00 -04:00
committed by GitHub
+106 -51
View File
@@ -11,22 +11,23 @@ If you're working with Go Lang, the official [Infisical Go SDK](https://github.c
- [Package](https://pkg.go.dev/github.com/infisical/go-sdk) - [Package](https://pkg.go.dev/github.com/infisical/go-sdk)
- [Github Repository](https://github.com/infisical/go-sdk) - [Github Repository](https://github.com/infisical/go-sdk)
## Basic Usage # Basic Usage
```go ```go
package main package main
import ( import (
"fmt" "fmt"
"os" "os"
"context"
infisical "github.com/infisical/go-sdk" infisical "github.com/infisical/go-sdk"
) )
func main() { 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 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") _, err = client.Auth().UniversalAuthLogin("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
@@ -64,32 +65,68 @@ This example demonstrates how to use the Infisical Go SDK in a simple Go applica
```console ```console
$ go get github.com/infisical/go-sdk $ go get github.com/infisical/go-sdk
``` ```
# Configuration # Configuration
Import the SDK and create a client instance. Import the SDK and create a client instance.
```go ```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 SiteUrl: "https://app.infisical.com", // Optional, default is https://api.infisical.com
}) })
``` ```
### ClientSettings methods ### Configuration Options
<ParamField query="options" type="object"> <ParamField query="options" type="object">
<Expandable title="properties"> <Expandable title="properties">
<ParamField query="SiteUrl" type="string" optional> <ParamField query="SiteUrl" type="string" optional default="https://app.infisical.com">
The URL of the Infisical API. Default is `https://api.infisical.com`. The URL of the Infisical API..
</ParamField> </ParamField>
<ParamField query="UserAgent" type="string" required> <ParamField query="UserAgent" type="string">
Optionally set the user agent that will be used for HTTP requests. _(Not recommended)_ Optionally set the user agent that will be used for HTTP requests. _(Not recommended)_
</ParamField> </ParamField>
<ParamField query="AutoTokenRefresh" type="boolean" default={true} optional>
Whether or not to let the SDK handle the access token lifecycle. Defaults to true if not specified.
</ParamField>
<ParamField query="SilentMode" type="boolean" default={false} optional>
Whether or not to suppress logs such as warnings from the token refreshing process. Defaults to false if not specified.
</ParamField>
</Expandable> </Expandable>
</ParamField> </ParamField>
### Authentication # 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.
# Authentication
The SDK supports a variety of authentication methods. The most common authentication method is Universal Auth, which uses a client ID and client secret to authenticate. The SDK supports a variety of authentication methods. The most common authentication method is Universal Auth, which uses a client ID and client secret to authenticate.
@@ -222,9 +259,12 @@ if err != nil {
} }
``` ```
## Working with Secrets ## Working With Secrets
### client.Secrets().List(options) ### List Secrets
`client.Secrets().List(options)`
Retrieve all secrets within the Infisical project and environment that client is connected to.
```go ```go
secrets, err := client.Secrets().List(infisical.ListSecretsOptions{ secrets, err := client.Secrets().List(infisical.ListSecretsOptions{
@@ -235,9 +275,7 @@ secrets, err := client.Secrets().List(infisical.ListSecretsOptions{
}) })
``` ```
Retrieve all secrets within the Infisical project and environment that client is connected to ### Parameters
#### Parameters
<ParamField query="Parameters" type="object"> <ParamField query="Parameters" type="object">
<Expandable title="properties"> <Expandable title="properties">
@@ -272,7 +310,11 @@ Retrieve all secrets within the Infisical project and environment that client is
</ParamField> </ParamField>
### client.Secrets().Retrieve(options) ###
### Retrieve Secret
`client.Secrets().Retrieve(options)`
Retrieve a secret from Infisical. By default `Secrets().Retrieve()` fetches and returns a shared secret.
```go ```go
secret, err := client.Secrets().Retrieve(infisical.RetrieveSecretOptions{ secret, err := client.Secrets().Retrieve(infisical.RetrieveSecretOptions{
@@ -282,11 +324,7 @@ secret, err := client.Secrets().Retrieve(infisical.RetrieveSecretOptions{
}) })
``` ```
Retrieve a secret from Infisical. ### Parameters
By default, `Secrets().Retrieve()` fetches and returns a shared secret.
#### Parameters
<ParamField query="Parameters" type="object" optional> <ParamField query="Parameters" type="object" optional>
<Expandable title="properties"> <Expandable title="properties">
@@ -308,7 +346,11 @@ By default, `Secrets().Retrieve()` fetches and returns a shared secret.
</Expandable> </Expandable>
</ParamField> </ParamField>
### client.Secrets().Create(options) ###
### Create Secret
`client.Secrets().Create(options)`
Create a new secret in Infisical.
```go ```go
secret, err := client.Secrets().Create(infisical.CreateSecretOptions{ secret, err := client.Secrets().Create(infisical.CreateSecretOptions{
@@ -321,9 +363,8 @@ secret, err := client.Secrets().Create(infisical.CreateSecretOptions{
}) })
``` ```
Create a new secret in Infisical.
#### Parameters ### Parameters
<ParamField query="Parameters" type="object" optional> <ParamField query="Parameters" type="object" optional>
<Expandable title="properties"> <Expandable title="properties">
@@ -351,7 +392,12 @@ Create a new secret in Infisical.
</Expandable> </Expandable>
</ParamField> </ParamField>
### client.Secrets().Update(options) ###
### Update Secret
`client.Secrets().Update(options)`
Update an existing secret in Infisical.
```go ```go
secret, err := client.Secrets().Update(infisical.UpdateSecretOptions{ secret, err := client.Secrets().Update(infisical.UpdateSecretOptions{
@@ -363,9 +409,7 @@ secret, err := client.Secrets().Update(infisical.UpdateSecretOptions{
}) })
``` ```
Update an existing secret in Infisical. ### Parameters
#### Parameters
<ParamField query="Parameters" type="object" optional> <ParamField query="Parameters" type="object" optional>
<Expandable title="properties"> <Expandable title="properties">
@@ -393,7 +437,11 @@ Update an existing secret in Infisical.
</Expandable> </Expandable>
</ParamField> </ParamField>
### client.Secrets().Delete(options) ###
### Delete Secret
`client.Secrets().Delete(options)`
Delete a secret in Infisical.
```go ```go
secret, err := client.Secrets().Delete(infisical.DeleteSecretOptions{ secret, err := client.Secrets().Delete(infisical.DeleteSecretOptions{
@@ -403,9 +451,7 @@ secret, err := client.Secrets().Delete(infisical.DeleteSecretOptions{
}) })
``` ```
Delete a secret in Infisical. ### Parameters
#### Parameters
<ParamField query="Parameters" type="object" optional> <ParamField query="Parameters" type="object" optional>
<Expandable title="properties"> <Expandable title="properties">
@@ -427,10 +473,14 @@ Delete a secret in Infisical.
</Expandable> </Expandable>
</ParamField> </ParamField>
## Working with folders ## Working With folders
### client.Folders().List(options) ###
### List Folders
`client.Folders().List(options)`
Retrieve all within the Infisical project and environment that client is connected to.
```go ```go
folders, err := client.Folders().List(infisical.ListFoldersOptions{ folders, err := client.Folders().List(infisical.ListFoldersOptions{
@@ -440,9 +490,7 @@ folders, err := client.Folders().List(infisical.ListFoldersOptions{
}) })
``` ```
Retrieve all within the Infisical project and environment that client is connected to. ### Parameters
#### Parameters
<ParamField query="Parameters" type="object"> <ParamField query="Parameters" type="object">
<Expandable title="properties"> <Expandable title="properties">
@@ -461,7 +509,11 @@ Retrieve all within the Infisical project and environment that client is connect
</ParamField> </ParamField>
### client.Folders().Create(options) ###
### Create Folder
`client.Folders().Create(options)`
Create a new folder in Infisical.
```go ```go
folder, err := client.Folders().Create(infisical.CreateFolderOptions{ folder, err := client.Folders().Create(infisical.CreateFolderOptions{
@@ -472,9 +524,7 @@ folder, err := client.Folders().Create(infisical.CreateFolderOptions{
}) })
``` ```
Create a new folder in Infisical. ### Parameters
#### Parameters
<ParamField query="Parameters" type="object" optional> <ParamField query="Parameters" type="object" optional>
<Expandable title="properties"> <Expandable title="properties">
@@ -494,8 +544,11 @@ Create a new folder in Infisical.
</ParamField> </ParamField>
###
### Update Folder
`client.Folders().Update(options)`
### client.Folders().Update(options) Update an existing folder in Infisical.
```go ```go
folder, err := client.Folders().Update(infisical.UpdateFolderOptions{ folder, err := client.Folders().Update(infisical.UpdateFolderOptions{
@@ -507,9 +560,7 @@ folder, err := client.Folders().Update(infisical.UpdateFolderOptions{
}) })
``` ```
Update an existing folder in Infisical. ### Parameters
#### Parameters
<ParamField query="Parameters" type="object" optional> <ParamField query="Parameters" type="object" optional>
<Expandable title="properties"> <Expandable title="properties">
@@ -531,7 +582,11 @@ Update an existing folder in Infisical.
</Expandable> </Expandable>
</ParamField> </ParamField>
### client.Folders().Delete(options) ###
### Delete Folder
`client.Folders().Delete(options)`
Delete a folder in Infisical.
```go ```go
deletedFolder, err := client.Folders().Delete(infisical.DeleteFolderOptions{ deletedFolder, err := client.Folders().Delete(infisical.DeleteFolderOptions{
@@ -544,9 +599,7 @@ deletedFolder, err := client.Folders().Delete(infisical.DeleteFolderOptions{
}) })
``` ```
Delete a folder in Infisical. ### Parameters
#### Parameters
<ParamField query="Parameters" type="object" optional> <ParamField query="Parameters" type="object" optional>
<Expandable title="properties"> <Expandable title="properties">
@@ -568,3 +621,5 @@ Delete a folder in Infisical.
</ParamField> </ParamField>
</Expandable> </Expandable>
</ParamField> </ParamField>