Last active
August 17, 2020 20:25
-
-
Save Deriul/f49a94d48030193f5451ed68de3c6ba6 to your computer and use it in GitHub Desktop.
Revisions
-
Deriul renamed this gist
Aug 17, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Deriul revised this gist
Aug 15, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import ( var uname string = "admin" var pwd string = "adm!nd42" var addr string = "https://10.10.10.10" type Client struct { HostURL string -
Deriul renamed this gist
Aug 14, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Deriul created this gist
Aug 14, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,82 @@ package main import ( "crypto/tls" "encoding/base64" "fmt" "io/ioutil" "net/http" "time" ) var uname string = "admin" var pwd string = "adm!nd42" var addr string = "https://10.88.111.28" type Client struct { HostURL string HTTPClient *http.Client Token string } func main() { c, _ := NewClient(&addr, &uname, &pwd) subnet, _ := c.GetSubnet("1") fmt.Println(string([]byte(subnet))) } func NewClient(host, username, password *string) (*Client, error) { c := Client{ HTTPClient: &http.Client{Timeout: 10 * time.Second}, HostURL: addr, } if host != nil { c.HostURL = *host } if (username != nil) && (password != nil) { c.Token = "Basic " + base64.StdEncoding.EncodeToString([]byte(*username+":"+*password)) } return &c, nil } func (c *Client) GetSubnet(ID string) ([]byte, error) { req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/1.0/subnets/%s/", c.HostURL, ID), nil) if err != nil { return nil, err } body, err := c.doRequest(req) // wrap HTTPClient.Do and adds auth, returns []byte with body if err != nil { return nil, err } return body, nil } func (c *Client) doRequest(req *http.Request) ([]byte, error) { //returns []byte with body inside req.Header.Set("Authorization", c.Token) http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} res, err := c.HTTPClient.Do(req) if err != nil { return nil, err } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err } if res.StatusCode != http.StatusOK { return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body) } return body, err }