client

package
v0.0.0-...-c1d1bf9 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewByteBody

func NewByteBody(b []byte) io.ReadCloser

NewByteBody creates an io.ReadCloser from a slice of bytes.

func NewMock

func NewMock(r ...MockResponse) *http.Client

NewMock returns a pointer to http.Client with the mocked Transport.

func NewStringBody

func NewStringBody(b string) io.ReadCloser

NewStringBody creates an io.ReadCloser from a string.

func NewStructBody

func NewStructBody(i interface{}) io.ReadCloser

NewStructBody creates an io.ReadCloser from a structure that is attempted to be encoded into JSON. In case of failure, it panics.

Types

type Config

type Config struct {
	HostPort *hostPort
	User     string
	Pass     string
	Timeout  time.Duration
	// contains filtered or unexported fields
}

Config contains the config http.Client that will be used for the http.Client

func NewClientConfig

func NewClientConfig(host string, port int, user string, pass string, timeout int, insecure bool) (*Config, error)

NewClientConfig handles the parameters that will be used in the HTTP Client If a socket is passed as a URL (http://<host>:<port>), the complex URL will prevail from the passed port

func (*Config) HTTPAdress

func (c *Config) HTTPAdress() string

HTTPAdress returns the host and port combination so it can be used by the Client http://host:port

func (*Config) SetHeader

func (c *Config) SetHeader(key string, value string)

SetHeader that will be sent with the request

func (*Config) SetHost

func (c *Config) SetHost(value string) error

SetHost modifies the target host

type HTTP

type HTTP struct {
	Config *Config
	// contains filtered or unexported fields
}

HTTP Wraps an http.Client with its config

func NewHTTP

func NewHTTP(config *Config, client *http.Client) *HTTP

NewHTTP is the factory function for HTTP

func (*HTTP) HandleCall

func (c *HTTP) HandleCall(method, url, body string) (*http.Response, error)

HandleCall is responsible to perform HTTP requests against the secified url it relies on the underlying net/http.Client or Injected CallerInterface.

Because we have to inject the `Content-Type: application/json`, client.Do is used.

func (*HTTP) SetHost

func (c *HTTP) SetHost(value string) error

SetHost modifies the target host

type MockResponse

type MockResponse struct {
	Response http.Response
	Error    error
}

MockResponse Wraps the response of the RoundTrip.

type RoundTripper

type RoundTripper struct {
	Responses []MockResponse
	// contains filtered or unexported fields
}

RoundTripper is aimed to be used as the Transport property in an http.Client in order to mock the responses that it would return in the normal execution. If the number of responses that are mocked are not enough, an error with the request iteration ID, method and full URL is returned.

func (*RoundTripper) Add

func (rt *RoundTripper) Add(res ...MockResponse) *RoundTripper

Add accepts multiple Response structures as variadric arguments and appends those to the current list of Responses.

func (*RoundTripper) RoundTrip

func (rt *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes a single HTTP transaction, returning a Response for the provided Request.

RoundTrip should not attempt to interpret the response. In particular, RoundTrip must return err == nil if it obtained a response, regardless of the response's HTTP status code. A non-nil err should be reserved for failure to obtain a response. Similarly, RoundTrip should not attempt to handle higher-level protocol details such as redirects, authentication, or cookies.

RoundTrip should not modify the request, except for consuming and closing the Request's Body. RoundTrip may read fields of the request in a separate goroutine. Callers should not mutate or reuse the request until the Response's Body has been closed.

RoundTrip must always close the body, including on errors, but depending on the implementation may do so in a separate goroutine even after RoundTrip returns. This means that callers wanting to reuse the body for subsequent requests must arrange to wait for the Close call before doing so.

The Request's URL and Header fields must be initialized.

Jump to

Keyboard shortcuts

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