github.com/instill-ai/component@v0.16.0-beta/pkg/connector/huggingface/v0/client.go (about) 1 package huggingface 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/go-resty/resty/v2" 9 "github.com/instill-ai/component/pkg/connector/util/httpclient" 10 "go.uber.org/zap" 11 "google.golang.org/protobuf/types/known/structpb" 12 ) 13 14 const ( 15 modelsPath = "/models/" 16 ) 17 18 func newClient(config *structpb.Struct, logger *zap.Logger) *httpclient.Client { 19 c := httpclient.New("Hugging Face", getBaseURL(config), 20 httpclient.WithLogger(logger), 21 httpclient.WithEndUserError(new(errBody)), 22 ) 23 24 c.SetAuthToken(getAPIKey(config)) 25 26 return c 27 } 28 29 type errBody struct { 30 // Error can be either a string or a string array. 31 Error json.RawMessage `json:"error,omitempty"` 32 } 33 34 func (e errBody) Message() string { 35 var errStr string 36 if err := json.Unmarshal(e.Error, &errStr); err == nil { // Error is string 37 return errStr 38 } 39 40 var errSlice []string 41 if err := json.Unmarshal(e.Error, &errSlice); err == nil { // Error is string slice 42 return fmt.Sprintf("[%s]", strings.Join(errSlice, ", ")) 43 } 44 45 return "" 46 } 47 48 func post(req *resty.Request, path string) (*resty.Response, error) { 49 resp, err := req.Post(path) 50 if err != nil { 51 err = httpclient.WrapURLError(err) 52 } 53 54 return resp, err 55 }