Skip to content

Commit

Permalink
Cleanup nomad client
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 committed May 8, 2022
1 parent 6cd6455 commit 8636b79
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
4 changes: 2 additions & 2 deletions main.go
Expand Up @@ -34,9 +34,9 @@ func (e errMsg) Error() string { return e.err.Error() }
func fetchJobs(url, token string) tea.Cmd {
return func() tea.Msg {
// TODO LEO: error handling
//body, _ := nomad.Get(fmt.Sprintf("%s/v1/jobs", url), token)
//body, _ := nomad.GetJobs(url, token)
body := MockJobsResponse

type JobResponseEntry struct {
Name string
}
Expand Down
25 changes: 6 additions & 19 deletions nomad/nomad.go
@@ -1,26 +1,13 @@
package nomad

import (
"io/ioutil"
"net/http"
)

func Get(url string, token string) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Nomad-Token", token)

resp, err := client.Do(req)
if err != nil {
return nil, err
}
var ApiPaths = map[string]string{
"jobs": "/v1/jobs",
}

body, err := ioutil.ReadAll(resp.Body)
func GetJobs(url, token string) ([]byte, error) {
path, err := urlWithPathFor(url, "jobs")
if err != nil {
return nil, err
}
return body, nil
return get(path, token)
}
34 changes: 34 additions & 0 deletions nomad/util.go
@@ -0,0 +1,34 @@
package nomad

import (
"fmt"
"io/ioutil"
"net/http"
)

func get(url, token string) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Nomad-Token", token)

resp, err := client.Do(req)
if err != nil {
return nil, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}

func urlWithPathFor(url, key string) (string, error) {
if val, exists := ApiPaths[key]; exists {
return fmt.Sprintf("%s/%s", url, val), nil
}
return "", fmt.Errorf("key '%s' has no associated path", key)
}

0 comments on commit 8636b79

Please sign in to comment.