github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/registry/regclient/client.go (about) 1 // Package regclient defines a client for interacting with a registry server 2 package regclient 3 4 import ( 5 "errors" 6 "net/http" 7 "net/url" 8 9 golog "github.com/ipfs/go-log" 10 qhttp "github.com/qri-io/qri/lib/http" 11 ) 12 13 var ( 14 // ErrNoRegistry indicates that no registry has been specified 15 // all client methods MUST return ErrNoRegistry for all method calls 16 // when config.Registry.Location is an empty string 17 ErrNoRegistry = errors.New("registry: no registry specified") 18 // ErrNoConnection indicates no path to the registry can be found 19 ErrNoConnection = errors.New("registry: no connection") 20 // ErrNotRegistered indicates this client is not registered 21 ErrNotRegistered = errors.New("registry: not registered") 22 23 // HTTPClient is hoisted here in case you'd like to use a different client instance 24 // by default we just use http.DefaultClient 25 HTTPClient = http.DefaultClient 26 27 log = golog.Logger("registry") 28 ) 29 30 // Client wraps a registry configuration with methods for interacting 31 // with the configured registry 32 type Client struct { 33 cfg *Config 34 httpClient *qhttp.Client 35 } 36 37 // Config encapsulates options for working with a registry 38 type Config struct { 39 // Location is the URL base to call to 40 Location string 41 } 42 43 // NewClient creates a registry from a provided Registry configuration 44 func NewClient(cfg *Config) *Client { 45 c := &Client{cfg: cfg} 46 if c.cfg.Location == "" { 47 return c 48 } 49 50 u, err := url.Parse(c.cfg.Location) 51 if err != nil { 52 log.Debugf(ErrNoRegistry.Error()) 53 } else { 54 us := u.String() 55 if u.Scheme != "" { 56 us = us[len(u.Scheme)+len("://"):] 57 } 58 httpClient := &qhttp.Client{ 59 Address: us, 60 Protocol: u.Scheme, 61 } 62 c.httpClient = httpClient 63 } 64 65 return c 66 }