httpclient

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: Apache-2.0 Imports: 6 Imported by: 7

README

httpclient

  • simple library to facilitate calls to http endpoints

two methods to call

  • function with parameters
  • method with structure

two options for results

  • raw response (*http.Response)
  • response body decoded to json

Examples

see examples directory

Full Response

equivalent of

curl 'https://api.example.com' -H 'Authorization: Bearer API_TOKEN'

function
func main() {
    token := os.Getenv("API_TOKEN")
    response, err := httpclient.GetResponse("", http.MethodGet, "https://api.example.com", token)
    if err !=nil {
        log.Fatal(err)
    }
    defer response.Body.Close()
    bytes, err := io.ReadAll(response.Body)
    log.Println(response.StatusCode, string(bytes))
}
method
func main() {
    token := os.Getenv("API_TOKEN")
    endpoint := httpclient.Endpoint {
        URL: "https://api.clustercat.com",
	    Method: http.MethodGet    
	    Authorization: token,
    }
    response, err := endpoint.GetResponse()
    defer response.Body.Close()
    bytes, err := io.ReadAll(response.Body)
    log.Println(response.StatusCode, string(bytes))
}

JSON response

equivalent of

curl 'https://api.example.com/login' -H 'Authorization: Bearer API_TOKEN' - H 'Content-Type: application/json' -d '{"name":"admin","pass":"password"}'

function
func main() {
    token := os.Getenv("API_TOKEN")
    data := struct {
        Name: "admin",
        Pass: "password",
    }
    response := struct {
        Language_Pref: "EN",
        JWT: "some string",
    }
    response, err := httpclient.GetJSON(data, http.MethodPOST, "https://api.example.com/api/login", "Bearer " + token)
    if err !=nil {
        log.Fatal(err)
    }
    log.Println(response)
}
method
func main() {
    token := os.Getenv("API_TOKEN")
    data := struct {
        Name: "admin",
        Pass: "password",
    }
    response := struct {
        Language_Pref: "EN",
        JWT: "some string",
    }
    endpoint := httpclient.Endpoint {
        URL: "https://api.example.com",
	    Method: http.MethodPOST
	    Authorization: token,
        Route: "/api/login",
        Data: data,
        Response: response,
    }
    jsonEndpoint := httpclient.JSONEndpoint[struct {Language_Pref string JWT string}] {
        endpoint,
        response,
    }
    answer, err := httpclient.JSON(response)
    if err !=nil {
        log.Fatal(err)
    }
    log.Println(response)
    ```

Documentation

Index

Constants

View Source
const (
	ErrJSON    = "httpclient: json error decoding response"
	ErrJSON2   = "httpclient: json error decoding error response"
	ErrRequest = "httpclient: error creating http request"
	ErrStatus  = "httpclient: http.Status not OK"
)

Variables

View Source
var Client http.Client

Functions

func GetJSON added in v0.2.0

func GetJSON[T any, R any](data any, resp T, errResponse R, method, url, auth string, headers []Header) (T, R, error)

JSON returns JSON response from http request if response.code is http.StatusOK (200) returns resp, nil, nil if response.code is not http.Status ok returns nil, errResponse, ErrStatus if json decode err returns nil, nil, err set to 'json decode err' for any other err returns nil, nil, err

func GetResponse added in v0.2.0

func GetResponse(data any, method, url, auth string, headers []Header) (*http.Response, error)

GetResponse returns respnse from http request to url

Types

type Endpoint

type Endpoint struct {
	URL           string
	Method        string
	Route         string
	Authorization string
	Headers       []Header
	Data          any
}

Endpoint struct for an http endpoint

func (*Endpoint) GetResponse

func (e *Endpoint) GetResponse() (*http.Response, error)

GetResponse returns response from http endpoint

type ErrHttpClient added in v0.6.1

type ErrHttpClient struct {
	Status  int
	Message string
	Body    http.Response
}

ErrHttpClient error stuct

func (ErrHttpClient) Error added in v0.6.1

func (e ErrHttpClient) Error() string

ErrHttpClient.Error implementation of error interface

type Header struct {
	Name  string
	Value string
}

type JSONEndpoint added in v0.3.0

type JSONEndpoint[T any, R any] struct {
	URL           string
	Method        string
	Route         string
	Authorization string
	Headers       []Header
	Data          any
	Response      T
	ErrorResponse R
}

func (*JSONEndpoint[T, R]) GetJSON added in v0.3.0

func (e *JSONEndpoint[T, R]) GetJSON(response T, errResponse R) (T, R, error)

GetJSON returns JSON received from http endpoint

Directories

Path Synopsis
examples
functions/json command
method/json command
method/response command

Jump to

Keyboard shortcuts

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