From 809415b26acb9338e45aec1dfa02c7ab0ca1110d Mon Sep 17 00:00:00 2001 From: Marko Kungla Date: Sun, 31 Mar 2024 13:32:43 +0300 Subject: [PATCH] feat: allow tracking requests_today for subscription Signed-off-by: Marko Kungla --- client.go | 7 ++++--- go.mod | 2 +- go.sum | 4 ++-- koios.go | 2 ++ request.go | 27 +++++++++++++++++---------- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index d9c12d4..6dfe00b 100644 --- a/client.go +++ b/client.go @@ -157,7 +157,7 @@ func (c *Client) request( rsp *http.Response ) if res != nil && c.reqStatsEnabled { - rsp, eqerr = c.requestWithStats(req, res) + rsp, eqerr = c.requestWithStats(req, res, opts.requestsToday) } else { rsp, eqerr = c.client.Do(req) } @@ -198,9 +198,10 @@ func (c *Client) applyReqHeaders(req *http.Request, headers http.Header) { } } -func (c *Client) requestWithStats(req *http.Request, res *Response) (*http.Response, error) { +func (c *Client) requestWithStats(req *http.Request, res *Response, requestsToday uint) (*http.Response, error) { res.Stats = &RequestStats{ - Auth: c.getAuth(), + Auth: c.getAuth(), + RequstesToday: requestsToday, } var dns, tlshs, connect time.Time req = req.WithContext( diff --git a/go.mod b/go.mod index 68a4262..c944cca 100644 --- a/go.mod +++ b/go.mod @@ -9,4 +9,4 @@ require ( golang.org/x/time v0.5.0 ) -require golang.org/x/mod v0.15.0 // indirect +require golang.org/x/mod v0.16.0 // indirect diff --git a/go.sum b/go.sum index a1491ac..24fce82 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/happy-sdk/happy/pkg/version v0.1.2 h1:8DJ0afkEVW2fdm1uEScTS4wPgGe0Tml github.com/happy-sdk/happy/pkg/version v0.1.2/go.mod h1:mFhI4DRvXZ8Ls8D5/aXIqcMnjCUBNtI3Ye9ilYL2Bz0= github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= -golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= -golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= +golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= diff --git a/koios.go b/koios.go index e89a50b..3623d59 100644 --- a/koios.go +++ b/koios.go @@ -162,6 +162,8 @@ type ( ReqDurStr string `json:"req_dur_str,omitempty"` Auth AuthInfo `json:"auth"` + + RequstesToday uint `json:"requests_today,omitempty"` } ) diff --git a/request.go b/request.go index f3c1c2b..c1ac449 100644 --- a/request.go +++ b/request.go @@ -12,11 +12,12 @@ import ( // RequestOptions for the request. type RequestOptions struct { - page uint - pageSize uint - locked bool - query url.Values - headers http.Header + page uint + pageSize uint + locked bool + query url.Values + headers http.Header + requestsToday uint } // QuerySet sets the key to value in request query. @@ -64,11 +65,12 @@ func (ro *RequestOptions) HeaderApply(h http.Header) { // Clone the request options for using it with other request. func (ro *RequestOptions) Clone() *RequestOptions { opts := &RequestOptions{ - headers: ro.headers.Clone(), - page: ro.page, - pageSize: ro.pageSize, - query: ro.query, - locked: false, + headers: ro.headers.Clone(), + page: ro.page, + pageSize: ro.pageSize, + query: ro.query, + requestsToday: ro.requestsToday, + locked: false, } q := url.Values{} for k, v := range ro.query { @@ -87,6 +89,11 @@ func (ro *RequestOptions) SetCurrentPage(page uint) { ro.page = page } +// SetRequestsToday sets the number of requests made today. +func (ro *RequestOptions) SetRequestsToday(n uint) { + ro.requestsToday = n +} + // lock the request options. func (ro *RequestOptions) lock() error { if ro.locked {