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.
|
||||
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}
|
||||
ticker := time.NewTicker(time.Second * 10)
|
||||
defer ticker.Stop()
|
||||
|
|
|
@ -19,12 +19,12 @@ const elonID = "44196397"
|
|||
// TweetPersister fetches tweets from twitter and persists them in the database.
|
||||
type TweetPersister struct {
|
||||
store twitterapi.Store
|
||||
apiClient twitterapi.APIClient
|
||||
apiClient twitterapi.Client
|
||||
logger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
// 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}
|
||||
}
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ type TwitterAPIClient interface {
|
|||
|
||||
type handler struct {
|
||||
store twitterapi.Store
|
||||
twitterAPIClientFunc func(c *http.Client) twitterapi.APIClient
|
||||
twitterAPIClientFunc func(c *http.Client) twitterapi.Client
|
||||
oauth2Config *oauth2.Config
|
||||
renderer *templates.Renderer
|
||||
sessionStore sessions.Store
|
||||
|
@ -71,7 +71,7 @@ type handler struct {
|
|||
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.Use(middleware.RequestID)
|
||||
r.Use(middleware.RealIP)
|
||||
|
|
|
@ -66,7 +66,7 @@ func TestGetIndex(t *testing.T) {
|
|||
handler := httpserver.NewHandler(
|
||||
config.Config{},
|
||||
&mocks.Store{},
|
||||
func(*http.Client) twitterapi.APIClient { return &mocks.TwitterAPIClient{} },
|
||||
func(*http.Client) twitterapi.Client { return &mocks.TwitterAPIClient{} },
|
||||
&mockSessionStore,
|
||||
&mockTokenGenerator{},
|
||||
zap.NewNop(),
|
||||
|
@ -123,7 +123,7 @@ func TestGetLogin(t *testing.T) {
|
|||
},
|
||||
},
|
||||
&mocks.Store{},
|
||||
func(*http.Client) twitterapi.APIClient { return &mocks.TwitterAPIClient{} },
|
||||
func(*http.Client) twitterapi.Client { return &mocks.TwitterAPIClient{} },
|
||||
&mockSessionStore,
|
||||
&mockTokenGenerator{tokens: []string{"state", "pkceVerifier"}},
|
||||
zap.NewNop(),
|
||||
|
@ -303,7 +303,7 @@ func TestGetCallback(t *testing.T) {
|
|||
},
|
||||
},
|
||||
&mockStore,
|
||||
func(*http.Client) twitterapi.APIClient { return &mockTwitterClient },
|
||||
func(*http.Client) twitterapi.Client { return &mockTwitterClient },
|
||||
&mockSessionStore,
|
||||
nil,
|
||||
zap.NewNop(),
|
||||
|
@ -338,7 +338,7 @@ func TestPostLogout(t *testing.T) {
|
|||
handler := httpserver.NewHandler(
|
||||
config.Config{},
|
||||
&mocks.Store{},
|
||||
func(*http.Client) twitterapi.APIClient { return &mocks.TwitterAPIClient{} },
|
||||
func(*http.Client) twitterapi.Client { return &mocks.TwitterAPIClient{} },
|
||||
&mockSessionStore,
|
||||
nil,
|
||||
zap.NewNop(),
|
||||
|
|
2
main.go
2
main.go
|
@ -47,7 +47,7 @@ func main() {
|
|||
handler := httpserver.NewHandler(
|
||||
cfg,
|
||||
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)),
|
||||
httpserver.RandomTokenGenerator{},
|
||||
logger,
|
||||
|
|
|
@ -13,21 +13,21 @@ type Getter interface {
|
|||
}
|
||||
|
||||
// NewClient returns a new APIClient.
|
||||
func NewClient(httpclient Getter) APIClient {
|
||||
return &apiClient{httpclient}
|
||||
func NewClient(httpclient Getter) Client {
|
||||
return &client{httpclient}
|
||||
}
|
||||
|
||||
// NewAPIClient returns a new APIClient which will authenticate with the
|
||||
// provided bearer token.
|
||||
func NewClientWithBearerToken(bearerToken string) APIClient {
|
||||
return &apiClient{&http.Client{
|
||||
func NewClientWithBearerToken(bearerToken string) Client {
|
||||
return &client{&http.Client{
|
||||
Timeout: time.Second * 5,
|
||||
Transport: &bearerTokenTransport{bearerToken: bearerToken},
|
||||
}}
|
||||
}
|
||||
|
||||
// apiClient implements APIClient.
|
||||
type apiClient struct {
|
||||
// client implements APIClient.
|
||||
type client struct {
|
||||
Getter
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,7 @@ func cloneRequest(r *http.Request) *http.Request {
|
|||
}
|
||||
|
||||
// GetMe returns the currently authenticated user.
|
||||
func (c *apiClient) GetMe() (*User, error) {
|
||||
func (c *client) GetMe() (*User, error) {
|
||||
type oauthResponse struct {
|
||||
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
|
||||
// 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 {
|
||||
Data []*Tweet `json:"data"`
|
||||
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
|
||||
// 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 {
|
||||
Data []*Tweet `json:"data"`
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@ type Store interface {
|
|||
UpsertElonTweet(context.Context, store.UpsertElonTweetParams) (store.ElonTweet, error)
|
||||
}
|
||||
|
||||
// APIClient is a client for the Twitter API.
|
||||
type APIClient interface {
|
||||
// Client is a client for the Twitter API.
|
||||
type Client interface {
|
||||
GetLastTweet(string) (*Tweet, error)
|
||||
GetTweets(string, string) ([]*Tweet, error)
|
||||
GetMe() (*User, error)
|
||||
|
|
Reference in New Issue