netkit

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderAuthorization = "Authorization"
	HeaderRequestID     = "X-REQUEST-ID"
)

Define common headers

View Source
const (
	VerdictMissingAuthentication = "missing_authentication"
	VerdictInvalidToken          = "invalid_token"
	VerdictInvalidParameters     = "invalid_parameters"
	VerdictSuccess               = "success"
	VerdictFailure               = "failure"
	VerdictNotFound              = "not_found"
	VerdictDuplicate             = "duplicate"
	VerdictLimitExceeded         = "limit_exceeded"
	VerdictExpiredData           = "expired_data"
	VerdictPermissionDenied      = "permission_denied"
)

Define common verdicts for common cases Each service can define its own verdicts

View Source
const (
	TokenTypeBearer = "Bearer"
)

Defines common token type

Variables

This section is empty.

Functions

func GetRemoteAddr

func GetRemoteAddr(r *http.Request) string

GetRemoteAddr gets IP of caller

func NewJSONRequest

func NewJSONRequest(ctx context.Context, method, url string, body interface{}) (*http.Request, error)

NewJSONRequest creates new request with JSON body

func NewQueryRequest

func NewQueryRequest(ctx context.Context, method, url string, queries map[string]interface{}) (*http.Request, error)

NewQueryRequest creates request with query strings

func NewTransport

func NewTransport(options ...func(*http.Transport)) http.RoundTripper

func NewUploadRequest

func NewUploadRequest(ctx context.Context, url string, reader io.Reader, modifiers ...func(*UploadOptions)) (*http.Request, error)

NewUploadRequest create a new http upload file request

func SendRequest

func SendRequest(r *http.Request) (*http.Response, error)

SendRequest sends general request to a URL and returns HTTP response

func WithIdleConnsPerHost

func WithIdleConnsPerHost(maxIdleConsPerHost int) func(*http.Transport)

Types

type InternalBody

type InternalBody[R any] struct {
	Verdict string    `json:"verdict"`
	Message string    `json:"message"`
	Time    time.Time `json:"time"`
	Data    R         `json:"data"`
}

InternalBody defines struct for internal response body This is for communicate with internal services

type Response

type Response[Body any] struct {
	StatusCode int  `json:"status_code"`
	Body       Body `json:"body"`
}

Response is generic response structure Body is data type of the response body

func Get

func Get[Body any](ctx context.Context, url string) (*Response[Body], error)

Get executes request with GET method and parses JSON response body Body is structure of response body

func ParseResponse

func ParseResponse[R any](ctx context.Context, r *http.Response) (*Response[R], error)

ParseResponse parses an http response to struct

func SendJSON

func SendJSON[Body any](ctx context.Context, method, url string, body interface{}) (*Response[Body], error)

SendJSON executes request JSON as body and parses JSON response body Body is structure of response body

type UploadOptions

type UploadOptions struct {
	Method    string            // Default: POST
	FieldName string            // Default: file
	FileName  string            // Default: UUID
	Fields    map[string]string // Default: Empty
}

UploadOptions upload options

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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