package main import ( "encoding/json" "fmt" "net/http" "time" "github.com/hashicorp/golang-lru/v2/expirable" ) var ( pkApi = NewPluralKit() ) type PluralKitMember struct { UUID string `json:"uuid"` Name string `json:"name"` } type PluralKit struct { Client http.Client Cache *expirable.LRU[string, *PluralKitMember] BaseURL string } func NewPluralKit() PluralKit { return PluralKit{ BaseURL: "https://api.pluralkit.me/v2", Client: http.Client{Timeout: time.Second * 30}, Cache: expirable.NewLRU[string, *PluralKitMember](20, nil, time.Hour), } } func (pk *PluralKit) GetMember(id string) (*PluralKitMember, error) { cached, ok := pk.Cache.Get(id) if ok { return cached, nil } resp, err := pk.Client.Get(fmt.Sprintf("%s/members/%s", pk.BaseURL, id)) if err != nil { return nil, fmt.Errorf("fetching pluralkit member %s failed: %w", id, err) } if resp.StatusCode != 200 { return nil, fmt.Errorf("pluralkit did not find member %ss", id) } var member PluralKitMember err = json.NewDecoder(resp.Body).Decode(&member) if err != nil { return nil, fmt.Errorf("malformed json: %w", err) } pk.Cache.Add(id, &member) return &member, nil }