rest

package
v6.25.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// GetMethod is a wrapper with expected status codes around the HTTP "GET" method
	GetMethod = Method{Method: "GET", ExpectedStatusCodes: []int{200}}
	// PostMethod is a wrapper with expected status codes around the HTTP "POST" method
	PostMethod = Method{Method: "POST", ExpectedStatusCodes: []int{200, 201}}
	// PutMethod is a wrapper with expected status codes around the HTTP "PUT" method
	PutMethod = Method{Method: "PUT", ExpectedStatusCodes: []int{204}}
	// PatchMethod is a wrapper with expected status codes around the HTTP "PATCH" method
	PatchMethod = Method{Method: "PATCH", ExpectedStatusCodes: []int{204}}
	// DeleteMethod is a wrapper with expected status codes around the HTTP "DELETE" method
	DeleteMethod = Method{Method: "DELETE", ExpectedStatusCodes: []int{204}}
)

Functions

This section is empty.

Types

type Date

type Date struct {
	// Time item containing the actual parsed time object
	time.Time
}

Date is defined because the transip api server returns date strings, not parsed by golang by default. So we need to do manual time parsing, by defining our own date struct encapsulating time.Time.

func (*Date) UnmarshalJSON

func (td *Date) UnmarshalJSON(input []byte) error

UnmarshalJSON parses date strings returned by the transip api

type Error

type Error struct {
	// Message contains the error from the api as a string
	Message string `json:"error"`
	// StatusCode contains a HTTP status code that the api server responded with
	StatusCode int
}

Error is used to unpack every error returned by the api

func (*Error) Error

func (e *Error) Error() string

type Method

type Method struct {
	// Method is where a HTTP method like "GET" would go
	Method string
	// ExpectedStatusCodes are the expected status codes with which
	// you can check if the response status code is correct
	ExpectedStatusCodes []int
}

Method is a struct which is used by the client to present a HTTP method of choice and the expected return status codes on which you can check to see if the response is correct, thus not an error.

func (*Method) StatusCodeOK

func (r *Method) StatusCodeOK(statusCode int) bool

StatusCodeOK returns true when the status code is correct This method used by the rest client to check if the given status code is correct.

type Request

type Request struct {
	// Endpoint is the api endpoint, without the server, which we will receive the request,
	// like: '/products'
	Endpoint string
	// Parameters is a map of strings (url.Values) that will be used to add
	// http query strings to the request, like '/domains?tags=123'
	Parameters url.Values
	// Body is left as an interface because the Request is not coupled to any specific Request.Body struct
	Body interface{}
	// TestMode is used when users want to tinker with the api without touching their real data
	TestMode bool
}

Request will be used by all repositories and the client. The Request struct can be transformed to a http request with method, url and optional body.

func (*Request) GetBodyReader

func (r *Request) GetBodyReader() (io.Reader, error)

GetBodyReader returns an io.Reader for the json marshalled body of this request this will be used by the writer used in the client.

func (*Request) GetHTTPRequest

func (r *Request) GetHTTPRequest(basePath string, method string) (*http.Request, error)

GetHTTPRequest generates and returns a http.Request object. It does this with the Request struct and the basePath and method, that are provided by the client itself.

func (*Request) GetJSONBody

func (r *Request) GetJSONBody() ([]byte, error)

GetJSONBody returns the request object as a json byte array

type Response

type Response struct {
	Body            []byte
	StatusCode      int
	Method          Method
	ContentLocation string
}

Response will contain a body (which can be empty), status code and the Method. This struct will be used to decode a response from the api server.

func (*Response) ParseResponse

func (r *Response) ParseResponse(dest interface{}) error

ParseResponse will convert a Response struct to the given interface. When the rest response has no body it will return without filling the dest variable.

type Time

type Time struct {
	// Time item containing the actual parsed time object
	time.Time
}

Time is defined because the transip api server does not return a rfc 3339 time string and golang requires this. So we need to do manual time parsing, by defining our own time struct encapsulating time.Time.

func (*Time) UnmarshalJSON

func (tt *Time) UnmarshalJSON(input []byte) error

UnmarshalJSON parses datetime strings returned by the transip api

Jump to

Keyboard shortcuts

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