-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move RateLimitHeaders struct
- Loading branch information
1 parent
e83a111
commit e61d41f
Showing
2 changed files
with
43 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package openai | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// RateLimitHeaders struct represents Openai rate limits headers. | ||
type RateLimitHeaders struct { | ||
LimitRequests int `json:"x-ratelimit-limit-requests"` | ||
LimitTokens int `json:"x-ratelimit-limit-tokens"` | ||
RemainingRequests int `json:"x-ratelimit-remaining-requests"` | ||
RemainingTokens int `json:"x-ratelimit-remaining-tokens"` | ||
ResetRequests ResetTime `json:"x-ratelimit-reset-requests"` | ||
ResetTokens ResetTime `json:"x-ratelimit-reset-tokens"` | ||
} | ||
|
||
type ResetTime string | ||
|
||
func (r ResetTime) String() string { | ||
return string(r) | ||
} | ||
|
||
func (r ResetTime) Time() time.Time { | ||
d, _ := time.ParseDuration(string(r)) | ||
return time.Now().Add(d) | ||
} | ||
|
||
func newRateLimitHeaders(h http.Header) RateLimitHeaders { | ||
limitReq, _ := strconv.Atoi(h.Get("x-ratelimit-limit-requests")) | ||
limitTokens, _ := strconv.Atoi(h.Get("x-ratelimit-limit-tokens")) | ||
remainingReq, _ := strconv.Atoi(h.Get("x-ratelimit-remaining-requests")) | ||
remainingTokens, _ := strconv.Atoi(h.Get("x-ratelimit-remaining-tokens")) | ||
return RateLimitHeaders{ | ||
LimitRequests: limitReq, | ||
LimitTokens: limitTokens, | ||
RemainingRequests: remainingReq, | ||
RemainingTokens: remainingTokens, | ||
ResetRequests: ResetTime(h.Get("x-ratelimit-reset-requests")), | ||
ResetTokens: ResetTime(h.Get("x-ratelimit-reset-tokens")), | ||
} | ||
} |