github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/papi/contract.go (about)

     1  package papi
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  )
     9  
    10  type (
    11  	// Contracts contains operations available on Contract resource
    12  	Contracts interface {
    13  		// GetContracts provides a read-only list of contract names and identifiers
    14  		//
    15  		// See: https://techdocs.akamai.com/property-mgr/reference/get-contracts
    16  		GetContracts(context.Context) (*GetContractsResponse, error)
    17  	}
    18  
    19  	// Contract represents a property contract resource
    20  	Contract struct {
    21  		ContractID       string `json:"contractId"`
    22  		ContractTypeName string `json:"contractTypeName"`
    23  	}
    24  
    25  	// ContractsItems is the response items array
    26  	ContractsItems struct {
    27  		Items []*Contract `json:"items"`
    28  	}
    29  
    30  	// GetContractsResponse is the response to the /papi/v1/contracts request
    31  	GetContractsResponse struct {
    32  		AccountID string         `json:"accountId"`
    33  		Contracts ContractsItems `json:"contracts"`
    34  	}
    35  )
    36  
    37  var (
    38  	// ErrGetContracts represents error when fetching contracts fails
    39  	ErrGetContracts = errors.New("fetching contracts")
    40  )
    41  
    42  func (p *papi) GetContracts(ctx context.Context) (*GetContractsResponse, error) {
    43  	var contracts GetContractsResponse
    44  
    45  	logger := p.Log(ctx)
    46  	logger.Debug("GetContracts")
    47  
    48  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/papi/v1/contracts", nil)
    49  	if err != nil {
    50  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetContracts, err)
    51  	}
    52  
    53  	resp, err := p.Exec(req, &contracts)
    54  	if err != nil {
    55  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetContracts, err)
    56  	}
    57  
    58  	if resp.StatusCode != http.StatusOK {
    59  		return nil, fmt.Errorf("%s: %w", ErrGetContracts, p.Error(resp))
    60  	}
    61  
    62  	return &contracts, nil
    63  }