mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-06 13:14:24 +08:00
add a simple get request function
This commit is contained in:
parent
b2d0a6dfe9
commit
94dc0d9c8a
3 changed files with 25 additions and 63 deletions
|
@ -2,11 +2,8 @@ package functions
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gravitl/netmaker/cli/config"
|
||||
"github.com/gravitl/netmaker/models"
|
||||
)
|
||||
|
||||
|
@ -27,25 +24,7 @@ func GetExtClient(networkName, clientID string) *models.ExtClient {
|
|||
|
||||
// GetExtClientConfig - fetch a wireguard config of an external client
|
||||
func GetExtClientConfig(networkName, clientID string) string {
|
||||
_, ctx := config.GetCurrentContext()
|
||||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/extclients/%s/%s/file", ctx.Endpoint, networkName, clientID), nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if ctx.MasterKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
|
||||
} else {
|
||||
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
bodyBytes, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return string(bodyBytes)
|
||||
return get(fmt.Sprintf("/api/extclients/%s/%s/file", networkName, clientID))
|
||||
}
|
||||
|
||||
// CreateExtClient - create an external client
|
||||
|
|
|
@ -92,3 +92,25 @@ retry:
|
|||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func get(route string) string {
|
||||
_, ctx := config.GetCurrentContext()
|
||||
req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+route, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if ctx.MasterKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
|
||||
} else {
|
||||
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
bodyBytes, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return string(bodyBytes)
|
||||
}
|
||||
|
|
|
@ -1,36 +1,15 @@
|
|||
package functions
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gravitl/netmaker/cli/config"
|
||||
cfg "github.com/gravitl/netmaker/config"
|
||||
"github.com/gravitl/netmaker/models"
|
||||
)
|
||||
|
||||
// GetLogs - fetch Netmaker server logs
|
||||
func GetLogs() string {
|
||||
_, ctx := config.GetCurrentContext()
|
||||
req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+"/api/logs", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if ctx.MasterKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
|
||||
} else {
|
||||
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
bodyBytes, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return string(bodyBytes)
|
||||
return get("/api/logs")
|
||||
}
|
||||
|
||||
// GetServerInfo - fetch minimal server info
|
||||
|
@ -45,23 +24,5 @@ func GetServerConfig() *cfg.ServerConfig {
|
|||
|
||||
// GetServerHealth - fetch server current health status
|
||||
func GetServerHealth() string {
|
||||
_, ctx := config.GetCurrentContext()
|
||||
req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+"/api/server/health", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if ctx.MasterKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
|
||||
} else {
|
||||
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
bodyBytes, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return string(bodyBytes)
|
||||
return get("/api/server/health")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue