-
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
Changes from 9 commits
df321ca
2566f5f
9a6859d
5c3a168
86d2298
8c0016b
a071658
82393b7
862b47a
5c7fa49
b2613cd
2f0d599
7608d82
d352c8a
43166d6
3956f65
866f601
bee1a24
eab8062
965039f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,67 +1,81 @@ | ||||||
package traq | ||||||
|
||||||
import ( | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"net/http" | ||||||
"net/url" | ||||||
"strconv" | ||||||
"context" | ||||||
|
||||||
"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) | ||||||
traqconf := traq.NewConfiguration() | ||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. ここのcontextはAPIクライアントを作るときに使ったものと同じものを使ってほしいです! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
data, err := repo.doRequest(token, req) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
user := new(traq.User) | ||||||
err = json.Unmarshal(data, &user) | ||||||
user := convertUserdetailToUser(userDtail) | ||||||
return user, err | ||||||
} | ||||||
|
||||||
func (repo *TraQRepository) GetUsers(token *oauth2.Token, includeSuspended bool) ([]*traq.User, error) { | ||||||
URL, err := url.Parse(fmt.Sprintf("%s/users", repo.URL)) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
q := URL.Query() | ||||||
q.Set("include-suspended", strconv.FormatBool(includeSuspended)) | ||||||
URL.RawQuery = q.Encode() | ||||||
req, err := http.NewRequest(http.MethodGet, URL.String(), nil) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
data, err := repo.doRequest(token, req) | ||||||
traqconf := traq.NewConfiguration() | ||||||
conf := TraQDefaultConfig | ||||||
traqconf.HTTPClient = conf.Client(context.Background(), token) | ||||||
apiClient := traq.NewAPIClient(traqconf) | ||||||
users, _, err := apiClient.UserApi.GetUsers(context.Background()).IncludeSuspended(includeSuspended).Execute() | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
users := make([]*traq.User, 0) | ||||||
err = json.Unmarshal(data, &users) | ||||||
return users, err | ||||||
res_users := convertUsersToUsers(users) | ||||||
return res_users, err | ||||||
} | ||||||
|
||||||
func (repo *TraQRepository) GetUserMe(token *oauth2.Token) (*traq.User, error) { | ||||||
URL := fmt.Sprintf("%s/users/me", repo.URL) | ||||||
req, err := http.NewRequest(http.MethodGet, URL, nil) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
data, err := repo.doRequest(token, req) | ||||||
traqconf := traq.NewConfiguration() | ||||||
conf := TraQDefaultConfig | ||||||
traqconf.HTTPClient = conf.Client(context.Background(), token) | ||||||
client := traq.NewAPIClient(traqconf) | ||||||
|
||||||
data, _, err := client.MeApi.GetMe(context.Background()).Execute() | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
user := convertMyUserdetailToUser(data) | ||||||
return user, err | ||||||
} | ||||||
|
||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. これくらいならconvert関数かかなくてもよさそうな気がします |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,32 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"crypto/hmac" | ||
"crypto/sha1" | ||
"encoding/hex" | ||
"errors" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/traPtitech/go-traq" | ||
) | ||
|
||
const baseURL = "https://q.trap.jp/api/v3" | ||
|
||
|
||
// RequestWebhook q.trap/jp にメッセージを送信します。 | ||
func RequestWebhook(message, secret, channelID, webhookID string, embed int) error { | ||
u, err := url.Parse(baseURL + "/webhooks") | ||
if err != nil { | ||
return err | ||
} | ||
u.Path = path.Join(u.Path, webhookID) | ||
query := u.Query() | ||
query.Set("embed", strconv.Itoa(embed)) | ||
u.RawQuery = query.Encode() | ||
|
||
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(message)) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set(echo.HeaderContentType, echo.MIMETextPlain) | ||
req.Header.Set("X-TRAQ-Signature", calcSignature(message, secret)) | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 定義から使用までが少し離れているのでPostWebhookの行の手前とかに定義すると読みやすいと思います |
||
configuration := traq.NewConfiguration() | ||
apiClient := traq.NewAPIClient(configuration) | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. あまり行が長くなりそうなら分割すると見やすそうです |
||
if err != nil { | ||
return err | ||
} | ||
if res.StatusCode >= 400 { | ||
return errors.New(http.StatusText(res.StatusCode)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
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も共通だと思うのでメソッドにしてよさそうです