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