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

     1  // Package papi provides access to the Akamai Property APIs
     2  package papi
     3  
     4  import (
     5  	"errors"
     6  	"net/http"
     7  
     8  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session"
     9  	"github.com/spf13/cast"
    10  )
    11  
    12  var (
    13  	// ErrStructValidation is returned when given struct validation failed
    14  	ErrStructValidation = errors.New("struct validation")
    15  
    16  	// ErrNotFound is returned when requested resource was not found
    17  	ErrNotFound = errors.New("resource not found")
    18  
    19  	// ErrSBDNotEnabled indicates that secure-by-default is not enabled on the given account
    20  	ErrSBDNotEnabled = errors.New("secure-by-default is not enabled")
    21  
    22  	// ErrDefaultCertLimitReached indicates that the limit for DEFAULT certificates has been reached
    23  	ErrDefaultCertLimitReached = errors.New("the limit for DEFAULT certificates has been reached")
    24  
    25  	// ErrMissingComplianceRecord is returned when compliance record is required and is not provided
    26  	ErrMissingComplianceRecord = errors.New("compliance record must be specified")
    27  )
    28  
    29  type (
    30  	// PAPI is the papi api interface
    31  	PAPI interface {
    32  		Activations
    33  		ClientSettings
    34  		Contracts
    35  		CPCodes
    36  		EdgeHostnames
    37  		Groups
    38  		Includes
    39  		IncludeRules
    40  		IncludeActivations
    41  		IncludeVersions
    42  		Products
    43  		Properties
    44  		PropertyRules
    45  		PropertyVersionHostnames
    46  		PropertyVersions
    47  		RuleFormats
    48  		Search
    49  	}
    50  
    51  	papi struct {
    52  		session.Session
    53  		usePrefixes bool
    54  	}
    55  
    56  	// Option defines a PAPI option
    57  	Option func(*papi)
    58  
    59  	// ClientFunc is a papi client new method, this can used for mocking
    60  	ClientFunc func(sess session.Session, opts ...Option) PAPI
    61  
    62  	// Response is a base PAPI Response type
    63  	Response struct {
    64  		AccountID  string   `json:"accountId,omitempty"`
    65  		ContractID string   `json:"contractId,omitempty"`
    66  		GroupID    string   `json:"groupId,omitempty"`
    67  		Etag       string   `json:"etag,omitempty"`
    68  		Errors     []*Error `json:"errors,omitempty"`
    69  		Warnings   []*Error `json:"warnings,omitempty"`
    70  	}
    71  )
    72  
    73  // Client returns a new papi Client instance with the specified controller
    74  func Client(sess session.Session, opts ...Option) PAPI {
    75  	p := &papi{
    76  		Session:     sess,
    77  		usePrefixes: true,
    78  	}
    79  
    80  	for _, opt := range opts {
    81  		opt(p)
    82  	}
    83  	return p
    84  }
    85  
    86  // WithUsePrefixes sets the `PAPI-Use-Prefixes` header on requests
    87  // See: https://techdocs.akamai.com/property-mgr/reference/id-prefixes
    88  func WithUsePrefixes(usePrefixes bool) Option {
    89  	return func(p *papi) {
    90  		p.usePrefixes = usePrefixes
    91  	}
    92  }
    93  
    94  // Exec overrides the session.Exec to add papi options
    95  func (p *papi) Exec(r *http.Request, out interface{}, in ...interface{}) (*http.Response, error) {
    96  	// explicitly add the PAPI-Use-Prefixes header
    97  	r.Header.Set("PAPI-Use-Prefixes", cast.ToString(p.usePrefixes))
    98  
    99  	return p.Session.Exec(r, out, in...)
   100  }