http

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ClientNetHTTP = iota + 1
	ClientFastHTTP
)

client type

View Source
const (
	NoData      requestDataFormat = 0
	QueryString requestDataFormat = 1
	Form        requestDataFormat = 2
	JSON        requestDataFormat = 3
	Raw         requestDataFormat = 4
)

request data formats

Variables

This section is empty.

Functions

func GetJSON

func GetJSON(respData interface{}, url string, params interface{}, headers ...*DataMap) error

GetJSON send GET request with data in JSON format, using `DefaultClient`

func PostForm

func PostForm(respData interface{}, url string, form interface{}, headers ...*DataMap) error

PostForm send POST request with data in Form format, using `DefaultClient`

func PostJSON

func PostJSON(respData interface{}, url string, data interface{}, headers ...*DataMap) error

PostJSON send POST request with data in JSON format, using `DefaultClient`

func RequestJSON

func RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, data interface{}, headers ...*DataMap) error

RequestJSON send http request with JSON response, support both GET and POST, different body format and headers, using `DefaultClient`

func SetClient

func SetClient(client Client)

SetClient sets http client

Types

type Client

type Client interface {
	// RequestJSON supports different request format with JSON Response
	RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, data interface{}, headers ...*DataMap) (err error)

	// GetRawClient returns the internal http client
	GetRawClient() interface{}
}

Client is the http client standard interface.

func GetClient

func GetClient(opts ...Option) Client

GetClient creates a `Client` based on `opts` and returns to caller

func NewDefaultClient

func NewDefaultClient() Client

NewDefaultClient initializes a new DefaultClient

func NewDefaultHTTPMockClient

func NewDefaultHTTPMockClient(getFileFunc GetMockFileNameFunc) Client

NewDefaultHTTPMockClient creates a http client for testing

type DataMap

type DataMap map[string]interface{}

DataMap is a map of key to any data

type DefaultClient

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

DefaultClient is the client to handle the various HTTP request, with net/http.Client as internal client

func (*DefaultClient) GetRawClient

func (c *DefaultClient) GetRawClient() interface{}

GetRawClient gets the internal client

func (*DefaultClient) RequestJSON

func (c *DefaultClient) RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, data interface{}, headers ...*DataMap) (err error)

RequestJSON Send http request to query JSON data `data` can be `*url.Values`, `*DataMap`, or struct, for any requestDataFormat

Note: 1. if pass `url.Values` or `DataMap` as data, should pass the pointer 2. For struct data, can specify the field name by `form` tag. E.g. struct { Name string `form:"name"` }

Examples:

  1. Response struct

    ``` type ApiResponse struct { Error string `json:"error"` } resp := ApiResponse{} ```

  2. Pass *url.Values as parameters

    ``` v := url.Values{} v.Set("name", "abc") v.Add("values", "1") v.Add("values", "2") http.HTTPRequestJSON(resp, "GET", "http://localhost/api", http.QueryString, &v) ```

The request will be:

	```
	GET /api?name=abc&values=1&values=2 HTTP/1.1
	Host: localhost
	```

 2. Pass DataMap as parameters

    ```
    http.HTTPRequestJSON(resp, "POST", "http://localhost/api", http.Form,
    &http.DataMap{
    	"name": "abc",
    	"values": []int{1, 2},
    })
    ```

The request will be:

	```
	POST /api HTTP/1.1
	Host: localhost
	Content-Type: application/x-www-form-urlencoded

	name=abc&values=1&values=2
	```

 3. Pass Struct as parameters

    ```
    type ApiRequest struct {
    	Name string `json:"name" form:"name"`
    	Values []int `json:"values" form:"values"`
    }
    http.HTTPRequestJSON(resp, "POST", "http://localhost/api", http.JSON,
    &ApiRequest{
    	Name: "abc",
    	Values: []int{1, 2},
    })
    ```

The request will be:

```
POST /api HTTP/1.1
Host: localhost
Content-Type: application/json

{"name":"abc","values":[1,2]}
```

type DefaultMockClient

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

DefaultMockClient read json content from mock file and unmarshal it to response.

func (DefaultMockClient) GetRawClient

func (hcm DefaultMockClient) GetRawClient() interface{}

GetRawClient gets the internal client

func (DefaultMockClient) RequestJSON

func (hcm DefaultMockClient) RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, data interface{}, headers ...*DataMap) (err error)

RequestJSON mocks RequestJSON in http but returns mock data

type FastHTTPClient

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

FastHTTPClient is the client to handle the various HTTP request, with net/fasthttp.Client as internal client

func (*FastHTTPClient) GetRawClient

func (c *FastHTTPClient) GetRawClient() interface{}

GetRawClient gets the internal client

func (*FastHTTPClient) RequestJSON

func (c *FastHTTPClient) RequestJSON(respData interface{}, method Method, url string, format requestDataFormat, data interface{}, headers ...*DataMap) (err error)

RequestJSON Send http request to query JSON data

type GetMockFileNameFunc

type GetMockFileNameFunc func(url string, method Method, data interface{}) string

GetMockFileNameFunc returns a file name, of which contains mock data for test

type Method

type Method string

Method represents http method in string

const (
	GET  Method = "GET"
	POST Method = "POST"
)

http methods

type Option

type Option func(*Options)

Option is modifier to update Options

func RawClientType

func RawClientType(clientType int) Option

RawClientType sets `Options.ClientType`

func Timeout

func Timeout(timeout time.Duration) Option

Timeout sets `Options.Timeout`

type Options

type Options struct {
	Timeout    time.Duration
	ClientType int
}

Options are options to create a client

Jump to

Keyboard shortcuts

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