utils

package
v0.0.0-...-3bbfe4c Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckForErrors

func CheckForErrors(resp *http.Response) ([]byte, error)

CheckForErrors reads the entire response body and checks the status code of the response. If the status code is not 200, a error is returned. Otherwise, the response body is returned.

func GetImageBase64

func GetImageBase64(endpoint, filename, id string) (string, error)

GetImageBase64 implements read-through caching in which the image's base64 string is retrieved from the cache first or the network if not found in the cache.

func GetURLQueryParams

func GetURLQueryParams(s string) (url.Values, error)

GetURLQueryParams extracts the query parameters from a url string and returns a map of strings.

func JSONResponse

func JSONResponse(w http.ResponseWriter, bs []byte) error

JSONResponse sends a JSON response to the client.

func Logger

func Logger() *zap.SugaredLogger

func NewHTTPError

func NewHTTPError(err error, status int, detail string) error

NewHTTPError returns a new HTTPError.

func SendGETRequest

func SendGETRequest(endpoint string, target interface{}) ([]byte, error)

SendGETRequest makes an HTTP GET request and decodes the JSON response into the provided target interface.

func SendPOSTRequest

func SendPOSTRequest(
	endpoint string,
	formValues map[string]string,
	target interface{},
) ([]byte, error)

Types

type ClientError

type ClientError interface {
	Error() string
	ResponseBody() ([]byte, error)
	ResponseHeaders() (int, map[string]string)
}

ClientError is an error whose details is to be shared with the client.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}
var (
	Client HTTPClient = &http.Client{Timeout: 60 * time.Second}
)

type HTTPError

type HTTPError struct {
	Cause  error  `json:"-"`
	Detail string `json:"detail"`
	Status int    `json:"-"`
}

HTTPError implements ClientError interface.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error causes HTTPError to satisfy the error interface.

func (*HTTPError) ResponseBody

func (e *HTTPError) ResponseBody() ([]byte, error)

ResponseBody returns JSON response body.

func (*HTTPError) ResponseHeaders

func (e *HTTPError) ResponseHeaders() (status int, headers map[string]string)

ResponseHeaders returns http status code and headers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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