gohttpclient

package module
v0.0.0-...-74da2d0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2019 License: MIT Imports: 5 Imported by: 1

README

godoc reference Go Report Card Build Status

GOlang HTTP client

What is this?

gohttpclient provides a HTTP Client interface with basic functions to make HTTP requests.

Benefits of using gohttpclient.HTTPClient interface

This is all Dependency Inversion Principle (SOLID). Relying on abstractions (that would be interfaces in golang) would avoid any dependency over a particular implementation. That's why I encourage you to use gohttpclient.HTTPClient in case you need to make HTTP requests. This way you will be able to switch over different implementations as long as your project evolves.

Let's say that when you started your project the golang native http package was a good fit but after one year you decide to replace it by fasthttp or by any other golang HTTP package. Other than switch to a different gohttpclient.HTTPClient implementation, your code will not be modified at all. Even in the case that there were not an implementation for the package you think would fit better, you could write it ... and your original code would keep the same.

gohttpclient implementations
  • NativeHTTPClient - GOlang native HTTP
Installation
go get github.com/enriquebris/gohttpclient
Examples
Simple curl
package main

import (
	"fmt"

	"github.com/enriquebris/gohttpclient"
)

func main() {
	// golang native HTTP implementation
	httpClient := gohttpclient.NewNativeHTTPClient()

	body, err := simpleCURL(httpClient, "GET", "https://ifconfig.co/")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("ip: %v\n", body)
}

func simpleCURL(httpClient gohttpclient.HTTPClient, method string, url string) (string, error) {
	// reset any previous parameter
	httpClient.Reset()
	// set method (verb)
	httpClient.SetMethod(method)
	// set url
	httpClient.SetURL(url)

	// make the request
	if resp, err := httpClient.Do(); err != nil {
		return "", err
	} else {
		return resp.GetBody(), nil
	}
}
Bridge pattern

gobetafaceapi - gohttpclient is used as the Implementor.

TODO
  • GOlang native HTTP
  • GOlang native HTTP testings
  • fasthttp implementation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultHTTPResponse

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

func NewDefaultHTTPResponse

func NewDefaultHTTPResponse(body string, statusCode int, status string, headers map[string][]string) *DefaultHTTPResponse

func (*DefaultHTTPResponse) GetBody

func (st *DefaultHTTPResponse) GetBody() string

func (*DefaultHTTPResponse) GetHeaders

func (st *DefaultHTTPResponse) GetHeaders() map[string][]string

func (*DefaultHTTPResponse) GetStatus

func (st *DefaultHTTPResponse) GetStatus() string

func (*DefaultHTTPResponse) GetStatusCode

func (st *DefaultHTTPResponse) GetStatusCode() int

type DefaultResponseWriter

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

func NewDefaultResponseWriter

func NewDefaultResponseWriter(w http.ResponseWriter) *DefaultResponseWriter

func (*DefaultResponseWriter) AddHeader

func (st *DefaultResponseWriter) AddHeader(key string, value string)

func (*DefaultResponseWriter) Print

func (st *DefaultResponseWriter) Print(message string)

func (*DefaultResponseWriter) Printf

func (st *DefaultResponseWriter) Printf(format string, a ...interface{})

func (*DefaultResponseWriter) SetStatusCode

func (st *DefaultResponseWriter) SetStatusCode(statusCode int)

type HTTPClient

type HTTPClient interface {
	SetMethod(string)
	SetURL(string)
	SetPayload(string)
	AddHeader(key string, value string)
	Do() (HTTPResponse, error)
	Reset()

	NewTestServer(func(ResponseWriter, Request)) HTTPTestServer
}

func NewNativeHTTPClient

func NewNativeHTTPClient() HTTPClient

type HTTPResponse

type HTTPResponse interface {
	GetBody() string
	GetStatus() string
	GetStatusCode() int
	GetHeaders() map[string][]string
}

type HTTPTestServer

type HTTPTestServer interface {
	GetURL() string
	Close()
}

type NativeHTTPClient

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

func (*NativeHTTPClient) AddHeader

func (st *NativeHTTPClient) AddHeader(key string, value string)

func (*NativeHTTPClient) Do

func (st *NativeHTTPClient) Do() (HTTPResponse, error)

func (*NativeHTTPClient) NewTestServer

func (st *NativeHTTPClient) NewTestServer(fn func(ResponseWriter, Request)) HTTPTestServer

func (*NativeHTTPClient) Reset

func (st *NativeHTTPClient) Reset()

func (*NativeHTTPClient) SetMethod

func (st *NativeHTTPClient) SetMethod(method string)

func (*NativeHTTPClient) SetPayload

func (st *NativeHTTPClient) SetPayload(payload string)

func (*NativeHTTPClient) SetURL

func (st *NativeHTTPClient) SetURL(url string)

type NativeHTTPTestServer

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

func NewNativeHTTPTestServer

func NewNativeHTTPTestServer(testServer *httptest.Server) *NativeHTTPTestServer

func (*NativeHTTPTestServer) Close

func (st *NativeHTTPTestServer) Close()

func (*NativeHTTPTestServer) GetURL

func (st *NativeHTTPTestServer) GetURL() string

type Request

type Request interface{}

type ResponseWriter

type ResponseWriter interface {
	SetStatusCode(statusCode int)
	AddHeader(key string, value string)
	Print(message string)
	Printf(format string, a ...interface{})
}

Jump to

Keyboard shortcuts

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