Select Git revision
0011_donateprojectpage_form_preselected.py
octopus.go 1.47 KiB
package octopus
import (
"os"
"fmt"
"log"
"errors"
"context"
"github.com/google/uuid"
"github.com/machinebox/graphql"
)
func ProfilePhoto (id string) (string, error) {
var keyname string
var response struct {
AllPeople struct {
Edges []struct {
Node struct {
ProfilePhoto string `json:"profilePhoto"`
} `json:"node"`
} `json:"edges"`
} `json:"allPeople"`
}
if _, err := uuid.Parse(id); err == nil {
keyname = "keycloakId"
} else {
keyname = "username"
}
// GraphAPI klient
octopus := graphql.NewClient(os.Getenv("OCTOPUS"))
if os.Getenv("DEBUG") != "" {
octopus.Log = func(s string) { log.Println(s) }
}
// Request
req := graphql.NewRequest(fmt.Sprintf(
`query profilePhoto($username: String!) {
allPeople(filters: {%s: {iExact: $username}}) {
edges { node { profilePhoto } }
}}
`, keyname ))
req.Var("username", id)
// define a Context for the request
ctx := context.Background()
// run it and capture the response
if err := octopus.Run(ctx, req, &response); err != nil {
log.Fatal(err)
}
if people := response.AllPeople.Edges; len(people) == 1 {
return people[0].Node.ProfilePhoto, nil
} else {
return "", errors.New("User not found")
}
}