Skip to content

Instantly share code, notes, and snippets.

@Deriul
Last active August 17, 2020 20:25
Show Gist options
  • Select an option

  • Save Deriul/f49a94d48030193f5451ed68de3c6ba6 to your computer and use it in GitHub Desktop.

Select an option

Save Deriul/f49a94d48030193f5451ed68de3c6ba6 to your computer and use it in GitHub Desktop.

Revisions

  1. Deriul renamed this gist Aug 17, 2020. 1 changed file with 0 additions and 0 deletions.
  2. Deriul revised this gist Aug 15, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simple_http_client_with_auth
    Original 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.88.111.28"
    var addr string = "https://10.10.10.10"

    type Client struct {
    HostURL string
  3. Deriul renamed this gist Aug 14, 2020. 1 changed file with 0 additions and 0 deletions.
  4. Deriul created this gist Aug 14, 2020.
    82 changes: 82 additions & 0 deletions simple http client with auth
    Original 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
    }