httpclient

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 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

This section is empty.

Variables

View Source
var Client http.Client

Functions

func GetJSON added in v0.2.0

func GetJSON[T any](data any, resp T, method, url, auth string) (any, int, error)

JSON returns JSON response from http request

func GetResponse added in v0.2.0

func GetResponse(data any, method, url, auth string) (*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
	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 JSONEndpoint added in v0.3.0

type JSONEndpoint[T any] struct {
	URL           string
	Method        string
	Route         string
	Authorization string
	Data          any
	Response      T
}

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

func (e *JSONEndpoint[T]) GetJSON(response T) (any, int, 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