request

package
v0.0.0-...-d22e7c3 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2020 License: GPL-3.0 Imports: 5 Imported by: 0

README

HTTP Request Builder

This package is an HTTP request builder for go

Get Request

import "github.com/albertwidi/internal/pkg/http/request"

func main() {
    // req  is a *http.Request 
    req, err := request.New(context.Background()).
        Get("https://google.com").
        Compile()
    if err != nil {
        // do something with error
    }
Get Request With Query
import "github.com/albertwidi/internal/pkg/http/request"

func main() {
    // req  is a *http.Request 
    req, err := request.New(context.Background()).
        Get("https://google.com").
        Query("key", "value").
        Compile()
    if err != nil {
        // do something with error
    }

Query receive variadic parameters with type string

Post Request

import "github.com/albertwidi/internal/pkg/http/request"

func main() {
    // req  is a *http.Request 
    req, err := request.New(context.Background()).
        Post("https://google.com").
        Compile()
    if err != nil {
        // do something with error
    }
Post Form
import "github.com/albertwidi/internal/pkg/http/request"

func main() {
    // req  is a *http.Request 
    req, err := request.New(context.Background()).
        Post("https://google.com").
        PostForm("key", "value")
        Compile()
    if err != nil {
        // do something with error
    }

PostForm receive variadic parameters with type string

Request Body

JSON
import "github.com/albertwidi/internal/pkg/http/request"

func main() {
    s := struct {
        Asd string `json:"asd"`
        Jkl string `json:"jkl"`
    }{}

    // req  is a *http.Request 
    req, err := request.New(context.Background()).
        Post("https://google.com").
        BodyJSON(s).
        Compile()
    if err != nil {
        // do something with error
    }
Raw/io.Reader
func main() {
    s := struct {
        Asd string `json:"asd"`
        Jkl string `json:"jkl"`
    }{}

    // req  is a *http.Request 
    req, err := request.New(context.Background()).
        Post("https://google.com").
        BodyJSON(s).
        Compile()
    if err != nil {
        // do something with error
    }

Version Selection Header

To be added

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// RoutingContext is a key name for version selection routing
	RoutingContext = "REQUEST_ROUTING_HEADER"
)

Functions

func Header(kv ...string) *hTTPHeader

Header function

Types

type Request

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

Request wrap the http request

func New

func New(ctx context.Context) *Request

New http request wrapper

func (*Request) Body

func (r *Request) Body(body io.Reader) *Request

Body of the request

func (*Request) BodyJSON

func (r *Request) BodyJSON(body interface{}) *Request

BodyJSON indicate that request body is a json data

func (*Request) Compile

func (r *Request) Compile() (*http.Request, error)

Compile the http request version selection header spesification:

  1. routes-version-select propagated header for selecting version in matching url example: svc1.cluster.local|0.1.0, svc2.cluster.local|0.2.0
  2. route-version-select propagated header for selecting version in matching url different from routes-version-select, matching url will not propagated example: svc1.cluster.local|0.1.0
  3. version-select propageted header when an url is match, only contain version value example: 0.2.0

func (*Request) Get

func (r *Request) Get(url string) *Request

Get function for building get request

func (*Request) Headers

func (r *Request) Headers(header http.Header) *Request

Headers function to set request header

func (*Request) Method

func (r *Request) Method(method string) *Request

Method function set the request method

func (*Request) NoVersionHeader

func (r *Request) NoVersionHeader() *Request

NoVersionHeader function to control the version selection header generation

func (*Request) Post

func (r *Request) Post(url string) *Request

Post function for building post request

func (*Request) PostForm

func (r *Request) PostForm(kv ...string) *Request

PostForm set a url values for a postform body in a request

func (*Request) Put

func (r *Request) Put(url string) *Request

Put function for building put request

func (*Request) Query

func (r *Request) Query(kv ...string) *Request

Query for creating url query

func (*Request) URL

func (r *Request) URL(url string) *Request

URL function set the request url

Jump to

Keyboard shortcuts

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