github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/contracts.go (about) 1 package edgeworkers 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net/http" 8 ) 9 10 type ( 11 // Contracts is an edgeworkers contracts API interface 12 Contracts interface { 13 // ListContracts lists contract IDs that can be used to list resource tiers 14 // 15 // See: https://techdocs.akamai.com/edgeworkers/reference/get-contracts-1 16 ListContracts(context.Context) (*ListContractsResponse, error) 17 } 18 19 // ListContractsResponse represents a response object returned by ListContracts 20 ListContractsResponse struct { 21 ContractIDs []string `json:"contractIds"` 22 } 23 ) 24 25 var ( 26 // ErrListContracts is returned in case an error occurs on ListContracts operation 27 ErrListContracts = errors.New("list contracts") 28 ) 29 30 func (e *edgeworkers) ListContracts(ctx context.Context) (*ListContractsResponse, error) { 31 logger := e.Log(ctx) 32 logger.Debug("ListContracts") 33 34 uri := "/edgeworkers/v1/contracts" 35 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 36 if err != nil { 37 return nil, fmt.Errorf("%w: failed to create request: %s", ErrListContracts, err) 38 } 39 40 var result ListContractsResponse 41 resp, err := e.Exec(req, &result) 42 if err != nil { 43 return nil, fmt.Errorf("%w: request failed: %s", ErrListContracts, err) 44 } 45 46 if resp.StatusCode != http.StatusOK { 47 return nil, fmt.Errorf("%s: %w", ErrListContracts, e.Error(resp)) 48 } 49 50 return &result, nil 51 }