request

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 10 Imported by: 0

README

go-http-wrapper

Easy use go http request

Required

Install

go get github.com/9bany/go-http-wrapper 

How to use

1. Create an target
type exampleTarget struct {}

func (target *exampleTarget) GetMethod() string {
	return "POST" // http.PostMethod
}

func (target *exampleTarget) GetEndpoint() string {
	return "localhost:3000"
}

func (target *exampleTarget) GetBody() []byte {
	return nil
}

func (target *exampleTarget) GetHeader() Header {
	return map[string]string{}
}
Use target with request wrapper
import wrapper "github.com/9bany/go-http-wrapper"
// Data structure mapping from json response
type Data struct {
    Msg string `json:"data"`
}

target := &exampleTarget{}
requestor := wrapper.NewRequest[Data](target)

resp, err := requestor.Execute(context.Background())
fmt.Println(resp)
fmt.Println(err)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Header map[string]string

type RequestProtocol

type RequestProtocol[DataResponseType any] interface {
	Execute(ctx context.Context) (*Response[DataResponseType], error)
}

func NewRequest

func NewRequest[DataResponseType any](target Target) RequestProtocol[DataResponseType]

type Response

type Response[T any] struct {
	Status     string
	StatusCode int
	Data       T
}

type Target

type Target interface {
	GetMethod() string
	GetEndpoint() string
	GetBody() []byte
	GetHeader() Header
}

Jump to

Keyboard shortcuts

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