Only apply refresh token middleware once
@@ -28,48 +28,12 @@ func init() {
// New builds a new client with defaults
func New(config config.Config) *RestClient {
- c := &RestClient{resty: resty.New(), config: config}
- c.resty.SetHeader("Accept", "application/json")
- c.resty.SetHeader("Content-Type", "application/json")
+ restyInst := resty.New()
+ client := &RestClient{resty: restyInst, config: config}
- // logging
- w := logger.Writer()
- defer w.Close()
- c.resty.SetLogger(w)
-
- return c
-}
-
-// R returns new resty.Request from configured client
-func (client *RestClient) R() *resty.Request {
- client.configure()
- request := client.resty.R()
-
- return request
-}
-
-// Reset client so that it reconfigure on next request
-func (client *RestClient) Reset() {
- client.configured = false
-}
-
-// ClearAuthToken clears the authoization token from the client config
-func (client *RestClient) ClearAuthToken() {
- client.configure()
- client.resty.SetAuthToken("")
-}
-
-func (client *RestClient) configure() {
- if client.configured {
- return
- }
-
- restyInst := client.resty
- config := client.config
-
- // Set URL & access token
- restyInst.SetHostURL(config.GetString("api-url"))
- restyInst.SetAuthToken(config.GetString("secret"))
+ // JSON
+ restyInst.SetHeader("Accept", "application/json")
+ restyInst.SetHeader("Content-Type", "application/json")
// Check that Access-Token has not expired
restyInst.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
@@ -114,5 +78,44 @@ func (client *RestClient) configure() {
return nil
})
+ // logging
+ w := logger.Writer()
+ defer w.Close()
+ restyInst.SetLogger(w)
+
+ return client
+}
+
+// R returns new resty.Request from configured client
+func (client *RestClient) R() *resty.Request {
+ client.configure()
+ request := client.resty.R()
+
+ return request
+}
+
+// Reset client so that it reconfigure on next request
+func (client *RestClient) Reset() {
+ client.configured = false
+}
+
+// ClearAuthToken clears the authoization token from the client config
+func (client *RestClient) ClearAuthToken() {
+ client.configure()
+ client.resty.SetAuthToken("")
+}
+
+func (client *RestClient) configure() {
+ if client.configured {
+ return
+ }
+
+ restyInst := client.resty
+ config := client.config
+
+ // Set URL & access token
+ restyInst.SetHostURL(config.GetString("api-url"))
+ restyInst.SetAuthToken(config.GetString("secret"))
+
client.configured = true
}
