Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options pattern #276

Merged
merged 4 commits into from
Feb 3, 2025
Merged

Conversation

denopink
Copy link
Contributor

@denopink denopink commented Jan 23, 2025

  • client options
    • get logger
    • store base url
    • store api path
    • http client
    • store bucket
  • listener options
    • set and get logger
    • pull interval
    • listeners (called at each pull interval)
  • allow users to define their own db client instead of using argus

chrysom/fx.go Outdated
Comment on lines 16 to 28
// GetLogger returns a logger from the given context.
type GetLogger func(context.Context) *zap.Logger

// SetLogger embeds the `Listener.logger` in outgoing request contexts for `Listener.Update` calls.
type SetLogger func(context.Context, *zap.Logger) context.Context

type BasicClientIn struct {
fx.In

// Ancla Client config.
Config BasicClientConfig
// GetLogger returns a logger from the given context.
GetLogger GetLogger
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most code was moved to ancla/chrysom/listener/fx.go while the ProvideBasicClient related code was moved to ancla/fx.go

type Items []model.Item

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better home for Items

@@ -95,7 +92,7 @@ func (s *service) GetAll(ctx context.Context) ([]InternalWebhook, error) {
}

// NewService returns an ancla client used to interact with an Argus database.
func NewService(client *chrysom.BasicClient) *service {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the chrysom.PushReader interface instead

Comment on lines -37 to -39
// BasicClientConfig is the configuration for the Argus database client.
BasicClientConfig chrysom.BasicClientConfig

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using client.Options instead

@denopink denopink marked this pull request as draft January 23, 2025 19:48
@denopink denopink force-pushed the denopink/feat/options-integration branch from fe581d2 to 180d7f6 Compare January 23, 2025 20:12
Comment on lines -50 to -53
type BasicClientConfig struct {
// Address is the Argus URL (i.e. https://example-argus.io:8090)
Address string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced by clientOptions

Comment on lines -254 to -260

func validateBasicConfig(config *BasicClientConfig) error {
if config.Address == "" {
return ErrAddressEmpty
}

if config.Bucket == "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced by clientOptions' validation

Comment on lines 160 to 165
url, err := url.JoinPath(c.storeBaseURL, c.storeAPIPath)
if err != nil {
return errors.Join(err, ErrMisconfiguredClient)
}

c.storeBaseURL = url
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url.JoinPath is safer than using fmt.Sprintf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi, validateStoreBaseURL is called after all options are applied

}

return client, nil
}

func ProvideDefaultListenerReader(client *BasicClient) Reader {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to ProvideReaderOption

Comment on lines -48 to -49
type observerConfig struct {
listener Listener
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary struct

}

// Start begins listening for updates on an interval given that client configuration
// is setup correctly. If a listener process is already in progress, calling Start()
// is a NoOp. If you want to restart the current listener process, call Stop() first.
func (c *ListenerClient) Start(ctx context.Context) error {
logger := c.getLogger(ctx)
if c.observer == nil || c.observer.listener == nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.observer struct was removed

@@ -22,7 +24,7 @@ type Pusher interface {
RemoveItem(ctx context.Context, id, owner string) (model.Item, error)
}

type Listener interface {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to ListenerInterface because it collided with the listener option Listener

}

// ProvideBasicClient provides a new BasicClient.
func ProvideBasicClient(in BasicClientIn) (*BasicClient, error) {
Copy link
Contributor Author

@denopink denopink Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code moved into ProvideService, allowing users to define their own PushReader client instead of using the default argus db client.

fx.go Outdated
Comment on lines 32 to 36
// ProvideService provides the Argus client service from the given configuration and client options.
func ProvideService(in ServiceIn) (ProvideServiceOut, error) {
// If the user provides a non-nil chrysom.PushReader (their own db client), then use that instead
// of an Argus db client.
// Otherwise, create and use a new Argus db client.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the logic from ProvideBasicClient into here, allowing users to define their own PushReader client instead of using the default argus db client.

@denopink denopink force-pushed the denopink/feat/options-integration branch from 180d7f6 to e8c26d7 Compare January 23, 2025 20:28
@denopink denopink self-assigned this Jan 23, 2025
@denopink denopink added the enhancement improvement or small functionality added to an existing feature label Jan 23, 2025
@denopink denopink marked this pull request as ready for review January 23, 2025 20:29
@denopink denopink changed the title feat: add options pattern to BasicClient feat: add options pattern Jan 23, 2025
- client options
  -  get logger
  - store base url
  - store api path
  - http client
  - store bucket
- listener options
  - set and get logger
  - pull interval
  - listeners (called at each pull interval)
- allow users to define their own db client instead of using argus
@denopink denopink force-pushed the denopink/feat/options-integration branch from e8c26d7 to 3e2d082 Compare January 23, 2025 20:33
@denopink denopink requested a review from maurafortino January 23, 2025 21:07
Client ListenerClient
}

tcs := []testCase{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests seem a bit overly explicit. I'd probably favor just passing an array of options and confirming the New(opts...) returns an error or not. That makes the code less brittle later.

Copy link

codecov bot commented Jan 31, 2025

Codecov Report

Attention: Patch coverage is 85.27919% with 29 lines in your changes missing coverage. Please review.

Project coverage is 83.83%. Comparing base (24d29af) to head (1dc9f50).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
fx.go 0.00% 13 Missing ⚠️
chrysom/fx.go 0.00% 8 Missing ⚠️
chrysom/basicClientOptions.go 95.58% 2 Missing and 1 partial ⚠️
auth/acquire.go 0.00% 2 Missing ⚠️
chrysom/listenerClient.go 92.85% 1 Missing and 1 partial ⚠️
service.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #276      +/-   ##
==========================================
+ Coverage   80.83%   83.83%   +2.99%     
==========================================
  Files          20       23       +3     
  Lines         793      903     +110     
==========================================
+ Hits          641      757     +116     
+ Misses        134      131       -3     
+ Partials       18       15       -3     
Flag Coverage Δ
unittests 83.83% <85.27%> (+2.99%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


func (f DecoratorFunc) Decorate(ctx context.Context, req *http.Request) error { return f(ctx, req) }

var Nop = DecoratorFunc(func(context.Context, *http.Request) error { return nil })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made Nop public since it's used by our tests and I think it'll be useful for clients when they want to explicitly indicate their ancla client is not configured for auth.

@denopink denopink requested a review from schmidtw February 3, 2025 18:45
@@ -19,7 +19,6 @@ func Provide() fx.Option {
ancla.ProvideListener,
ancla.ProvideDefaultListenerWatchers,
chrysom.ProvideBasicClient,
chrysom.ProvideDefaultListenerReader,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ancla will solely use the argus client until we discover a use case for swappable db clients

@denopink denopink merged commit 94a8289 into main Feb 3, 2025
17 checks passed
@denopink denopink deleted the denopink/feat/options-integration branch February 3, 2025 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improvement or small functionality added to an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants