Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating mergo.Merge() to version passing override functions #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cover.out
vendor/*
!vendor/vendor.json
.idea/
21 changes: 21 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion phpipam/phpipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
20 changes: 17 additions & 3 deletions phpipam/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion phpipam/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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()
}

13 changes: 0 additions & 13 deletions vendor/vendor.json

This file was deleted.