httputil

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

This package provides a syntactic sugar and helper functions to make http things easier to read or to express

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONEncode

func JSONEncode(rw http.ResponseWriter, v interface{}) error

JSONEncode encodes the given object to json format and writes it to given ResponseWriter

func JSONEncodeWithCode

func JSONEncodeWithCode(rw http.ResponseWriter, v interface{}, statusCode int) error

JSONEncodeWithCode encodes the given object to json format and writes it to given ResponseWriter with custom status code

func NewClient

func NewClient(timeoutSec time.Duration, skipCertVerification bool) *http.Client

func NewRenegotiationTLSClient

func NewRenegotiationTLSClient(timeoutSec time.Duration, skipCertVerification bool) *http.Client

func WriteErrorResponse

func WriteErrorResponse(w http.ResponseWriter, code int, err error)

func WriteResponse

func WriteResponse(w http.ResponseWriter, code int, object interface{})

Types

type ErrorDTO

type ErrorDTO struct {
	// Status is original HTTP error code
	Status int `json:"status"`

	// RequestID defines the id of the incoming request
	RequestID string `json:"requestId"`

	// Message is descriptive human-readable error
	Message string `json:"message,omitempty"`

	// Details about the error
	Details string `json:"details,omitempty"`
}

ErrorDTO represents a json returned in case of error. Only Status and RequestID fields are required.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer provides syntactic sugar for writing http responses. Works in two modes:

  • devMode: true - returns a given error in the response under `details` field
  • devMode: false - only log the given error in context of the requestID but do not return it in response

func NewResponseWriter

func NewResponseWriter(log logrus.FieldLogger, devMode bool) *Writer

NewResponseWriter returns new instance of Writer

func (*Writer) InternalServerError

func (w *Writer) InternalServerError(rw http.ResponseWriter, r *http.Request, err error, context string)

InternalServerError writes standard InternalServerError response to given ResponseWriter.

func (*Writer) NotFound

func (w *Writer) NotFound(rw http.ResponseWriter, r *http.Request, err error, context string)

NotFound writes standard NotFound response to given ResponseWriter.

Jump to

Keyboard shortcuts

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