Working with packages and allowed IPs.

giredorectl now able to interact with giredored about:

* Setting package data. There is no such thing as "create" or "update",
just set.

* Deleting package data.

* Setting allowed IP addresses. This is the only authorization method
ATM, more may come in future.
This commit is contained in:
2019-10-07 18:21:26 +05:00
parent 83a8694061
commit 6ce7747dd5
24 changed files with 725 additions and 47 deletions

View File

@@ -2,6 +2,8 @@ package requester
import (
// stdlib
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
@@ -21,14 +23,22 @@ func Initialize() {
log.Info().Msg("Initializing...")
}
func execRequest(method string, url string, data map[string]string) ([]byte, error) {
func Delete(url string, data interface{}) ([]byte, error) {
return execRequest("DELETE", url, data)
}
func execRequest(method string, url string, data interface{}) ([]byte, error) {
log.Debug().Str("method", method).Str("URL", url).Msg("Trying to execute HTTP request...")
httpClient := getHTTPClient()
var dataToSend []byte
if data != nil {
dataToSend, _ = json.Marshal(data)
}
// Compose HTTP request.
// ToDo: POST/PUT/other methods that require body.
httpReq, err := http.NewRequest(method, url, nil)
httpReq, err := http.NewRequest(method, url, bytes.NewReader(dataToSend))
if err != nil {
return nil, err
}
@@ -52,3 +62,11 @@ func execRequest(method string, url string, data map[string]string) ([]byte, err
func Get(url string) ([]byte, error) {
return execRequest("GET", url, nil)
}
func Post(url string, data interface{}) ([]byte, error) {
return execRequest("POST", url, data)
}
func Put(url string, data interface{}) ([]byte, error) {
return execRequest("PUT", url, data)
}