oc-lib/tools/remote_caller.go

116 lines
3.0 KiB
Go
Raw Normal View History

package tools
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
// HTTP Method Enum defines the different methods that can be used to interact with the HTTP server
2024-08-13 09:49:42 +02:00
type METHOD int
const (
GET METHOD = iota
PUT
POST
DELETE
)
// String returns the string of the enum
2024-08-23 09:53:37 +02:00
func (m METHOD) String() string {
return [...]string{"GET", "PUT", "POST", "DELETE"}[m]
}
// EnumIndex returns the index of the enum
2024-08-23 09:53:37 +02:00
func (m METHOD) EnumIndex() int {
return int(m)
}
// ToMethod returns the method from a string
2024-08-23 09:53:37 +02:00
func ToMethod(str string) METHOD {
for _, s := range []METHOD{GET, PUT, POST, DELETE} {
if s.String() == str {
return s
}
}
return GET
}
var HTTPCallerInstance = &HTTPCaller{} // Singleton instance of the HTTPCaller
type HTTPCaller struct {
2024-10-02 12:23:22 +02:00
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
Disabled bool // Disabled flag
}
// NewHTTPCaller creates a new instance of the HTTP Caller
2024-10-02 10:45:52 +02:00
func NewHTTPCaller(urls map[DataType]map[METHOD]string) *HTTPCaller {
return &HTTPCaller{
2024-10-02 12:23:22 +02:00
URLS: urls, // Set the urls defined in the config & based on the data name type & method
Disabled: false,
}
}
// CallGet calls the GET method on the HTTP server
2024-10-21 11:39:52 +02:00
func (caller *HTTPCaller) CallGet(url string, subpath string, types ...string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, url+subpath, bytes.NewBuffer([]byte("")))
if err != nil {
return nil, err
}
for _, t := range types {
req.Header.Set("Content-Type", t)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
// CallPut calls the DELETE method on the HTTP server
2024-08-13 09:49:42 +02:00
func (caller *HTTPCaller) CallDelete(url string, subpath string) ([]byte, error) {
resp, err := http.NewRequest("DELETE", url+subpath, nil)
2024-10-18 08:51:12 +02:00
if err != nil || resp == nil || resp.Body == nil {
2024-08-13 09:49:42 +02:00
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
// CallPost calls the POST method on the HTTP server
2024-10-22 09:28:18 +02:00
func (caller *HTTPCaller) CallPost(url string, subpath string, body map[string]interface{}, types ...string) ([]byte, error) {
postBody, _ := json.Marshal(body)
responseBody := bytes.NewBuffer(postBody)
2024-10-22 09:28:18 +02:00
contentType := "application/json"
if len(types) > 0 {
contentType = types[0]
}
resp, err := http.Post(url+subpath, contentType, responseBody)
2024-10-17 17:04:09 +02:00
if err != nil || resp == nil || resp.Body == nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
2024-10-16 13:50:09 +02:00
// CallPost calls the POST method on the HTTP server
func (caller *HTTPCaller) CallPut(url string, subpath string, body map[string]interface{}) ([]byte, error) {
postBody, _ := json.Marshal(body)
responseBody := bytes.NewBuffer(postBody)
req, err := http.NewRequest(http.MethodPut, url+subpath, responseBody)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}