Add client package.
This commit is contained in:
137
client/example_test.go
Normal file
137
client/example_test.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package client_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/client"
|
||||
)
|
||||
|
||||
func Example() {
|
||||
// Create a new client with default settings
|
||||
c := client.NewClient()
|
||||
|
||||
// Create a context with timeout
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Authenticate with username and password
|
||||
tokenResp, err := c.LoginWithPassword(ctx, "username", "password")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to login: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Authenticated with token: %s\n", tokenResp.Token)
|
||||
fmt.Printf("Token expires at: %s\n", time.Unix(tokenResp.Expires, 0).Format(time.RFC3339))
|
||||
|
||||
// If TOTP is enabled, verify the TOTP code
|
||||
if tokenResp.TOTPEnabled {
|
||||
fmt.Println("TOTP is enabled, please enter your TOTP code")
|
||||
totpCode := "123456" // In a real application, this would be user input
|
||||
|
||||
totpResp, err := c.VerifyTOTP(ctx, "username", totpCode)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to verify TOTP: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("TOTP verified, new token: %s\n", totpResp.Token)
|
||||
fmt.Printf("Token expires at: %s\n", time.Unix(totpResp.Expires, 0).Format(time.RFC3339))
|
||||
}
|
||||
|
||||
// Get database credentials
|
||||
dbCreds, err := c.GetDatabaseCredentials(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get database credentials: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Database Host: %s\n", dbCreds.Host)
|
||||
fmt.Printf("Database Port: %d\n", dbCreds.Port)
|
||||
fmt.Printf("Database Name: %s\n", dbCreds.Name)
|
||||
fmt.Printf("Database User: %s\n", dbCreds.User)
|
||||
fmt.Printf("Database Password: %s\n", dbCreds.Password)
|
||||
|
||||
// Example of authenticating with a token
|
||||
tokenClient := client.NewClient()
|
||||
tokenResp, err = tokenClient.LoginWithToken(ctx, "username", "existing-token")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to login with token: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Authenticated with token: %s\n", tokenResp.Token)
|
||||
fmt.Printf("Token expires at: %s\n", time.Unix(tokenResp.Expires, 0).Format(time.RFC3339))
|
||||
}
|
||||
|
||||
func ExampleClient_LoginWithPassword() {
|
||||
c := client.NewClient(
|
||||
client.WithBaseURL("https://mcias.example.com"),
|
||||
)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
tokenResp, err := c.LoginWithPassword(ctx, "username", "password")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to login: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Authenticated with token: %s\n", tokenResp.Token)
|
||||
fmt.Printf("Token expires at: %s\n", time.Unix(tokenResp.Expires, 0).Format(time.RFC3339))
|
||||
|
||||
if tokenResp.TOTPEnabled {
|
||||
fmt.Println("TOTP verification required")
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleClient_LoginWithToken() {
|
||||
c := client.NewClient()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
tokenResp, err := c.LoginWithToken(ctx, "username", "existing-token")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to login with token: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Authenticated with token: %s\n", tokenResp.Token)
|
||||
fmt.Printf("Token expires at: %s\n", time.Unix(tokenResp.Expires, 0).Format(time.RFC3339))
|
||||
}
|
||||
|
||||
func ExampleClient_VerifyTOTP() {
|
||||
c := client.NewClient()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
totpResp, err := c.VerifyTOTP(ctx, "username", "123456")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to verify TOTP: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("TOTP verified, token: %s\n", totpResp.Token)
|
||||
fmt.Printf("Token expires at: %s\n", time.Unix(totpResp.Expires, 0).Format(time.RFC3339))
|
||||
}
|
||||
|
||||
func ExampleClient_GetDatabaseCredentials() {
|
||||
// Create a client with pre-configured authentication
|
||||
c := client.NewClient(
|
||||
client.WithUsername("username"),
|
||||
client.WithToken("existing-token"),
|
||||
)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
dbCreds, err := c.GetDatabaseCredentials(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get database credentials: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Database Host: %s\n", dbCreds.Host)
|
||||
fmt.Printf("Database Port: %d\n", dbCreds.Port)
|
||||
fmt.Printf("Database Name: %s\n", dbCreds.Name)
|
||||
fmt.Printf("Database User: %s\n", dbCreds.User)
|
||||
fmt.Printf("Database Password: %s\n", dbCreds.Password)
|
||||
}
|
||||
Reference in New Issue
Block a user