-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix/issue439 userAPI #479
Fix/issue439 userAPI #479
Conversation
これを実行するとコードがフォーマットされるのでまずはこれを実行してほしいです:pray: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!いくつかレビューしました!
infra/traq/user.go
Outdated
traqconf := traq.NewConfiguration() | ||
conf := TraQDefaultConfig | ||
traqconf.HTTPClient = conf.Client(context.Background(), token) | ||
apiClient := traq.NewAPIClient(traqconf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここは多分どのAPIも共通だと思うのでメソッドにしてよさそうです
infra/traq/user.go
Outdated
conf := TraQDefaultConfig | ||
traqconf.HTTPClient = conf.Client(context.Background(), token) | ||
apiClient := traq.NewAPIClient(traqconf) | ||
userDtail, _, err := apiClient.UserApi.GetUser(context.Background(), userID.String()).Execute() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここのcontextはAPIクライアントを作るときに使ったものと同じものを使ってほしいです!
utils/api.go
Outdated
|
||
res, err := http.DefaultClient.Do(req) | ||
res, err := apiClient.WebhookApi.PostWebhook(context.Background(), webhookID).XTRAQChannelId(channelID).XTRAQSignature(xTRAQSignature).Embed(int32(embed)).Body(message).Execute() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あまり行が長くなりそうなら分割すると見やすそうです
infra/traq/user.go
Outdated
conf := TraQDefaultConfig | ||
traqconf.HTTPClient = conf.Client(context.Background(), token) | ||
apiClient := traq.NewAPIClient(traqconf) | ||
userDtail, _, err := apiClient.UserApi.GetUser(context.Background(), userID.String()).Execute() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
userDtail, _, err := apiClient.UserApi.GetUser(context.Background(), userID.String()).Execute() | |
userDetail, _, err := apiClient.UserApi.GetUser(context.Background(), userID.String()).Execute() |
infra/traq/user.go
Outdated
func convertMyUserdetailToUser(userdetail *traq.MyUserDetail) *traq.User { | ||
user := new(traq.User) | ||
err = json.Unmarshal(data, &user) | ||
return user, err | ||
user.Id = userdetail.Id | ||
user.Name = userdetail.Name | ||
user.DisplayName = userdetail.DisplayName | ||
user.IconFileId = userdetail.IconFileId | ||
user.Bot = userdetail.Bot | ||
user.State = userdetail.State | ||
user.UpdatedAt = userdetail.UpdatedAt | ||
return user | ||
} | ||
|
||
func convertUserdetailToUser(userdetail *traq.UserDetail) *traq.User { | ||
user := new(traq.User) | ||
user.Id = userdetail.Id | ||
user.Name = userdetail.Name | ||
user.DisplayName = userdetail.DisplayName | ||
user.IconFileId = userdetail.IconFileId | ||
user.Bot = userdetail.Bot | ||
user.State = userdetail.State | ||
user.UpdatedAt = userdetail.UpdatedAt | ||
return user | ||
} | ||
func convertUsersToUsers(users []traq.User) []*traq.User { | ||
new_users := make([]*traq.User, len(users)) | ||
for i, _user := range users { | ||
user := _user | ||
new_users[i] = &user | ||
} | ||
return new_users | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これくらいならconvert関数かかなくてもよさそうな気がします
GetUsers
の返り値も[]traq.User
にしちゃってよさそう
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いくつかかきました!
infra/traq/user.go
Outdated
|
||
"github.com/gofrs/uuid" | ||
"github.com/traPtitech/go-traq" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func (repo *TraQRepository) GetUser(token *oauth2.Token, userID uuid.UUID) (*traq.User, error) { | ||
URL := fmt.Sprintf("%s/users/%s", repo.URL, userID) | ||
req, err := http.NewRequest(http.MethodGet, URL, nil) | ||
apiClient := MakeApiClient(token) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここの引数でcontextを含めるとapiClientとリクエストで単一のcontextを使えるのでそうして欲しいです🙏
infra/traq/traq.go
Outdated
@@ -77,3 +78,11 @@ func (repo *TraQRepository) doRequest(token *oauth2.Token, req *http.Request) ([ | |||
} | |||
return io.ReadAll(resp.Body) | |||
} | |||
|
|||
func MakeApiClient(token *oauth2.Token, ctx context.Context) *traq.APIClient { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
contextは第1引数に取るのが慣例なのでそうして欲しいです🙏
infra/traq/user.go
Outdated
req, err := http.NewRequest(http.MethodGet, URL, nil) | ||
ctx := context.TODO() | ||
apiClient := MakeApiClient(token, ctx) | ||
userDetail, _, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レスポンスのStatus Codeもチェックするといいと思います
infra/traq/user.go
Outdated
user := new(traq.User) | ||
err = json.Unmarshal(data, &user) | ||
user.Id = userDetail.Id | ||
user.Name = userDetail.Name | ||
user.DisplayName = userDetail.DisplayName | ||
user.IconFileId = userDetail.IconFileId | ||
user.Bot = userDetail.Bot | ||
user.State = userDetail.State | ||
user.UpdatedAt = userDetail.UpdatedAt | ||
return user, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
個人的には
user := traq.User {
Id: userDetail.Id,
// ...
}
で一気に書く方が見やすいと思います
utils/api.go
Outdated
|
||
res, err := http.DefaultClient.Do(req) | ||
res, err := apiClient.WebhookApi.PostWebhook(context.Background(), webhookID). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
動作変わらないのでどっちでもいいんですが他のところでcontext.TODO
使ってくれてるので合わせると良さそう
utils/api.go
Outdated
if channelID != "" { | ||
req.Header.Set("X-TRAQ-Channel-Id", channelID) | ||
} | ||
xTRAQSignature := calcSignature(message, secret) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
定義から使用までが少し離れているのでPostWebhookの行の手前とかに定義すると読みやすいと思います
infra/traq/traq.go
Outdated
@@ -77,3 +78,11 @@ func (repo *TraQRepository) doRequest(token *oauth2.Token, req *http.Request) ([ | |||
} | |||
return io.ReadAll(resp.Body) | |||
} | |||
|
|||
func MakeApiClient(token *oauth2.Token, ctx context.Context) *traq.APIClient { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ApiよりもAPIの方が好まれるので直してもらいたいです🙏
ref: https://google.github.io/styleguide/go/decisions#initialisms
あとポインタを返すならMakeよりNewっぽい気がする(割とこの関数を頻繁につかうのでNewも100%しっくり来てるわけじゃないけどGetとかRetrieveもうーんって感じなのでNewでいいです)
修正したので確認をお願いします。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
動作確認しました!よさそうです!
infra/traq/user.go のAPIを呼んでいる部分をgo traqを用いたコードに書き換えました。