@@ -30,13 +30,15 @@ type Response _http.Response
3030type Header _http.Header
3131type Client _http.Client
3232
33+ // NewDefaultClient returns new HTTP client pointer with default timeout
3334func NewDefaultClient () * Client {
3435 return & Client {Timeout : defaultTimeout }
3536}
3637
3738var DefaultClient = NewDefaultClient ()
3839var httpDefaultClient = _http .DefaultClient
3940
41+ // newRequestWithContentType wraps NewRequestWithContext using context.Background and set content type to headers
4042func newRequestWithContentType (method , url string , contentType string , body io.Reader ) (* _http.Request , error ) {
4143 req , err := NewRequest (method , url , body )
4244 if err != nil {
@@ -49,6 +51,8 @@ func newRequestWithContentType(method, url string, contentType string, body io.R
4951 return req , err
5052}
5153
54+ // Do send an HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth)
55+ // as configured on the client.
5256func (c * Client ) Do (req * _http.Request ) (* _http.Response , error ) {
5357
5458 // If url include protocol, then use default http client
@@ -59,6 +63,9 @@ func (c *Client) Do(req *_http.Request) (*_http.Response, error) {
5963
6064}
6165
66+ // processRequest proxy request to external endpoints
67+ // or proxy to Tradologics API if URL doesn't include protocol,
68+ // or proxy to Backtest/ZMQ client if backtest mode is turned on
6269func (c * Client ) processRequest (method , url , contentType string , body io.Reader , header _http.Header ) (* _http.Response , error ) {
6370
6471 if includeProtocol (url ) {
@@ -72,7 +79,6 @@ func (c *Client) processRequest(method, url, contentType string, body io.Reader,
7279 }
7380
7481 if IsBacktest {
75- // TODO error handler
7682 req , err := newRequestWithContentType (method , url , contentType , body )
7783 if err != nil {
7884 return nil , err
@@ -107,42 +113,52 @@ func (c *Client) processRequest(method, url, contentType string, body io.Reader,
107113 return r , nil
108114}
109115
116+ // Head issues a HEAD to the specified URL using default client
110117func Head (url string ) (resp * _http.Response , err error ) {
111118 return DefaultClient .Head (url )
112119}
113120
121+ // Head issues a HEAD to the specified URL
114122func (c * Client ) Head (url string ) (resp * _http.Response , err error ) {
115123 return c .processRequest ("HEAD" , url , "" , nil , nil )
116124}
117125
126+ // Get issues a GET to the specified URL using default client
118127func Get (url string ) (resp * _http.Response , err error ) {
119128 return DefaultClient .Get (url )
120129}
121130
131+ // Get issues a GET to the specified URL
122132func (c * Client ) Get (url string ) (resp * _http.Response , err error ) {
123133 return c .processRequest ("GET" , url , "" , nil , nil )
124134}
125135
136+ // Post issues a POST to the specified URL using default client
126137func Post (url , contentType string , body io.Reader ) (resp * _http.Response , err error ) {
127138 return DefaultClient .Post (url , contentType , body )
128139}
129140
141+ // Post issues a POST to the specified URL
130142func (c * Client ) Post (url , contentType string , body io.Reader ) (resp * _http.Response , err error ) {
131143 return c .processRequest ("POST" , url , contentType , body , nil )
132144}
133145
146+ // PostForm issues a POST to the specified URL using default client
134147func PostForm (url string , data url.Values ) (resp * _http.Response , err error ) {
135148 return DefaultClient .PostForm (url , data )
136149}
137150
151+ // PostForm issues a POST to the specified URL using default client
138152func (c * Client ) PostForm (url string , data url.Values ) (resp * _http.Response , err error ) {
139153 return c .Post (url , "application/x-www-form-urlencoded" , strings .NewReader (data .Encode ()))
140154}
141155
156+ // SetToken set Authorization token
142157func SetToken (token string ) {
143158 Token = token
144159}
145160
161+ // SetBacktestMode turn on backtest mode
146162func SetBacktestMode (start , end string ) (err error ) {
147163 Backtest , err = backtest .NewBacktest (start , end , socketUrl )
148164 if err != nil {
@@ -154,6 +170,7 @@ func SetBacktestMode(start, end string) (err error) {
154170 return nil
155171}
156172
173+ // SetCurrentBarInfo set current Backtest currentBarInfo datetime and resolution
157174func SetCurrentBarInfo (info * backtest.BarInfo ) error {
158175 if Backtest != nil {
159176 Backtest .SetCurrentBarInfo (info )
@@ -163,6 +180,7 @@ func SetCurrentBarInfo(info *backtest.BarInfo) error {
163180 return errors .New ("please set backtest mode first" )
164181}
165182
183+ // GetRuntimeEvents returns current Backtest runtime events
166184func GetRuntimeEvents () (map [string ]interface {}, error ) {
167185 if Backtest != nil {
168186 return Backtest .GetRuntimeEvents (), nil
0 commit comments