github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.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  	// See: https://developer.akamai.com/api/core_features/property_manager/v1.html#contractsgroup
    13  	Contracts interface {
    14  		// GetContract provides a read-only list of contract names and identifiers
    15  		// See: https://developer.akamai.com/api/core_features/property_manager/v1.html#getcontracts
    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 represents a collection of property manager contracts
    31  	// This is the reponse to the /papi/v1/contracts request
    32  	GetContractsResponse struct {
    33  		AccountID string         `json:"accountId"`
    34  		Contracts ContractsItems `json:"contracts"`
    35  	}
    36  )
    37  
    38  var (
    39  	// ErrGetContracts represents error when fetching contracts fails
    40  	ErrGetContracts = errors.New("fetching contracts")
    41  )
    42  
    43  func (p *papi) GetContracts(ctx context.Context) (*GetContractsResponse, error) {
    44  	var contracts GetContractsResponse
    45  
    46  	logger := p.Log(ctx)
    47  	logger.Debug("GetContracts")
    48  
    49  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/papi/v1/contracts", nil)
    50  	if err != nil {
    51  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetContracts, err)
    52  	}
    53  
    54  	resp, err := p.Exec(req, &contracts)
    55  	if err != nil {
    56  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetContracts, err)
    57  	}
    58  
    59  	if resp.StatusCode != http.StatusOK {
    60  		return nil, fmt.Errorf("%s: %w", ErrGetContracts, p.Error(resp))
    61  	}
    62  
    63  	return &contracts, nil
    64  }