github.com/polygon-io/client-go@v1.16.4/rest/models/response.go (about)

     1  package models
     2  
     3  import "fmt"
     4  
     5  // BaseResponse has all possible attributes that any response can use. It's intended to be embedded in a domain specific
     6  // response struct.
     7  type BaseResponse struct {
     8  	PaginationHooks
     9  
    10  	// The status of this request's response.
    11  	Status string `json:"status,omitempty"`
    12  
    13  	// A request id assigned by the server.
    14  	RequestID string `json:"request_id,omitempty"`
    15  
    16  	// The total number of results for this request.
    17  	Count int `json:"count,omitempty"`
    18  
    19  	// A response message for successful requests.
    20  	Message string `json:"message,omitempty"`
    21  
    22  	// An error message for unsuccessful requests.
    23  	ErrorMessage string `json:"error,omitempty"`
    24  }
    25  
    26  // PaginationHooks are links to next and/or previous pages. Embed this struct into an API response if the endpoint
    27  // supports pagination.
    28  type PaginationHooks struct {
    29  	// If present, this value can be used to fetch the next page of data.
    30  	NextURL string `json:"next_url,omitempty"`
    31  }
    32  
    33  func (p PaginationHooks) NextPage() string {
    34  	return p.NextURL
    35  }
    36  
    37  // ErrorResponse represents an API response with an error status code.
    38  type ErrorResponse struct {
    39  	BaseResponse
    40  
    41  	// An HTTP status code for unsuccessful requests.
    42  	StatusCode int
    43  }
    44  
    45  // Error returns the details of an error response.
    46  func (e *ErrorResponse) Error() string {
    47  	return fmt.Sprintf("bad status with code '%d': message '%s': request ID '%s': internal status: '%s'", e.StatusCode, e.ErrorMessage, e.RequestID, e.Status)
    48  }