2021-12-02 04:00:33 +00:00
|
|
|
package ficsit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Khan/genqlient/graphql"
|
2022-10-14 16:11:16 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-12-02 04:00:33 +00:00
|
|
|
"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)
|
|
|
|
return rt, errors.Wrap(err, "failed roundtrip")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|