github.com/akamai/AkamaiOPEN-edgegrid-golang/v5@v5.0.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/v5/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  
    26  type (
    27  	// PAPI is the papi api interface
    28  	PAPI interface {
    29  		Activations
    30  		ClientSettings
    31  		Contracts
    32  		CPCodes
    33  		EdgeHostnames
    34  		Groups
    35  		Includes
    36  		IncludeRules
    37  		IncludeActivations
    38  		IncludeVersions
    39  		Products
    40  		Properties
    41  		PropertyRules
    42  		PropertyVersionHostnames
    43  		PropertyVersions
    44  		RuleFormats
    45  		Search
    46  	}
    47  
    48  	papi struct {
    49  		session.Session
    50  		usePrefixes bool
    51  	}
    52  
    53  	// Option defines a PAPI option
    54  	Option func(*papi)
    55  
    56  	// ClientFunc is a papi client new method, this can used for mocking
    57  	ClientFunc func(sess session.Session, opts ...Option) PAPI
    58  
    59  	// Response is a base PAPI Response type
    60  	Response struct {
    61  		AccountID  string   `json:"accountId,omitempty"`
    62  		ContractID string   `json:"contractId,omitempty"`
    63  		GroupID    string   `json:"groupId,omitempty"`
    64  		Etag       string   `json:"etag,omitempty"`
    65  		Errors     []*Error `json:"errors,omitempty"`
    66  		Warnings   []*Error `json:"warnings,omitempty"`
    67  	}
    68  )
    69  
    70  // Client returns a new papi Client instance with the specified controller
    71  func Client(sess session.Session, opts ...Option) PAPI {
    72  	p := &papi{
    73  		Session:     sess,
    74  		usePrefixes: true,
    75  	}
    76  
    77  	for _, opt := range opts {
    78  		opt(p)
    79  	}
    80  	return p
    81  }
    82  
    83  // WithUsePrefixes sets the `PAPI-Use-Prefixes` header on requests
    84  // See: https://techdocs.akamai.com/property-mgr/reference/id-prefixes
    85  func WithUsePrefixes(usePrefixes bool) Option {
    86  	return func(p *papi) {
    87  		p.usePrefixes = usePrefixes
    88  	}
    89  }
    90  
    91  // Exec overrides the session.Exec to add papi options
    92  func (p *papi) Exec(r *http.Request, out interface{}, in ...interface{}) (*http.Response, error) {
    93  	// explicitly add the PAPI-Use-Prefixes header
    94  	r.Header.Set("PAPI-Use-Prefixes", cast.ToString(p.usePrefixes))
    95  
    96  	return p.Session.Exec(r, out, in...)
    97  }