Browse Source

Reuse http client, and export it (#301)

jonas747 8 years ago
parent
commit
edc3213ffb
3 changed files with 11 additions and 4 deletions
  1. 6 1
      discord.go
  2. 1 3
      restapi.go
  3. 4 0
      structs.go

+ 6 - 1
discord.go

@@ -13,7 +13,11 @@
 // Package discordgo provides Discord binding for Go
 package discordgo
 
-import "fmt"
+import (
+	"fmt"
+	"net/http"
+	"time"
+)
 
 // VERSION of Discordgo, follows Symantic Versioning. (http://semver.org/)
 const VERSION = "0.16.0-dev"
@@ -43,6 +47,7 @@ func New(args ...interface{}) (s *Session, err error) {
 		ShardID:                0,
 		ShardCount:             1,
 		MaxRestRetries:         3,
+		Client:                 &http.Client{Timeout: (20 * time.Second)},
 	}
 
 	// If no arguments are passed return the empty Session interface.

+ 1 - 3
restapi.go

@@ -87,9 +87,7 @@ func (s *Session) request(method, urlStr, contentType string, b []byte, bucketID
 		}
 	}
 
-	client := &http.Client{Timeout: (20 * time.Second)}
-
-	resp, err := client.Do(req)
+	resp, err := s.Client.Do(req)
 	if err != nil {
 		bucket.Release(nil)
 		return

+ 4 - 0
structs.go

@@ -13,6 +13,7 @@ package discordgo
 
 import (
 	"encoding/json"
+	"net/http"
 	"strconv"
 	"sync"
 	"time"
@@ -73,6 +74,9 @@ type Session struct {
 	// StateEnabled is true.
 	State *State
 
+	// The http client used for REST requests
+	Client *http.Client
+
 	// Event handlers
 	handlersMu   sync.RWMutex
 	handlers     map[string][]*eventHandlerInstance