diff --git a/.gitignore b/.gitignore index 1b256ac..b713e92 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ cover.out vendor/* -!vendor/vendor.json +.idea/ diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..12235e5 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,21 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/davecgh/go-spew" + packages = ["spew"] + revision = "346938d642f2ec3594ed81d874461961cd0faa76" + version = "v1.1.0" + +[[projects]] + name = "github.com/imdario/mergo" + packages = ["."] + revision = "163f41321a19dd09362d4c63cc2489db2015f1f4" + version = "0.3.2" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "56d6604f451ec9a4dc705a505ad2dad3e2afa81b31c4226697d9009f545c341f" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..2bde623 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,11 @@ +[[constraint]] + name = "github.com/davecgh/go-spew" + version = "1.1.0" + +[[constraint]] + name = "github.com/imdario/mergo" + version = "0.3.2" + +[[constraint]] + branch = "master" + name = "github.com/paybyphone/phpipam-sdk-go" diff --git a/phpipam/phpipam.go b/phpipam/phpipam.go index 4f6e25c..1ad59fd 100644 --- a/phpipam/phpipam.go +++ b/phpipam/phpipam.go @@ -105,7 +105,7 @@ func (bis *BoolIntString) UnmarshalJSON(b []byte) error { switch s { case "0", "": *bis = false - case "1": + case "1","4": // for the bizarre case when IPAM returns "4" as true *bis = true default: return &json.UnmarshalTypeError{ diff --git a/phpipam/request/request.go b/phpipam/request/request.go index d8f47dd..5b84db6 100644 --- a/phpipam/request/request.go +++ b/phpipam/request/request.go @@ -129,12 +129,26 @@ func newRequestResponse(r *http.Response) *requestResponse { func (r *Request) Send() error { var req *http.Request var err error - client := &http.Client{ - CheckRedirect: func(req *http.Request, via []*http.Request) error { + + r.Session.RLock() + client := r.Session.HttpClient + r.Session.RUnlock() + + if client == nil { + client = new(http.Client) + } + + if client.CheckRedirect == nil { + client.CheckRedirect = func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse - }, + } } + // Write the client back to the session object and re-use it next time + r.Session.Lock() + r.Session.HttpClient = client + r.Session.Unlock() + switch r.Method { case "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE": bs, err := json.Marshal(r.Input) diff --git a/phpipam/session/session.go b/phpipam/session/session.go index 9329683..4eceaa0 100644 --- a/phpipam/session/session.go +++ b/phpipam/session/session.go @@ -4,6 +4,8 @@ package session import ( "github.com/imdario/mergo" "github.com/paybyphone/phpipam-sdk-go/phpipam" + "net/http" + "sync" ) // timeLayout represents the datetime format returned by the PHPIPAM api. @@ -22,6 +24,12 @@ type Session struct { // The session token. Token Token + + // A session shares this client if provided + HttpClient *http.Client + + sync.RWMutex // protect updates of HttpClient + } // NewSession creates a new session based off supplied configs. It is up to the @@ -32,8 +40,17 @@ func NewSession(configs ...phpipam.Config) *Session { Config: phpipam.DefaultConfigProvider(), } for _, v := range configs { - mergo.MergeWithOverwrite(&s.Config, v) + mergo.Merge(&s.Config, v, mergo.WithOverride) } return s } + +// SetHttpClient sets a specific http client with a particular transport etc +// For backwards compatibility, a CheckRedirect function will be added at call time if not present +func (s *Session) SetHttpClient(hc *http.Client) { + s.Lock() + s.HttpClient = hc + s.Unlock() +} + diff --git a/vendor/vendor.json b/vendor/vendor.json deleted file mode 100644 index 91b13c7..0000000 --- a/vendor/vendor.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "comment": "", - "ignore": "test", - "package": [ - { - "checksumSHA1": "hwGdeQbcfc2RvIQS5wAaYRKJDd4=", - "path": "github.com/imdario/mergo", - "revision": "50d4dbd4eb0e84778abe37cefef140271d96fade", - "revisionTime": "2016-05-17T06:44:35Z" - } - ], - "rootPath": "github.com/paybyphone/phpipam-sdk-go" -}