@@ -2,13 +2,15 @@ package rest
22
33import (
44 "bytes"
5+ "context"
56 "encoding/json"
67 "fmt"
78 "io"
89 "io/ioutil"
910 "net/http"
1011 "net/url"
1112 "strings"
13+ "time"
1214
1315 "github.com/pkg/errors"
1416
@@ -38,22 +40,34 @@ type ClientOption func(*RESTClient)
3840
3941func NewRESTClient (baseUrl string , opts ... ClientOption ) (* RESTClient , error ) {
4042 client := & RESTClient {
41- endpoint : baseUrl ,
43+ endpoint : complementURL ( baseUrl ) ,
4244 httpClient : & http.Client {},
4345 }
46+
4447 for _ , option := range opts {
4548 option (client )
4649 }
50+
4751 if client .token == "" && client .session == nil && client .credentials != nil {
48- var err error
49- client . session , err = client . PostTicket ( * client . credentials )
50- if err != nil {
52+ ctx , cancel := context . WithTimeout ( context . TODO (), 1 * time . Minute )
53+ defer cancel ( )
54+ if err := client . makeNewSession ( ctx ); err != nil {
5155 return nil , err
5256 }
5357 }
5458 return client , nil
5559}
5660
61+ func complementURL (url string ) string {
62+ if ! strings .HasPrefix (url , "http" ) {
63+ url = "http://" + url
64+ }
65+ if ! strings .HasSuffix (url , "/api2/json" ) {
66+ url += "/api2/json"
67+ }
68+ return url
69+ }
70+
5771func WithClient (client * http.Client ) ClientOption {
5872 return func (c * RESTClient ) {
5973 c .httpClient = client
@@ -78,18 +92,13 @@ func WithUserPassword(username, password string) ClientOption {
7892 }
7993}
8094
81- func withLogin ( ) ClientOption {
95+ func WithAPIToken ( tokenid , secret string ) ClientOption {
8296 return func (c * RESTClient ) {
97+ c .token = fmt .Sprintf ("%s=%s" , tokenid , secret )
8398 }
8499}
85100
86- func (c * RESTClient ) Do (httpMethod , urlPath string , req , v interface {}) error {
87- if ! strings .HasPrefix (c .endpoint , "http" ) {
88- c .endpoint = "http://" + c .endpoint
89- }
90- if ! strings .HasSuffix (c .endpoint , "/api2/json" ) {
91- c .endpoint += "/api2/json"
92- }
101+ func (c * RESTClient ) Do (ctx context.Context , httpMethod , urlPath string , req , v interface {}) error {
93102 url , err := url .JoinPath (c .endpoint , urlPath )
94103 if err != nil {
95104 return err
@@ -104,7 +113,7 @@ func (c *RESTClient) Do(httpMethod, urlPath string, req, v interface{}) error {
104113 body = bytes .NewReader (jsonReq )
105114 }
106115
107- httpReq , err := http .NewRequest ( httpMethod , url , body )
116+ httpReq , err := http .NewRequestWithContext ( ctx , httpMethod , url , body )
108117 if err != nil {
109118 return err
110119 }
@@ -138,20 +147,20 @@ func (c *RESTClient) Do(httpMethod, urlPath string, req, v interface{}) error {
138147 return json .Unmarshal (buf , & v )
139148}
140149
141- func (c * RESTClient ) Get (path string , res interface {}) error {
142- return c .Do (http .MethodGet , path , nil , res )
150+ func (c * RESTClient ) Get (ctx context. Context , path string , res interface {}) error {
151+ return c .Do (ctx , http .MethodGet , path , nil , res )
143152}
144153
145- func (c * RESTClient ) Post (path string , req , res interface {}) error {
146- return c .Do (http .MethodPost , path , req , res )
154+ func (c * RESTClient ) Post (ctx context. Context , path string , req , res interface {}) error {
155+ return c .Do (ctx , http .MethodPost , path , req , res )
147156}
148157
149- func (c * RESTClient ) Put (path string , req , res interface {}) error {
150- return c .Do (http .MethodPut , path , req , res )
158+ func (c * RESTClient ) Put (ctx context. Context , path string , req , res interface {}) error {
159+ return c .Do (ctx , http .MethodPut , path , req , res )
151160}
152161
153- func (c * RESTClient ) Delete (path string , req , res interface {}) error {
154- return c .Do (http .MethodDelete , path , req , res )
162+ func (c * RESTClient ) Delete (ctx context. Context , path string , req , res interface {}) error {
163+ return c .Do (ctx , http .MethodDelete , path , req , res )
155164}
156165
157166func (c * RESTClient ) makeAuthHeaders () http.Header {
@@ -167,9 +176,9 @@ func (c *RESTClient) makeAuthHeaders() http.Header {
167176 return header
168177}
169178
170- func (c * RESTClient ) makeNewSession () error {
179+ func (c * RESTClient ) makeNewSession (ctx context. Context ) error {
171180 var err error
172- c .session , err = c .PostTicket (* c .credentials )
181+ c .session , err = c .PostTicket (ctx , * c .credentials )
173182 if err != nil {
174183 return err
175184 }
0 commit comments