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
|
IotTargeted
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Client holds all the information to communicate with abuseipdb
|
||||||
type Client struct {
|
type Client struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
BaseURL *url.URL
|
BaseURL *url.URL
|
||||||
|
@ -75,7 +76,11 @@ type Client struct {
|
||||||
|
|
||||||
// ratelimit
|
// ratelimit
|
||||||
RateLimit *rate.Limit
|
RateLimit *rate.Limit
|
||||||
|
}
|
||||||
|
|
||||||
|
// CachedClient wraps the Client and overwrites certain methods
|
||||||
|
type CachedClient struct {
|
||||||
|
*Client
|
||||||
cache *bigcache.BigCache
|
cache *bigcache.BigCache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,9 +100,13 @@ func (c *Client) AddApiKey(key string) {
|
||||||
c.APIKey = key
|
c.APIKey = key
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddCache with "ttl", suggestion 30min
|
// Cached returns CachedClient with "ttl", suggestion 30min
|
||||||
func (c *Client) AddCache(eviction time.Duration) {
|
func (c *Client) Cached(eviction time.Duration) *CachedClient {
|
||||||
c.cache, _ = bigcache.New(context.Background(), bigcache.DefaultConfig(eviction))
|
cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(eviction))
|
||||||
|
return &CachedClient{
|
||||||
|
Client: c,
|
||||||
|
cache: cache,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) initialize() {
|
func (c *Client) initialize() {
|
||||||
|
@ -250,11 +259,8 @@ func (c *Client) Check(ctx context.Context, ip net.IP, opts *CheckOptions) (*Che
|
||||||
return &result, nil
|
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)
|
// Check 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) {
|
func (c *CachedClient) Check(ctx context.Context, ip net.IP, opts *CheckOptions) (*CheckResult, error) {
|
||||||
if c.cache == nil {
|
|
||||||
return nil, errors.New("cache is missing")
|
|
||||||
}
|
|
||||||
key := fmt.Sprintf("%s:%s", endpointCheck, ip.String())
|
key := fmt.Sprintf("%s:%s", endpointCheck, ip.String())
|
||||||
if r, err := c.cache.Get(key); err == nil {
|
if r, err := c.cache.Get(key); err == nil {
|
||||||
result := CheckResult{}
|
result := CheckResult{}
|
||||||
|
@ -262,7 +268,7 @@ func (c *Client) CheckCached(ctx context.Context, ip net.IP, opts *CheckOptions)
|
||||||
return &result, nil
|
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{}
|
resultEncoded := bytes.Buffer{}
|
||||||
if jsonErr := json.NewEncoder(&resultEncoded).Encode(result); jsonErr != nil {
|
if jsonErr := json.NewEncoder(&resultEncoded).Encode(result); jsonErr != nil {
|
||||||
return nil, jsonErr
|
return nil, jsonErr
|
||||||
|
@ -516,11 +522,8 @@ func (c *Client) CheckBlock(ctx context.Context, ipnNet *net.IPNet, opts *CheckB
|
||||||
return &result, nil
|
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)
|
// CheckBlock 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) {
|
func (c *CachedClient) CheckBlock(ctx context.Context, ipNet *net.IPNet, opts *CheckBlockOptions) (*CheckBlockResult, error) {
|
||||||
if c.cache == nil {
|
|
||||||
return nil, errors.New("cache is missing")
|
|
||||||
}
|
|
||||||
key := fmt.Sprintf("%s:%s", endpointCheck, ipNet.String())
|
key := fmt.Sprintf("%s:%s", endpointCheck, ipNet.String())
|
||||||
if r, err := c.cache.Get(key); err == nil {
|
if r, err := c.cache.Get(key); err == nil {
|
||||||
result := CheckBlockResult{}
|
result := CheckBlockResult{}
|
||||||
|
@ -528,7 +531,7 @@ func (c *Client) CheckBlockCached(ctx context.Context, ipNet *net.IPNet, opts *C
|
||||||
return &result, nil
|
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{}
|
resultEncoded := bytes.Buffer{}
|
||||||
if jsonErr := json.NewEncoder(&resultEncoded).Encode(result); jsonErr != nil {
|
if jsonErr := json.NewEncoder(&resultEncoded).Encode(result); jsonErr != nil {
|
||||||
return nil, jsonErr
|
return nil, jsonErr
|
||||||
|
|
Loading…
Reference in a new issue