session

package
v7.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2024 License: Apache-2.0 Imports: 13 Imported by: 17

README

session package

This library provides a simple and consistent REST over HTTP library for accessing Akamai Endpoints

Depedencies

This library is dependent on the github.com/akamai/AkamaiOPEN-edgegrid-golang/pkg/edgegrid interface.

Basic Example

func main() {
     edgerc := Must(New())

     s, err := session.New(
         session.WithConfig(edgerc),
     )
     if err != nil {
         panic(err)
     }

     var contracts struct {
		AccountID string         `json:"accountId"`
		Contracts ContractsItems `json:"contracts"`
        Items []struct {
            ContractID       string `json:"contractId"`
		    ContractTypeName string `json:"contractTypeName"`
        } `json:"items"`
     }

     req, _ := http.NewRequest(http.MethodGet, "/papi/v1/contracts", nil)

     _, err := s.Exec(r, &contracts)
     if err != nil {
         panic(err);
     }

     // do something with contracts
}
        

Library Logging

The session package supports the structured logging interface from github.com/apex. These can be applied globally to the session or to the request context.

Adding a logger to the session
    s, err := session.New(
         session.WithConfig(edgerc),
         session.WithLog(log.Log),
     )
     if err != nil {
         panic(err)
     }
Request logging

The logger can be overidden for a specific request like this

    req, _ := http.NewRequest(http.MethodGet, "/papi/v1/contracts", nil)

    req = req.WithContext(
        session.ContextWithOptions(request.Context(),
            session.WithContextLog(otherlog),
        )

Custom request headers

The context can also be updated to pass special http headers when necessary

    customHeader := make(http.Header)
    customHeader.Set("X-Custom-Header", "some custom value")

    req = req.WithContext(
        session.ContextWithOptions(request.Context(),
            session.WithContextHeaders(customHeader),
        )

Documentation

Overview

Package session provides the base secure http client and request management for akamai apis

Index

Constants

View Source
const (
	// Version is the client version
	Version = "7.0.0"
)

Variables

View Source
var (
	// ErrInvalidArgument is returned when invalid number of arguments were supplied to a function
	ErrInvalidArgument = errors.New("invalid arguments provided")
	// ErrMarshaling represents marshaling error
	ErrMarshaling = errors.New("marshaling input")
	// ErrUnmarshaling represents unmarshaling error
	ErrUnmarshaling = errors.New("unmarshaling output")
)

Functions

func ContextWithOptions

func ContextWithOptions(ctx context.Context, opts ...ContextOption) context.Context

ContextWithOptions adds request specific options to the context This log will debug the request only using the provided log

Types

type ContextOption

type ContextOption func(*contextOptions)

ContextOption are options on the context

func WithContextHeaders

func WithContextHeaders(h http.Header) ContextOption

WithContextHeaders sets the context headers

func WithContextLog

func WithContextLog(l log.Interface) ContextOption

WithContextLog provides a context specific logger

type Option

type Option func(*session)

Option defines a client option

func WithClient

func WithClient(client *http.Client) Option

WithClient creates a client using the specified http.Client

func WithHTTPTracing

func WithHTTPTracing(trace bool) Option

WithHTTPTracing sets the request and response dump for debugging

func WithLog

func WithLog(l log.Interface) Option

WithLog sets the log interface for the client

func WithRequestLimit

func WithRequestLimit(requestLimit int) Option

WithRequestLimit sets the maximum number of API calls that the provider will make per second.

func WithSigner

func WithSigner(signer edgegrid.Signer) Option

WithSigner sets the request signer for the session

func WithUserAgent

func WithUserAgent(u string) Option

WithUserAgent sets the user agent string for the client

type Session

type Session interface {
	// Exec will sign and execute a request returning the response
	// The response body will be unmarshaled in to out
	// Optionally the in value will be marshaled into the body
	Exec(r *http.Request, out interface{}, in ...interface{}) (*http.Response, error)

	// Sign will only sign a request, this is useful for circumstances
	// when the caller wishes to manage the http client
	Sign(r *http.Request) error

	// Log returns the logging interface for the session
	// If provided all debugging will output to this log interface
	Log(ctx context.Context) log.Interface

	// Client return the session http client
	Client() *http.Client
}

Session is the interface that is used by the pa This allows the client itself to be more extensible and readily testable, ets.

func Must

func Must(sess Session, err error) Session

Must is a helper tthat will result in a panic if an error is returned ex. sess := Must(New())

func New

func New(opts ...Option) (Session, error)

New returns a new session

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL