http

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 16 Imported by: 0

README

go-utils Http

The go-utils Http package provides handy methods to use api end points using http protocol.

Installation

To use the log package you must import the package.

import "github.com/eliona-smart-building-assistant/go-utils/http"

Usage

After installation, you can read payload from http end points.

import "github.com/eliona-smart-building-assistant/go-utils/http"

For example, you can make a request and read current weather conditions from this endpoint.

payload, err := http.Read(http.NewRequest("https://weatherdbi.herokuapp.com/data/weather/winterthur"), 10, true)
var result map[string]interface{}
_ = json.Unmarshal(payload, &result)
fmt.Printf(result["currentConditions"].(map[string]interface{})["comment"].(string))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(request *http.Request, timeout time.Duration, checkCertificate bool) ([]byte, error)

Do return the payload returned from the request

func DoWithStatusCode added in v1.0.24

func DoWithStatusCode(request *http.Request, timeout time.Duration, checkCertificate bool) ([]byte, int, error)

DoWithStatusCode return the body and the code

func ListenApiWithOs added in v1.0.14

func ListenApiWithOs(server *http.Server)

ListenApiWithOs starts an API server and listen for API requests

func ListenWebSocket added in v1.0.19

func ListenWebSocket[T any](conn *websocket.Conn, objects chan T) error

ListenWebSocket on a web socket connection and returns typed data

func ListenWebSocketWithReconnect added in v1.0.20

func ListenWebSocketWithReconnect[T any](newWebSocket func() (*websocket.Conn, error), reconnectDelay time.Duration, objects chan T) error

func ListenWebSocketWithReconnectAlways added in v1.0.45

func ListenWebSocketWithReconnectAlways[T any](newWebSocket func() (*websocket.Conn, error), reconnectDelay time.Duration, objects chan T) error

func NewDeleteRequestWithApiKey added in v1.0.26

func NewDeleteRequestWithApiKey(url string, key string, value string) (*http.Request, error)

func NewDeleteRequestWithApiKeyAndBody added in v1.0.33

func NewDeleteRequestWithApiKeyAndBody(url string, body any, key string, value string) (*http.Request, error)

func NewDeleteRequestWithBearer added in v1.0.54

func NewDeleteRequestWithBearer(url string, token string) (*http.Request, error)

func NewPatchRequestWithApiKey added in v1.0.26

func NewPatchRequestWithApiKey(url string, key string, value string) (*http.Request, error)

func NewPostFormRequestWithBasicAuth added in v1.0.39

func NewPostFormRequestWithBasicAuth(url string, form map[string][]string, username string, password string) (*http.Request, error)

func NewPostFormRequestWithHeaders added in v1.0.39

func NewPostFormRequestWithHeaders(url string, form map[string][]string, headers map[string]string) (*http.Request, error)

func NewPostRequest

func NewPostRequest(url string, body any) (*http.Request, error)

NewPostRequest creates a new request for the given url and the body as payload. The url have to provide free access without any authentication. For authentication use other functions like NewPostRequestWithBearer.

func NewPostRequestWithApiKey added in v1.0.15

func NewPostRequestWithApiKey(url string, body any, key string, value string) (*http.Request, error)

NewPostRequestWithApiKey creates a new request for the given url. The url is authenticated with a named api key.

func NewPostRequestWithBearer

func NewPostRequestWithBearer(url string, body any, token string) (*http.Request, error)

NewPostRequestWithBearer creates a new request for the given url. The url is authenticated with a barrier token.

func NewPostRequestWithHeaders added in v1.0.37

func NewPostRequestWithHeaders(url string, body any, headers map[string]string) (*http.Request, error)

func NewPutRequest

func NewPutRequest(url string, body any) (*http.Request, error)

NewPutRequest creates a new request for the given url and the body as payload. The url have to provide free access without any authentication. For authentication use other functions like NewPutRequestWithBearer.

func NewPutRequestWithApiKey added in v1.0.15

func NewPutRequestWithApiKey(url string, body any, key string, value string) (*http.Request, error)

NewPutRequestWithApiKey creates a new request for the given url. The url is authenticated with a named api key.

func NewPutRequestWithBearer

func NewPutRequestWithBearer(url string, body any, token string) (*http.Request, error)

NewPutRequestWithBearer creates a new request for the given url. The url is authenticated with a barrier token.

func NewRequest

func NewRequest(url string) (*http.Request, error)

NewRequest creates a new request for the given url. The url have to provide free access without any authentication. For authentication use other functions like NewGetRequestWithBarrier.

func NewRequestWithApiKey added in v1.0.15

func NewRequestWithApiKey(url string, key string, value string) (*http.Request, error)

NewRequestWithApiKey creates a new request for the given url. The url is authenticated with a named api key.

func NewRequestWithBearer

func NewRequestWithBearer(url string, token string) (*http.Request, error)

NewRequestWithBearer creates a new request for the given url. The url is authenticated with a barrier token.

func NewRequestWithHeaders added in v1.0.23

func NewRequestWithHeaders(url string, headers map[string]string) (*http.Request, error)

func NewWebSocketConnectionWithApiKey added in v1.0.19

func NewWebSocketConnectionWithApiKey(url string, key string, value string) (*websocket.Conn, error)

NewWebSocketConnectionWithApiKey creates a connection to a web socket. The url is authenticated with a named api key.

func Read

func Read[T any](request *http.Request, timeout time.Duration, checkCertificate bool) (T, error)

Read returns the response data converted to a corresponding structure

func ReadWebSocket added in v1.0.19

func ReadWebSocket[T any](conn *websocket.Conn) (*T, error)

ReadWebSocket reads one message from a web socket connection and returns typed data

func ReadWithStatusCode added in v1.0.24

func ReadWithStatusCode[T any](request *http.Request, timeout time.Duration, checkCertificate bool) (T, int, error)

ReadWithStatusCode returns the response data converted to a corresponding structure

Types

type CORSEnabledHandler added in v1.0.30

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

func NewCORSEnabledHandler added in v1.0.30

func NewCORSEnabledHandler(handler http.Handler) CORSEnabledHandler

func (CORSEnabledHandler) ServeHTTP added in v1.0.30

func (h CORSEnabledHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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