From 0a1a579adc7dea0ae5f7318a5eb96849517d72a8 Mon Sep 17 00:00:00 2001 From: iChemy Date: Mon, 26 Aug 2024 20:16:15 +0900 Subject: [PATCH 1/2] :art: remove NewOAuthAPI function, remove HTTPCLient field's assignment --- infra/traq/group.go | 4 ++-- infra/traq/traq.go | 19 ------------------- infra/traq/user.go | 4 ++-- 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/infra/traq/group.go b/infra/traq/group.go index 9b05945c..22813572 100644 --- a/infra/traq/group.go +++ b/infra/traq/group.go @@ -40,8 +40,8 @@ func (repo *TraQRepository) GetAllGroups() ([]traq.UserGroup, error) { } func (repo *TraQRepository) GetUserBelongingGroupIDs(token *oauth2.Token, userID uuid.UUID) ([]uuid.UUID, error) { - ctx := context.TODO() - apiClient := NewOauth2APIClient(ctx, token) + ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, token.AccessToken) + apiClient := traq.NewAPIClient(traqAPIConfig) user, resp, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute() if err != nil { return nil, err diff --git a/infra/traq/traq.go b/infra/traq/traq.go index 7c5a6405..3d808bd1 100644 --- a/infra/traq/traq.go +++ b/infra/traq/traq.go @@ -19,17 +19,6 @@ type TraQRepository struct { ServerAccessToken string } -var TraQDefaultConfig = &oauth2.Config{ - ClientID: "something", - ClientSecret: "any", - RedirectURL: "foo", - Scopes: []string{"read", "write", "manage_bot"}, - Endpoint: oauth2.Endpoint{ - AuthURL: "https://q.trap.jp/api/v3/oauth2/authorize", - TokenURL: "https://q.trap.jp/api/v3/oauth2/token", - }, -} - var traqAPIConfig = traq.NewConfiguration() func newPKCE() (pkceOptions []oauth2.AuthCodeOption, codeVerifier string) { @@ -66,11 +55,3 @@ func (repo *TraQRepository) GetOAuthToken(query, state, codeVerifier string) (*o option := oauth2.SetAuthURLParam("code_verifier", codeVerifier) return repo.Config.Exchange(ctx, code, option) } - -func NewOauth2APIClient(ctx context.Context, token *oauth2.Token) *traq.APIClient { - traqconf := traqAPIConfig - conf := TraQDefaultConfig - traqconf.HTTPClient = conf.Client(ctx, token) - apiClient := traq.NewAPIClient(traqconf) - return apiClient -} diff --git a/infra/traq/user.go b/infra/traq/user.go index f8c87e0f..082ed61f 100644 --- a/infra/traq/user.go +++ b/infra/traq/user.go @@ -48,8 +48,8 @@ func (repo *TraQRepository) GetUsers(includeSuspended bool) ([]traq.User, error) } func (repo *TraQRepository) GetUserMe(token *oauth2.Token) (*traq.User, error) { - ctx := context.TODO() - apiClient := NewOauth2APIClient(ctx, token) + ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, token.AccessToken) + apiClient := traq.NewAPIClient(traqAPIConfig) userDetail, resp, err := apiClient.MeApi.GetMe(ctx).Execute() if err != nil { return nil, err From 8ea6bb8fa3c44491da45ccc8bfe786ed184167a3 Mon Sep 17 00:00:00 2001 From: iChemy Date: Mon, 2 Sep 2024 20:46:22 +0900 Subject: [PATCH 2/2] :art: replace traqAPIConfig var to traq.NewConfiguration() --- infra/traq/group.go | 6 +++--- infra/traq/traq.go | 3 --- infra/traq/user.go | 6 +++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/infra/traq/group.go b/infra/traq/group.go index 22813572..21c3b126 100644 --- a/infra/traq/group.go +++ b/infra/traq/group.go @@ -11,7 +11,7 @@ import ( func (repo *TraQRepository) GetGroup(groupID uuid.UUID) (*traq.UserGroup, error) { ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, repo.ServerAccessToken) - apiClient := traq.NewAPIClient(traqAPIConfig) + apiClient := traq.NewAPIClient(traq.NewConfiguration()) // TODO: 一定期間キャッシュする group, resp, err := apiClient.GroupApi.GetUserGroup(ctx, groupID.String()).Execute() if err != nil { @@ -26,7 +26,7 @@ func (repo *TraQRepository) GetGroup(groupID uuid.UUID) (*traq.UserGroup, error) func (repo *TraQRepository) GetAllGroups() ([]traq.UserGroup, error) { ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, repo.ServerAccessToken) - apiClient := traq.NewAPIClient(traqAPIConfig) + apiClient := traq.NewAPIClient(traq.NewConfiguration()) // TODO: 一定期間キャッシュする groups, resp, err := apiClient.GroupApi.GetUserGroups(ctx).Execute() if err != nil { @@ -41,7 +41,7 @@ func (repo *TraQRepository) GetAllGroups() ([]traq.UserGroup, error) { func (repo *TraQRepository) GetUserBelongingGroupIDs(token *oauth2.Token, userID uuid.UUID) ([]uuid.UUID, error) { ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, token.AccessToken) - apiClient := traq.NewAPIClient(traqAPIConfig) + apiClient := traq.NewAPIClient(traq.NewConfiguration()) user, resp, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute() if err != nil { return nil, err diff --git a/infra/traq/traq.go b/infra/traq/traq.go index 3d808bd1..ab5bc5b4 100644 --- a/infra/traq/traq.go +++ b/infra/traq/traq.go @@ -7,7 +7,6 @@ import ( "errors" "net/url" - "github.com/traPtitech/go-traq" "github.com/traPtitech/knoQ/utils/random" "golang.org/x/oauth2" ) @@ -19,8 +18,6 @@ type TraQRepository struct { ServerAccessToken string } -var traqAPIConfig = traq.NewConfiguration() - func newPKCE() (pkceOptions []oauth2.AuthCodeOption, codeVerifier string) { codeVerifier = random.AlphaNumeric(43, true) result := sha256.Sum256([]byte(codeVerifier)) diff --git a/infra/traq/user.go b/infra/traq/user.go index 082ed61f..1bd4ff9a 100644 --- a/infra/traq/user.go +++ b/infra/traq/user.go @@ -10,7 +10,7 @@ import ( func (repo *TraQRepository) GetUser(userID uuid.UUID) (*traq.User, error) { ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, repo.ServerAccessToken) - apiClient := traq.NewAPIClient(traqAPIConfig) + apiClient := traq.NewAPIClient(traq.NewConfiguration()) // TODO: 一定期間キャッシュする userDetail, resp, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute() if err != nil { @@ -34,7 +34,7 @@ func (repo *TraQRepository) GetUser(userID uuid.UUID) (*traq.User, error) { func (repo *TraQRepository) GetUsers(includeSuspended bool) ([]traq.User, error) { ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, repo.ServerAccessToken) - apiClient := traq.NewAPIClient(traqAPIConfig) + apiClient := traq.NewAPIClient(traq.NewConfiguration()) // TODO: 一定期間キャッシュする users, resp, err := apiClient.UserApi.GetUsers(ctx).IncludeSuspended(includeSuspended).Execute() if err != nil { @@ -49,7 +49,7 @@ func (repo *TraQRepository) GetUsers(includeSuspended bool) ([]traq.User, error) func (repo *TraQRepository) GetUserMe(token *oauth2.Token) (*traq.User, error) { ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, token.AccessToken) - apiClient := traq.NewAPIClient(traqAPIConfig) + apiClient := traq.NewAPIClient(traq.NewConfiguration()) userDetail, resp, err := apiClient.MeApi.GetMe(ctx).Execute() if err != nil { return nil, err