Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cd6455
commit 8636b79
Showing
3 changed files
with
42 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |