netmaker/test/main.go
kayos@tcp.direct e878e4820a
Fixes+Chores: avoid de-referencing nil ptrs + lint
- Avoid referencing conditions we know are false/true

 - Avoid using name of imported package as variable

 - Avoid broken (see list item 1) if else statement in `ipservice.go` by refactoring to switch statement

 - When assigning a pointer value to a variable along with an error, check that error before referencing that pointer. Thus avoiding de-referencing a nil and causing a panic.
  *** This item is the most important ***

 - Standard gofmt package sorting + linting; This includes fixing comment starts for go doc

 - Explicit non-handling of unhandled errors where appropriate (assigning errs to _ to reduce linter screaming)

 - Export ErrExpired in `netcache` package so that we can properly reference it using `errors.Is` instead of using `strings.Contains` against an `error.Error()` value
2022-12-06 20:11:20 -08:00

75 lines
1.8 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"github.com/kr/pretty"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/functions"
)
func main() {
cfg := &config.ClientConfig{}
cfg.Network = "short"
cfg.ReadConfig()
token, err := functions.Authenticate(cfg)
if err != nil {
log.Fatal(err)
} else {
log.Println("success", token)
}
url := "https://" + cfg.Server.API + "/api/nodes/" + cfg.Network + "/" + cfg.Node.ID
response, err := api("", http.MethodGet, url, token)
if err != nil {
fmt.Println(err)
}
fmt.Println(response.StatusCode, response.Status)
if response.StatusCode != http.StatusOK {
resBytes, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
_, _ = pretty.Println(string(resBytes))
}
defer response.Body.Close()
node := models.Node{}
if err := json.NewDecoder(response.Body).Decode(&node); err != nil {
fmt.Println(err)
}
pretty.Println(node)
}
func api(data any, method, url, authorization string) (*http.Response, error) {
var request *http.Request
var err error
if data != "" {
payload, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("error encoding data %w", err)
}
request, err = http.NewRequest(method, url, bytes.NewBuffer(payload))
if err != nil {
return nil, fmt.Errorf("error creating http request %w", err)
}
request.Header.Set("Content-Type", "application/json")
} else {
request, err = http.NewRequest(method, url, nil)
if err != nil {
return nil, fmt.Errorf("error creating http request %w", err)
}
}
if authorization != "" {
request.Header.Set("authorization", "Bearer "+authorization)
}
client := http.Client{}
return client.Do(request)
}