http_goclient

package module
v0.0.0-...-7112df6 Latest Latest
Warning

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

Go to latest
Published: May 25, 2019 License: Apache-2.0 Imports: 6 Imported by: 5

README

HTTP Go Client

Http Go Client represents an HTTP wrapper that reduces the boiler plate needed to marshall/un-marshall request/response bodies by providing friendly CRUD operations matching their corresponding HTTP verbs that allow in/out interfaces.

The client supports CRUD operations in the form of GET, POST, PUT and DELETE. The client also supports convenient json operations like PutJson/PostJson so the user does not need to set up the content type of the payload.

Additionally, the client provides a handy testing package that allows for the creation of a fake http client and server. The server side can be configured with a request matcher that verifies whether the incoming request matches the expected one and if so replies with a configured (payload and HTTP code response) HTTP response.

How to use the client?

Http Go Client is really easy to use, it is first configured with the actual underlying http.client which is the one that eventually performs the http calls.

Here is an example on how you to build the struct:

import (
	"github.com/dikhan/http_goclient"
)

func main() {
    httpClient := &HttpClient{&http.Client{}}	
}

Once the client is created, the different CRUD operations available can be invoked:

    res, err := c.httpClient.Get(url, c.requestHeaders(), in)
    res, err := c.httpClient.PostJson(url, c.requestHeaders(), in, out)
    res, err := c.httpClient.PutJson(url, c.requestHeaders(), in, out)
    res, err := c.httpClient.Delete(url, c.requestHeaders())

Please note that this client does not inspect the response at all. The user of the library should take care of the different http response codes returned by using the *http.Response object returned by the CRUD operations. The following logentries_goclient can serve as an example on how to use the library.

How to use the client's testing package?

The following snippet of code shows how to create a new RequestMatcher, initialize the clientServer struct and subsequently how to instantiate the TestClientServer().

import (
	"github.com/dikhan/http_goclient/testutils"
)

type TestStruct struct {
	Name string `json:"name"`
	Username string `json:"username"`
}

func TestLogSets_GetLogSets(t *testing.T) {
    expectedPayload := &TestStruct{
        Name: "Dani",
        Username: "dikhan",
    }

    requestMatcher := testutils.NewRequestMatcher(http.MethodGet, "/api/resource", nil, http.StatusOK, expectedPayload)
    
	testClientServer := testutils.TestClientServer {
		RequestMatcher: requestMatcher,
	}
	httpClient, httpServer := testClientServer.TestClientServer()
    ...
    // init the struct which depends on *http.Client
}

The client will end up sending the HTTP request to the 'fake' server which will perform some verifications on the incoming request (as configured previously), and reply with the expected response.

The following tests from the logentries_goclient can be used as reference to better understand how to use the testing package.

Contributing

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request :D

Authors

Daniel I. Khan Ramiro

See also the list of contributors who participated in this project.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpClient

type HttpClient struct {
	HttpClient *http.Client
}

HttpClient represents an http wrapper which reduces the boiler plate needed to marshall/un-marshall request/response bodies by providing friendly CRUD http operations that allow in/out interfaces

func (*HttpClient) Delete

func (httpClient *HttpClient) Delete(url string, headers map[string]string) (*http.Response, error)

Delete issues a DELETE HTTP request to the specified URL including the headers passed in.

func (*HttpClient) Get

func (httpClient *HttpClient) Get(url string, headers map[string]string, out interface{}) (*http.Response, error)

Get issues a GET HTTP request to the specified URL including the headers passed in.

The 'out' param interface is the un-marshall representation of the http response returned

Example on how to invoke the method:

	type Out struct {
		Id string `json:"id"`
		Name        string   `json:"name"`
		Description string   `json:"description"`
	}

 out := &Out{}
 headers := map[string]string{"header_example": "header_value"}
 HttpClient.Get("http://api.com/resource", headers, out)

func (*HttpClient) Post

func (httpClient *HttpClient) Post(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)

Post issues a POST HTTP request to the specified URL including the headers passed in.

The in interface is marshall and added to the htp request body. The out interface is the un-marshall representation of the http response returned

Example on how to invoke the method:

	type In struct {
		Name        string   `json:"name"`
		Description string   `json:"description"`
	}

	type Out struct {
		Id string `json:"id"`
		Name        string   `json:"name"`
		Description string   `json:"description"`
	}
 in := &In{}
 out := &Out{}
 headers := map[string]string{"header_example": "header_value"}
 HttpClient.Post("http://api.com/resource", headers, in, out)

func (*HttpClient) PostJson

func (httpClient *HttpClient) PostJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)

PostJson issues a POST to the specified URL including the headers passed in. The content type of the body is set to application/json so it doesn't need to be added to the headers passed in

The 'in' param interface is marshall and added to the htp request body. The 'out' param interface is the un-marshall representation of the http response returned

Example on how to invoke the method:

	type In struct {
		Name        string   `json:"name"`
		Description string   `json:"description"`
	}

	type Out struct {
		Id string `json:"id"`
		Name        string   `json:"name"`
		Description string   `json:"description"`
	}
 in := &In{}
 out := &Out{}
 headers := map[string]string{"header_example": "header_value"}
 HttpClient.PostJson("http://api.com/resource", headers, in, out)

func (*HttpClient) Put

func (httpClient *HttpClient) Put(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)

Post issues a PUT HTTP request to the specified URL including the headers passed in.

The in interface is marshall and added to the http request body. The out interface is the un-marshall representation of the http response returned

Example on how to invoke the method:

	type In struct {
		Name        string   `json:"name"`
		Description string   `json:"description"`
	}

	type Out struct {
		Id string `json:"id"`
		Name        string   `json:"name"`
		Description string   `json:"description"`
	}
 in := &In{}
 out := &Out{}
 headers := map[string]string{"header_example": "header_value"}
 HttpClient.Put("http://api.com/resource", headers, in, out)

func (*HttpClient) PutJson

func (httpClient *HttpClient) PutJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)

PutJson issues a PUT HTTP request to the specified URL including the headers passed in. The content type of the body is set to application/json

The 'in' param interface is marshall and added to the htp request body. The 'out' param interface is the un-marshall representation of the http response returned

Example on how to invoke the method:

	type In struct {
		Name        string   `json:"name"`
		Description string   `json:"description"`
	}

	type Out struct {
		Id string `json:"id"`
		Name        string   `json:"name"`
		Description string   `json:"description"`
	}
 in := &In{}
 out := &Out{}
 headers := map[string]string{"header_example": "header_value"}
 HttpClient.PutJson("http://api.com/resource", headers, in, out)

type HttpClientIface

type HttpClientIface interface {
	Get(url string, headers map[string]string, out interface{}) (*http.Response, error)
	PostJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)
	Post(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)
	PutJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)
	Put(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)
	Delete(url string, headers map[string]string) (*http.Response, error)
}

type HttpClientStub

type HttpClientStub struct {
	// Properties that tests can run assertions against to
	URL     string
	Headers map[string]string
	In      interface{}
	Out     interface{}
	// Stub response
	Response *http.Response
	// Stub error
	Error error
}

HttpClientStub implements the HttpClientIface and should be used for unit testing purposes

func (*HttpClientStub) Delete

func (c *HttpClientStub) Delete(url string, headers map[string]string) (*http.Response, error)

func (*HttpClientStub) Get

func (c *HttpClientStub) Get(url string, headers map[string]string, out interface{}) (*http.Response, error)

func (*HttpClientStub) Post

func (c *HttpClientStub) Post(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)

func (*HttpClientStub) PostJson

func (c *HttpClientStub) PostJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)

func (*HttpClientStub) Put

func (c *HttpClientStub) Put(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)

func (*HttpClientStub) PutJson

func (c *HttpClientStub) PutJson(url string, headers map[string]string, in interface{}, out interface{}) (*http.Response, error)

Directories

Path Synopsis
Package testutils provides utilities for testing purposes
Package testutils provides utilities for testing purposes

Jump to

Keyboard shortcuts

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