Rename APIClient > Client
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
3f134bd417
commit
7bb49d0860
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run blocks until the provided context is terminated.
|
// Run blocks until the provided context is terminated.
|
||||||
func Run(ctx context.Context, store twitterapi.Store, apiClient twitterapi.APIClient, logger *zap.SugaredLogger) error {
|
func Run(ctx context.Context, store twitterapi.Store, apiClient twitterapi.Client, logger *zap.SugaredLogger) error {
|
||||||
tweetPersister := TweetPersister{store: store, apiClient: apiClient, logger: logger}
|
tweetPersister := TweetPersister{store: store, apiClient: apiClient, logger: logger}
|
||||||
ticker := time.NewTicker(time.Second * 10)
|
ticker := time.NewTicker(time.Second * 10)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
|
@ -19,12 +19,12 @@ const elonID = "44196397"
|
||||||
// TweetPersister fetches tweets from twitter and persists them in the database.
|
// TweetPersister fetches tweets from twitter and persists them in the database.
|
||||||
type TweetPersister struct {
|
type TweetPersister struct {
|
||||||
store twitterapi.Store
|
store twitterapi.Store
|
||||||
apiClient twitterapi.APIClient
|
apiClient twitterapi.Client
|
||||||
logger *zap.SugaredLogger
|
logger *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTweetPersister creates a new NewTweetPersister.
|
// NewTweetPersister creates a new NewTweetPersister.
|
||||||
func NewTweetPersister(store twitterapi.Store, apiClient twitterapi.APIClient, logger *zap.SugaredLogger) *TweetPersister {
|
func NewTweetPersister(store twitterapi.Store, apiClient twitterapi.Client, logger *zap.SugaredLogger) *TweetPersister {
|
||||||
return &TweetPersister{store: store, apiClient: apiClient, logger: logger}
|
return &TweetPersister{store: store, apiClient: apiClient, logger: logger}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ type TwitterAPIClient interface {
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
store twitterapi.Store
|
store twitterapi.Store
|
||||||
twitterAPIClientFunc func(c *http.Client) twitterapi.APIClient
|
twitterAPIClientFunc func(c *http.Client) twitterapi.Client
|
||||||
oauth2Config *oauth2.Config
|
oauth2Config *oauth2.Config
|
||||||
renderer *templates.Renderer
|
renderer *templates.Renderer
|
||||||
sessionStore sessions.Store
|
sessionStore sessions.Store
|
||||||
|
@ -71,7 +71,7 @@ type handler struct {
|
||||||
logger *zap.SugaredLogger
|
logger *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandler(cfg config.Config, store twitterapi.Store, twitterAPIClientFunc func(c *http.Client) twitterapi.APIClient, sessionStore sessions.Store, tokenGenerator TokenGenerator, logger *zap.Logger) http.Handler {
|
func NewHandler(cfg config.Config, store twitterapi.Store, twitterAPIClientFunc func(c *http.Client) twitterapi.Client, sessionStore sessions.Store, tokenGenerator TokenGenerator, logger *zap.Logger) http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Use(middleware.RequestID)
|
r.Use(middleware.RequestID)
|
||||||
r.Use(middleware.RealIP)
|
r.Use(middleware.RealIP)
|
||||||
|
|
|
@ -66,7 +66,7 @@ func TestGetIndex(t *testing.T) {
|
||||||
handler := httpserver.NewHandler(
|
handler := httpserver.NewHandler(
|
||||||
config.Config{},
|
config.Config{},
|
||||||
&mocks.Store{},
|
&mocks.Store{},
|
||||||
func(*http.Client) twitterapi.APIClient { return &mocks.TwitterAPIClient{} },
|
func(*http.Client) twitterapi.Client { return &mocks.TwitterAPIClient{} },
|
||||||
&mockSessionStore,
|
&mockSessionStore,
|
||||||
&mockTokenGenerator{},
|
&mockTokenGenerator{},
|
||||||
zap.NewNop(),
|
zap.NewNop(),
|
||||||
|
@ -123,7 +123,7 @@ func TestGetLogin(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&mocks.Store{},
|
&mocks.Store{},
|
||||||
func(*http.Client) twitterapi.APIClient { return &mocks.TwitterAPIClient{} },
|
func(*http.Client) twitterapi.Client { return &mocks.TwitterAPIClient{} },
|
||||||
&mockSessionStore,
|
&mockSessionStore,
|
||||||
&mockTokenGenerator{tokens: []string{"state", "pkceVerifier"}},
|
&mockTokenGenerator{tokens: []string{"state", "pkceVerifier"}},
|
||||||
zap.NewNop(),
|
zap.NewNop(),
|
||||||
|
@ -303,7 +303,7 @@ func TestGetCallback(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&mockStore,
|
&mockStore,
|
||||||
func(*http.Client) twitterapi.APIClient { return &mockTwitterClient },
|
func(*http.Client) twitterapi.Client { return &mockTwitterClient },
|
||||||
&mockSessionStore,
|
&mockSessionStore,
|
||||||
nil,
|
nil,
|
||||||
zap.NewNop(),
|
zap.NewNop(),
|
||||||
|
@ -338,7 +338,7 @@ func TestPostLogout(t *testing.T) {
|
||||||
handler := httpserver.NewHandler(
|
handler := httpserver.NewHandler(
|
||||||
config.Config{},
|
config.Config{},
|
||||||
&mocks.Store{},
|
&mocks.Store{},
|
||||||
func(*http.Client) twitterapi.APIClient { return &mocks.TwitterAPIClient{} },
|
func(*http.Client) twitterapi.Client { return &mocks.TwitterAPIClient{} },
|
||||||
&mockSessionStore,
|
&mockSessionStore,
|
||||||
nil,
|
nil,
|
||||||
zap.NewNop(),
|
zap.NewNop(),
|
||||||
|
|
2
main.go
2
main.go
|
@ -47,7 +47,7 @@ func main() {
|
||||||
handler := httpserver.NewHandler(
|
handler := httpserver.NewHandler(
|
||||||
cfg,
|
cfg,
|
||||||
store,
|
store,
|
||||||
func(c *http.Client) twitterapi.APIClient { return twitterapi.NewClient(c) },
|
func(c *http.Client) twitterapi.Client { return twitterapi.NewClient(c) },
|
||||||
sessions.NewCookieStore([]byte(cfg.SessionKey)),
|
sessions.NewCookieStore([]byte(cfg.SessionKey)),
|
||||||
httpserver.RandomTokenGenerator{},
|
httpserver.RandomTokenGenerator{},
|
||||||
logger,
|
logger,
|
||||||
|
|
|
@ -13,21 +13,21 @@ type Getter interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns a new APIClient.
|
// NewClient returns a new APIClient.
|
||||||
func NewClient(httpclient Getter) APIClient {
|
func NewClient(httpclient Getter) Client {
|
||||||
return &apiClient{httpclient}
|
return &client{httpclient}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAPIClient returns a new APIClient which will authenticate with the
|
// NewAPIClient returns a new APIClient which will authenticate with the
|
||||||
// provided bearer token.
|
// provided bearer token.
|
||||||
func NewClientWithBearerToken(bearerToken string) APIClient {
|
func NewClientWithBearerToken(bearerToken string) Client {
|
||||||
return &apiClient{&http.Client{
|
return &client{&http.Client{
|
||||||
Timeout: time.Second * 5,
|
Timeout: time.Second * 5,
|
||||||
Transport: &bearerTokenTransport{bearerToken: bearerToken},
|
Transport: &bearerTokenTransport{bearerToken: bearerToken},
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// apiClient implements APIClient.
|
// client implements APIClient.
|
||||||
type apiClient struct {
|
type client struct {
|
||||||
Getter
|
Getter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ func cloneRequest(r *http.Request) *http.Request {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMe returns the currently authenticated user.
|
// GetMe returns the currently authenticated user.
|
||||||
func (c *apiClient) GetMe() (*User, error) {
|
func (c *client) GetMe() (*User, error) {
|
||||||
type oauthResponse struct {
|
type oauthResponse struct {
|
||||||
Data *User `json:"data"`
|
Data *User `json:"data"`
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ var ErrNoTweets = errors.New("no tweets available")
|
||||||
|
|
||||||
// GetLastTweet returns the most recent tweet for a given user. If no tweets
|
// GetLastTweet returns the most recent tweet for a given user. If no tweets
|
||||||
// are available, ErrNoTweets will be returned.
|
// are available, ErrNoTweets will be returned.
|
||||||
func (c *apiClient) GetLastTweet(userID string) (*Tweet, error) {
|
func (c *client) GetLastTweet(userID string) (*Tweet, error) {
|
||||||
type oauthResponse struct {
|
type oauthResponse struct {
|
||||||
Data []*Tweet `json:"data"`
|
Data []*Tweet `json:"data"`
|
||||||
Meta TwitterMetadata `json:"meta"`
|
Meta TwitterMetadata `json:"meta"`
|
||||||
|
@ -127,7 +127,7 @@ func (c *apiClient) GetLastTweet(userID string) (*Tweet, error) {
|
||||||
|
|
||||||
// GetTweets returns the latest tweets for a given user, up to the maximum
|
// GetTweets returns the latest tweets for a given user, up to the maximum
|
||||||
// batch size of 100 allowable by the Twitter API.
|
// batch size of 100 allowable by the Twitter API.
|
||||||
func (c *apiClient) GetTweets(userID string, sinceID string) ([]*Tweet, error) {
|
func (c *client) GetTweets(userID string, sinceID string) ([]*Tweet, error) {
|
||||||
type oauthResponse struct {
|
type oauthResponse struct {
|
||||||
Data []*Tweet `json:"data"`
|
Data []*Tweet `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ type Store interface {
|
||||||
UpsertElonTweet(context.Context, store.UpsertElonTweetParams) (store.ElonTweet, error)
|
UpsertElonTweet(context.Context, store.UpsertElonTweetParams) (store.ElonTweet, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// APIClient is a client for the Twitter API.
|
// Client is a client for the Twitter API.
|
||||||
type APIClient interface {
|
type Client interface {
|
||||||
GetLastTweet(string) (*Tweet, error)
|
GetLastTweet(string) (*Tweet, error)
|
||||||
GetTweets(string, string) ([]*Tweet, error)
|
GetTweets(string, string) ([]*Tweet, error)
|
||||||
GetMe() (*User, error)
|
GetMe() (*User, error)
|
||||||
|
|
Reference in New Issue