github.com/newrelic/newrelic-client-go@v1.1.0/pkg/synthetics/synthetics.go (about)

     1  // Package synthetics provides a programmatic API for interacting with the New Relic Synthetics product.
     2  package synthetics
     3  
     4  import (
     5  	"strings"
     6  
     7  	"github.com/newrelic/newrelic-client-go/internal/http"
     8  	"github.com/newrelic/newrelic-client-go/pkg/config"
     9  	"github.com/newrelic/newrelic-client-go/pkg/logging"
    10  )
    11  
    12  // Synthetics is used to communicate with the New Relic Synthetics product.
    13  type Synthetics struct {
    14  	client http.Client
    15  	config config.Config
    16  	logger logging.Logger
    17  	pager  http.Pager
    18  }
    19  
    20  // ErrorResponse represents an error response from New Relic Synthetics.
    21  type ErrorResponse struct {
    22  	http.DefaultErrorResponse
    23  
    24  	Message            string        `json:"error,omitempty"`
    25  	Messages           []ErrorDetail `json:"errors,omitempty"`
    26  	ServerErrorMessage string        `json:"message,omitempty"`
    27  }
    28  
    29  // ErrorDetail represents an single error from New Relic Synthetics.
    30  type ErrorDetail struct {
    31  	Message string `json:"error,omitempty"`
    32  }
    33  
    34  // Error surfaces an error message from the New Relic Synthetics error response.
    35  func (e *ErrorResponse) Error() string {
    36  	if e.ServerErrorMessage != "" {
    37  		return e.ServerErrorMessage
    38  	}
    39  
    40  	if e.Message != "" {
    41  		return e.Message
    42  	}
    43  
    44  	if len(e.Messages) > 0 {
    45  		messages := []string{}
    46  		for _, m := range e.Messages {
    47  			messages = append(messages, m.Message)
    48  		}
    49  		return strings.Join(messages, ", ")
    50  	}
    51  
    52  	return ""
    53  }
    54  
    55  // New creates a new instance of ErrorResponse.
    56  func (e *ErrorResponse) New() http.ErrorResponse {
    57  	return &ErrorResponse{}
    58  }
    59  
    60  // New is used to create a new Synthetics client instance.
    61  func New(config config.Config) Synthetics {
    62  	client := http.NewClient(config)
    63  	client.SetAuthStrategy(&http.PersonalAPIKeyCapableV2Authorizer{})
    64  	client.SetErrorValue(&ErrorResponse{})
    65  
    66  	pkg := Synthetics{
    67  		client: client,
    68  		config: config,
    69  		logger: config.GetLogger(),
    70  		pager:  &http.LinkHeaderPager{},
    71  	}
    72  
    73  	return pkg
    74  }