github.com/fastly/go-fastly@v1.18.0/fastly/package.go (about) 1 package fastly 2 3 import ( 4 "fmt" 5 "io" 6 "time" 7 ) 8 9 // Package is a container for data returned about a package. 10 type Package struct { 11 ID string 12 ServiceID string `mapstructure:"service_id"` 13 Version int 14 Metadata PackageMetadata 15 CreatedAt *time.Time `mapstructure:"created_at"` 16 UpdatedAt *time.Time `mapstructure:"updated_at"` 17 DeletedAt *time.Time `mapstructure:"deleted_at"` 18 } 19 20 // Package is a container for metadata returned about a package. 21 // It is a separate struct to allow correct serialisation by mapstructure - 22 // the raw data is returned as a json sub-block. 23 type PackageMetadata struct { 24 Name string 25 Description string 26 Authors []string 27 Language string 28 Size int64 29 HashSum string 30 } 31 32 // GetPackageInput is used as input to the GetPackage function. 33 type GetPackageInput struct { 34 // Service is the ID of the service. 35 // Version is the specific configuration version. 36 // Both fields are required. 37 Service string `mapstructure:"service_id"` 38 Version int `mapstructure:"version"` 39 } 40 41 // GetPackage retrieves package information for the given service and version. 42 func (c *Client) GetPackage(i *GetPackageInput) (*Package, error) { 43 path, err := MakePackagePath(i.Service, i.Version) 44 if err != nil { 45 return nil, err 46 } 47 48 resp, err := c.Get(path, nil) 49 if err != nil { 50 return nil, err 51 } 52 53 return PopulatePackage(resp.Body) 54 } 55 56 // UpdatePackageInput is used as input to the UpdatePackage function. 57 type UpdatePackageInput struct { 58 // Service is the ID of the service. 59 // Version is the specific configuration version. 60 // Both fields are required. 61 Service string `mapstructure:"service_id"` 62 Version int `mapstructure:"version"` 63 64 // PackagePath is the local filesystem path to the package to upload. 65 PackagePath string 66 } 67 68 // UpdatePackage updates a package for a specific version. 69 func (c *Client) UpdatePackage(i *UpdatePackageInput) (*Package, error) { 70 71 urlPath, err := MakePackagePath(i.Service, i.Version) 72 if err != nil { 73 return nil, err 74 } 75 76 resp, err := c.PutFormFile(urlPath, i.PackagePath, "package", nil) 77 if err != nil { 78 return nil, err 79 } 80 81 return PopulatePackage(resp.Body) 82 } 83 84 // MakePackagePath ensures we create the correct REST path for referencing packages in the API. 85 func MakePackagePath(Service string, Version int) (string, error) { 86 if Service == "" { 87 return "", ErrMissingService 88 } 89 if Version == 0 { 90 return "", ErrMissingVersion 91 } 92 return fmt.Sprintf("/service/%s/version/%d/package", Service, Version), nil 93 } 94 95 // PopulatePackage encapsulates the decoding of returned package data. 96 func PopulatePackage(body io.ReadCloser) (*Package, error) { 97 var p *Package 98 if err := decodeBodyMap(body, &p); err != nil { 99 return nil, err 100 } 101 return p, nil 102 }