Skip to content

Commit

Permalink
feat: allow tracking requests_today for subscription
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Kungla <marko@mkungla.dev>
  • Loading branch information
mkungla committed May 30, 2024
1 parent 2fc8713 commit 3b11698
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
7 changes: 4 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions koios.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ type (
ReqDurStr string `json:"req_dur_str,omitempty"`

Auth AuthInfo `json:"auth"`

RequstesToday uint `json:"requests_today,omitempty"`
}
)

Expand Down
27 changes: 17 additions & 10 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 3b11698

Please sign in to comment.