github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/pinning/service.go (about) 1 package pinning 2 3 import ( 4 "fmt" 5 6 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config" 7 ) 8 9 type ServiceType int 10 11 const ( 12 NoType ServiceType = iota 13 Pinata 14 Local 15 ) 16 17 type Service struct { 18 Apikey string 19 Secret string 20 Jwt string 21 HeaderFunc func(s *Service, contentType string) map[string]string 22 } 23 24 func NewService(chain string, serviceType ServiceType) (Service, error) { 25 apiKey, secret, jwt := config.GetKey("pinata").ApiKey, config.GetKey("pinata").Secret, config.GetKey("pinata").Jwt 26 switch serviceType { 27 case Local: 28 return Service{}, nil 29 case Pinata: 30 return Service{ 31 Apikey: apiKey, 32 Secret: secret, 33 Jwt: jwt, 34 HeaderFunc: pinataHeaders, 35 }, nil 36 default: 37 return Service{}, fmt.Errorf("unknown pinning service type %d", serviceType) 38 } 39 } 40 41 func pinataHeaders(s *Service, contentType string) map[string]string { 42 headers := make(map[string]string) 43 headers["Content-Type"] = contentType 44 if s.Secret != "" { 45 headers["pinata_secret_api_key"] = s.Secret 46 headers["pinata_api_key"] = s.Apikey 47 } else { 48 headers["authorization"] = "Bearer " + s.Jwt 49 } 50 return headers 51 }