ficsit-cli-flake/ficsit/root.go

38 lines
713 B
Go
Raw Normal View History

2021-12-02 04:00:33 +00:00
package ficsit
import (
"fmt"
2021-12-02 04:00:33 +00:00
"net/http"
"github.com/Khan/genqlient/graphql"
"github.com/spf13/viper"
)
2022-10-14 16:11:16 +00:00
type AuthedTransport struct {
Wrapped http.RoundTripper
}
func (t *AuthedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
key := viper.GetString("api-key")
if key != "" {
req.Header.Set("Authorization", key)
}
rt, err := t.Wrapped.RoundTrip(req)
if err != nil {
return nil, fmt.Errorf("failed roundtrip: %w", err)
}
return rt, nil
2022-10-14 16:11:16 +00:00
}
2021-12-02 04:00:33 +00:00
func InitAPI() graphql.Client {
2022-10-14 16:11:16 +00:00
httpClient := http.Client{
Transport: &AuthedTransport{
Wrapped: http.DefaultTransport,
},
}
return graphql.NewClient(viper.GetString("api-base")+viper.GetString("graphql-api"), &httpClient)
2021-12-02 04:00:33 +00:00
}