Skip to content

enriquebris/gohttpclient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Releases

No releases published

Packages

No packages published

Languages