Documentation ¶
Index ¶
- type Client
- func (c *Client) AddPlugin(p heimdall.Plugin)
- func (c *Client) Delete(url string, headers http.Header) (*http.Response, error)
- func (c *Client) Do(request *http.Request) (*http.Response, error)
- func (c *Client) Get(url string, headers http.Header) (*http.Response, error)
- func (c *Client) Patch(url string, body io.Reader, headers http.Header) (*http.Response, error)
- func (c *Client) Post(url string, body io.Reader, headers http.Header) (*http.Response, error)
- func (c *Client) Put(url string, body io.Reader, headers http.Header) (*http.Response, error)
- type MockPlugin
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the http client implementation
func (*Client) AddPlugin ¶
func (c *Client) AddPlugin(p heimdall.Plugin)
AddPlugin Adds plugin to client
type MockPlugin ¶
MockPlugin provides a mock plugin for heimdall
func (*MockPlugin) OnError ¶
func (m *MockPlugin) OnError(req *http.Request, err error)
OnError is called when the request errors out
func (*MockPlugin) OnRequestEnd ¶
func (m *MockPlugin) OnRequestEnd(req *http.Request, res *http.Response)
OnRequestEnd is called when the request ends
func (*MockPlugin) OnRequestStart ¶
func (m *MockPlugin) OnRequestStart(req *http.Request)
OnRequestStart is called when the request starts
type Option ¶
type Option func(*Client)
Option represents the client options
func WithHTTPClient ¶
func WithHTTPClient(client heimdall.Doer) Option
WithHTTPClient sets a custom http client
Example ¶
m := &mockClient{} c := NewClient(WithHTTPClient(m)) req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil) if err != nil { panic(err) } _, _ = c.Do(req)
Output: mock client called
func WithHTTPTimeout ¶
WithHTTPTimeout sets hystrix timeout
Example ¶
c := NewClient(WithHTTPTimeout(5 * time.Second)) req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil) if err != nil { panic(err) } res, err := c.Do(req) if err != nil { panic(err) } fmt.Println("Response status : ", res.StatusCode)
Output: Response status : 200
Example (Expired) ¶
c := NewClient(WithHTTPTimeout(1 * time.Millisecond)) req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil) if err != nil { panic(err) } res, err := c.Do(req) if err != nil { fmt.Println("error:", err) return } fmt.Println("Response status : ", res.StatusCode)
Output:
func WithRetrier ¶
func WithRetrier(retrier heimdall.Retriable) Option
WithRetrier sets the strategy for retrying
Example ¶
c := NewClient(WithHTTPTimeout(1*time.Millisecond), WithRetryCount(3), WithRetrier(&mockRetrier{})) req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil) if err != nil { panic(err) } res, err := c.Do(req) if err != nil { fmt.Println("error") return } fmt.Println("Response status : ", res.StatusCode)
Output: retry attempt 0 retry attempt 1 retry attempt 2 retry attempt 3 error
func WithRetryCount ¶
WithRetryCount sets the retry count for the hystrixHTTPClient
Example ¶
c := NewClient(WithHTTPTimeout(1*time.Millisecond), WithRetryCount(3)) req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil) if err != nil { panic(err) } res, err := c.Do(req) if err != nil { fmt.Println("error:", err) return } fmt.Println("Response status : ", res.StatusCode)
Output:
Click to show internal directories.
Click to hide internal directories.