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

🎨 remove NewOAuthAPI function, remove HTTPClient field's assignment #565

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions infra/traq/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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(traq.NewConfiguration())
user, resp, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute()
if err != nil {
return nil, err
Expand Down
22 changes: 0 additions & 22 deletions infra/traq/traq.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"net/url"

"github.com/traPtitech/go-traq"
"github.com/traPtitech/knoQ/utils/random"
"golang.org/x/oauth2"
)
Expand All @@ -19,19 +18,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) {
codeVerifier = random.AlphaNumeric(43, true)
result := sha256.Sum256([]byte(codeVerifier))
Expand Down Expand Up @@ -66,11 +52,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
}
8 changes: 4 additions & 4 deletions infra/traq/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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(traq.NewConfiguration())
userDetail, resp, err := apiClient.MeApi.GetMe(ctx).Execute()
if err != nil {
return nil, err
Expand Down
Loading