Skip to content

Commit

Permalink
Support login session using ibapauth cookie. (#36)
Browse files Browse the repository at this point in the history
* Support login session using ibapauth cookie.

This makes multiple requests use the same login session instead of
creating one login event per request.

A Logout() method is added to the *Connector so you can invalidate the
cookie when you are done, for example using a defer statement.

* Place Logout() next to other *Connector methods.
  • Loading branch information
eest authored and johnbelamaric committed Feb 27, 2018
1 parent b812144 commit 2bf9bc4
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"encoding/json"
"errors"
"fmt"
"golang.org/x/net/publicsuffix"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -130,7 +132,13 @@ func (whr *WapiHttpRequestor) Init(cfg TransportConfig) {
ResponseHeaderTimeout: cfg.HttpRequestTimeout * time.Second,
}

whr.client = http.Client{Transport: tr}
// All users of cookiejar should import "golang.org/x/net/publicsuffix"
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
log.Fatal(err)
}

whr.client = http.Client{Jar: jar, Transport: tr}
}

func (whr *WapiHttpRequestor) SendRequest(req *http.Request) (res []byte, err error) {
Expand Down Expand Up @@ -316,6 +324,15 @@ func (c *Connector) UpdateObject(obj IBObject, ref string) (refRes string, err e
return
}

func (c *Connector) Logout() (err error) {
_, err = c.makeRequest(CREATE, nil, "logout")
if err != nil {
log.Printf("Logout request error: '%s'\n", err)
}

return
}

var ValidateConnector = validateConnector

func validateConnector(c *Connector) (err error) {
Expand Down

0 comments on commit 2bf9bc4

Please sign in to comment.