The Gandalf SDK for Go provides two main libraries: EyeOfSauron
for interacting with GraphQL APIs and Connect
for generating valid Connect URLs for account linking.
Visit the official Gandalf documentation here.
Package | Description | Links |
---|---|---|
EyeofSauron |
A command line tool for generating client code for Gandalf eyeofsauron . | Docs |
Connect |
A package for generating connect URL for Gandalf | Docs |
EyeOfSauron
completely abstracts away the complexity of authentication and interacting with Gandalf GraphQL APIs.
To install the EyeOfSauron
package, use the following command:
go get github.com/gandalf-network/gandalf-sdk-go/eyeofsauron
To generate the necessary files, use the following command:
go run github.com/gandalf-network/gandalf-sdk-go/eyeofsauron -f generated
-f, --folder [folder]
: Set the destination folder for the generated files.
Once you have successfully generated the necessary files and installed the required dependencies using EyeOfSauron
, you can proceed to use these files to interact with the API.
package main
import (
"log"
"github.com/gandalf-network/gandalf-sdk-go/connect"
"github.com/gandalf-network/gandalf-sdk-go/eyeofsauron/generated"
)
func main() {
eye, err := generated.NewEyeOfSauron("<YOUR_GANDALF_PRIVATE_KEY")
if err != nil {
log.Fatalf("failed to run gandalf client: %s", err)
}
}
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/gandalf-network/gandalf-sdk-go/eyeofsauron/generated"
)
func main() {
// initialize eyeofsauron object
...
// Get activity
response, err := eye.GetActivity(
context.Background(),
"MY_DATA_KEY",
[]generated.ActivityType{generated.ActivityTypeWatch},
generated.SourceNetflix,
10,
1,
)
if err != nil {
log.Fatalf("failed to get activity: %s", err)
}
for _, activity := range response.GetGetActivity().Data {
switch meta := activity.Metadata.(type) {
case *generated.GetActivityActivityResponseDataActivityMetadataAmazonActivityMetadata:
fmt.Println("Amazon Activity Metadata:")
printJSON(meta.AmazonActivityMetadata)
case *generated.GetActivityActivityResponseDataActivityMetadataInstacartActivityMetadata:
fmt.Println("Instacart Activity Metadata:")
printJSON(meta.InstacartActivityMetadata)
case *generated.GetActivityActivityResponseDataActivityMetadataNetflixActivityMetadata:
fmt.Println("Netflix Activity Metadata:")
printJSON(meta.NetflixActivityMetadata)
case *generated.GetActivityActivityResponseDataActivityMetadataPlaystationActivityMetadata:
fmt.Println("Playstation Activity Metadata:")
printJSON(meta.PlaystationActivityMetadata)
case *generated.GetActivityActivityResponseDataActivityMetadataUberActivityMetadata:
fmt.Println("Uber Activity Metadata:")
printJSON(meta.UberActivityMetadata)
case *generated.GetActivityActivityResponseDataActivityMetadataYoutubeActivityMetadata:
fmt.Println("YouTube Activity Metadata:")
printJSON(meta.YoutubeActivityMetadata)
default:
log.Printf("Unknown metadata type: %T\n", meta)
}
}
}
func printJSON(v interface{}) {
jsonData, err := json.MarshalIndent(v, "", " ")
if err != nil {
log.Printf("failed to marshal metadata: %v", err)
return
}
fmt.Println(string(jsonData))
}
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/gandalf-network/gandalf-sdk-go/eyeofsauron/generated"
)
func main() {
// initialize eyeofsauron object
...
// Lookup activity
response, err := eye.LookupActivity(
context.Background(),
"MY_DATA_KEY",
"ACTIVITY_ID",
)
if err != nil {
log.Fatalf("failed to lookup activity: %s", err)
}
metadata := response.LookupActivity.GetMetadata()
switch meta := metadata.(type) {
case *generated.LookupActivityMetadataAmazonActivityMetadata:
fmt.Println("Amazon Activity Metadata:")
printJSON(meta.AmazonActivityMetadata)
case *generated.LookupActivityMetadataInstacartActivityMetadata:
fmt.Println("Instacart Activity Metadata:")
printJSON(meta.InstacartActivityMetadata)
case *generated.LookupActivityMetadataNetflixActivityMetadata:
fmt.Println("Netflix Activity Metadata:")
printJSON(meta.NetflixActivityMetadata)
case *generated.LookupActivityMetadataPlaystationActivityMetadata:
fmt.Println("Playstation Activity Metadata:")
printJSON(meta.PlaystationActivityMetadata)
case *generated.LookupActivityMetadataUberActivityMetadata:
fmt.Println("Uber Activity Metadata:")
printJSON(meta.UberActivityMetadata)
case *generated.LookupActivityMetadataYoutubeActivityMetadata:
fmt.Println("YouTube Activity Metadata:")
printJSON(meta.YoutubeActivityMetadata)
default:
log.Printf("Unknown metadata type: %T\n", meta)
}
}
func printJSON(v interface{}) {
jsonData, err := json.MarshalIndent(v, "", " ")
if err != nil {
log.Printf("failed to marshal metadata: %v", err)
return
}
fmt.Println(string(jsonData))
}
func main() {
// initialize eyeofsauron object
...
response, err := eye.GetTraits(context.Background(), "MY_DATA_KEY", generated.SourceNetflix, []generated.TraitLabel{generated.TraitLabelPlan})
if err != nil {
log.Fatalf("failed to get traits: %s", err)
}
fmt.Println("Get Traits", response.GetGetTraits())
}
func main() {
// initialize eyeofsauron object
...
traitID, err := uuid.Parse("MY_TRAIT_ID")
if err != nil {
log.Fatalf("failed to parse string to uuid")
}
response, err := eye.LookupTrait(context.Background(), "MY_DATA_KEY", traitID)
if err != nil {
log.Fatalf("failed to lookup trait: %s", err)
}
fmt.Println("Lookup Trait", response.GetLookupTrait())
}
Connect
is a library in Go that makes it easier to generate valid Connect URLs that let your users link their accounts to Gandalf. To use this library, follow the installation and usage instructions provided in the documentation.
To install the Connect
package, use the following command:
go get github.com/gandalf-network/gandalf-sdk-go/connect
const publicKey = "0x036518f1c7a10fc77f835becc0aca9916c54505f771c82d87dd5943bb01ba5ca08";
const redirectURL = "https://example.com"
func main() {
// Define the input data
services := connect.InputData{
"netflix": connect.Service{
Traits: []string{"rating"},
Activities: []string{"watch"},
},
}
// Define the config parameters
config := connect.Config{
PublicKey: publicKey,
RedirectURL: redirectURL,
Data: services,
}
// Initialization
conn, err := connect.NewConnect(config)
if err != nil {
log.Fatalf("An error occurred with initializing connect: %v", err)
}
// Call the GenerateURL method
url, err := conn.GenerateURL()
if err != nil {
log.Fatalf("An error occurred generating url: %v", err)
}
fmt.Println("URL => ", url)
// Call the GenerateQRCode method
qrCode, err := conn.GenerateQRCode()
if err != nil {
log.Fatalf("An error occurred generating QR Code url: %v", err)
}
fmt.Println("Base64 QR Code => ", qrCode)
}
import (
...
"github.com/gandalf-network/gandalf-sdk-go/eyeofsauron/generated"
)
func main() {
// Define the input data
services := connect.InputData{
"netflix": connect.Service{
Traits: []string{"rating"},
Activities: []string{"watch"},
},
}
// Define the config parameters
config := connect.Config{
PublicKey: publicKey,
RedirectURL: redirectURL,
Data: services,
Platform: connect.PlatformTypeAndroid,
}
// Call the GenerateURL method for Android
androidUrl, err := conn.GenerateURL()
if err != nil {
log.Fatalf("An error occurred generating url: %v", err)
}
fmt.Println("URL => ", androidUrl)
}