feature: switch to nested struct and overwriting for cached versions
This commit is contained in:
parent
4f1fe4840e
commit
70d28fa7c4
1 changed files with 18 additions and 15 deletions
|
@ -66,6 +66,7 @@ const (
|
|||
IotTargeted
|
||||
)
|
||||
|
||||
// Client holds all the information to communicate with abuseipdb
|
||||
type Client struct {
|
||||
client *http.Client
|
||||
BaseURL *url.URL
|
||||
|
@ -75,7 +76,11 @@ type Client struct {
|
|||
|
||||
// ratelimit
|
||||
RateLimit *rate.Limit
|
||||
}
|
||||
|
||||
// CachedClient wraps the Client and overwrites certain methods
|
||||
type CachedClient struct {
|
||||
*Client
|
||||
cache *bigcache.BigCache
|
||||
}
|
||||
|
||||
|
@ -95,9 +100,13 @@ func (c *Client) AddApiKey(key string) {
|
|||
c.APIKey = key
|
||||
}
|
||||
|
||||
// AddCache with "ttl", suggestion 30min
|
||||
func (c *Client) AddCache(eviction time.Duration) {
|
||||
c.cache, _ = bigcache.New(context.Background(), bigcache.DefaultConfig(eviction))
|
||||
// Cached returns CachedClient with "ttl", suggestion 30min
|
||||
func (c *Client) Cached(eviction time.Duration) *CachedClient {
|
||||
cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(eviction))
|
||||
return &CachedClient{
|
||||
Client: c,
|
||||
cache: cache,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) initialize() {
|
||||
|
@ -250,11 +259,8 @@ func (c *Client) Check(ctx context.Context, ip net.IP, opts *CheckOptions) (*Che
|
|||
return &result, nil
|
||||
}
|
||||
|
||||
// CheckCached same as Check but uses the client cache and the ip as a key (cache has to be added to client for this to work)
|
||||
func (c *Client) CheckCached(ctx context.Context, ip net.IP, opts *CheckOptions) (*CheckResult, error) {
|
||||
if c.cache == nil {
|
||||
return nil, errors.New("cache is missing")
|
||||
}
|
||||
// Check uses the client cache and the ip as a key (cache has to be added to client for this to work)
|
||||
func (c *CachedClient) Check(ctx context.Context, ip net.IP, opts *CheckOptions) (*CheckResult, error) {
|
||||
key := fmt.Sprintf("%s:%s", endpointCheck, ip.String())
|
||||
if r, err := c.cache.Get(key); err == nil {
|
||||
result := CheckResult{}
|
||||
|
@ -262,7 +268,7 @@ func (c *Client) CheckCached(ctx context.Context, ip net.IP, opts *CheckOptions)
|
|||
return &result, nil
|
||||
}
|
||||
}
|
||||
if result, err := c.Check(ctx, ip, opts); err == nil {
|
||||
if result, err := c.Client.Check(ctx, ip, opts); err == nil {
|
||||
resultEncoded := bytes.Buffer{}
|
||||
if jsonErr := json.NewEncoder(&resultEncoded).Encode(result); jsonErr != nil {
|
||||
return nil, jsonErr
|
||||
|
@ -516,11 +522,8 @@ func (c *Client) CheckBlock(ctx context.Context, ipnNet *net.IPNet, opts *CheckB
|
|||
return &result, nil
|
||||
}
|
||||
|
||||
// CheckBlockCached same as CheckBlock but uses the client cache and the ipnet as a key (cache has to be added to client for this to work)
|
||||
func (c *Client) CheckBlockCached(ctx context.Context, ipNet *net.IPNet, opts *CheckBlockOptions) (*CheckBlockResult, error) {
|
||||
if c.cache == nil {
|
||||
return nil, errors.New("cache is missing")
|
||||
}
|
||||
// CheckBlock uses the client cache and the ipnet as a key (cache has to be added to client for this to work)
|
||||
func (c *CachedClient) CheckBlock(ctx context.Context, ipNet *net.IPNet, opts *CheckBlockOptions) (*CheckBlockResult, error) {
|
||||
key := fmt.Sprintf("%s:%s", endpointCheck, ipNet.String())
|
||||
if r, err := c.cache.Get(key); err == nil {
|
||||
result := CheckBlockResult{}
|
||||
|
@ -528,7 +531,7 @@ func (c *Client) CheckBlockCached(ctx context.Context, ipNet *net.IPNet, opts *C
|
|||
return &result, nil
|
||||
}
|
||||
}
|
||||
if result, err := c.CheckBlock(ctx, ipNet, opts); err == nil {
|
||||
if result, err := c.Client.CheckBlock(ctx, ipNet, opts); err == nil {
|
||||
resultEncoded := bytes.Buffer{}
|
||||
if jsonErr := json.NewEncoder(&resultEncoded).Encode(result); jsonErr != nil {
|
||||
return nil, jsonErr
|
||||
|
|
Loading…
Reference in a new issue