diff --git a/blocks_api.go b/blocks_api.go index b41b79a..cfcc4bb 100644 --- a/blocks_api.go +++ b/blocks_api.go @@ -44,7 +44,7 @@ func (c *Client) IndexBlock(userID ID) ([]*Block, error) { var resp struct { Blocks []*Block `json:"blocks"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -77,7 +77,7 @@ func (c *Client) BlockBetween(userID, otherUserID ID) (bool, error) { var resp struct { Between bool `json:"between"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return false, err } @@ -111,7 +111,7 @@ func (c *Client) CreateBlock(userID, otherUserID ID) (*Block, error) { var resp struct { Block *Block `json:"block"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -142,7 +142,7 @@ func (c *Client) Unblock(userID, otherUserID ID) error { query.Set("otherUser", otherUserID.String()) URL.RawQuery = query.Encode() - err = c.do(httpReq, nil) + err = c.doWithAuthToken(httpReq, nil) if err != nil { return err } diff --git a/bots_api.go b/bots_api.go index b901223..80c23c9 100644 --- a/bots_api.go +++ b/bots_api.go @@ -52,7 +52,7 @@ func (c *Client) CreateBot(bot *Bot) (*Bot, error) { httpReq.PostForm = data var resp Bot - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -108,7 +108,7 @@ func (c *Client) IndexBots() ([]*Bot, error) { } var resp []*Bot - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -139,5 +139,5 @@ func (c *Client) DestroyBot(botID ID) error { httpReq.PostForm = data - return c.do(httpReq, nil) + return c.doWithAuthToken(httpReq, nil) } diff --git a/bots_api_test.go b/bots_api_test.go index 9662e70..49f87ae 100644 --- a/bots_api_test.go +++ b/bots_api_test.go @@ -51,10 +51,11 @@ func TestBotsAPISuite(t *testing.T) { } func botsTestRouter() *mux.Router { - router := mux.NewRouter().Queries("token", "").Subrouter() + router := mux.NewRouter() + authRouter := router.Queries("token", "").Subrouter() // Create - router.Path("/bots"). + authRouter.Path("/bots"). Methods("POST"). Name("CreateBot"). HandlerFunc(func(w http.ResponseWriter, req *http.Request) { @@ -84,7 +85,7 @@ func botsTestRouter() *mux.Router { }) // Index - router.Path("/bots"). + authRouter.Path("/bots"). Methods("GET"). Name("IndexBots"). HandlerFunc(func(w http.ResponseWriter, req *http.Request) { @@ -108,7 +109,7 @@ func botsTestRouter() *mux.Router { }) // Destroy - router.Path("/bots/destroy"). + authRouter.Path("/bots/destroy"). Methods("POST"). Name("DestroyBot"). HandlerFunc(func(w http.ResponseWriter, req *http.Request) { diff --git a/chats_api.go b/chats_api.go index 5d685c8..1d5082f 100644 --- a/chats_api.go +++ b/chats_api.go @@ -52,7 +52,7 @@ func (c *Client) IndexChats(req *IndexChatsQuery) ([]*Chat, error) { URL.RawQuery = query.Encode() var resp []*Chat - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } diff --git a/client.go b/client.go index 3879a27..1e09d02 100644 --- a/client.go +++ b/client.go @@ -59,11 +59,6 @@ func (r response) UnmarshalJSON(bs []byte) error { } func (c Client) do(req *http.Request, i interface{}) error { - URL := req.URL - query := URL.Query() - query.Set("token", c.authorizationToken) - URL.RawQuery = query.Encode() - getResp, err := c.httpClient.Do(req) if err != nil { return err @@ -95,3 +90,12 @@ func (c Client) do(req *http.Request, i interface{}) error { return nil } + +func (c Client) doWithAuthToken(req *http.Request, i interface{}) error { + URL := req.URL + query := URL.Query() + query.Set("token", c.authorizationToken) + URL.RawQuery = query.Encode() + + return c.do(req, i) +} diff --git a/direct_messages_api.go b/direct_messages_api.go index 090e9fe..4f96360 100644 --- a/direct_messages_api.go +++ b/direct_messages_api.go @@ -83,7 +83,7 @@ func (c *Client) IndexDirectMessages(otherUserID ID, req *IndexDirectMessagesQue } var resp IndexDirectMessagesResponse - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return IndexDirectMessagesResponse{}, err } @@ -136,7 +136,7 @@ func (c *Client) CreateDirectMessage(m *Message) (*Message, error) { var resp struct { *Message `json:"message"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } diff --git a/groups_api.go b/groups_api.go index b7d9721..041e3a0 100755 --- a/groups_api.go +++ b/groups_api.go @@ -105,7 +105,7 @@ func (c *Client) IndexGroups(req *GroupsQuery) ([]*Group, error) { URL.RawQuery = query.Encode() var resp []*Group - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -127,7 +127,7 @@ func (c *Client) FormerGroups() ([]*Group, error) { } var resp []*Group - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -154,7 +154,7 @@ func (c *Client) ShowGroup(groupID ID) (*Group, error) { } var resp Group - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -195,7 +195,7 @@ func (c *Client) CreateGroup(gs GroupSettings) (*Group, error) { httpReq.PostForm = data var resp Group - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -241,7 +241,7 @@ func (c *Client) UpdateGroup(groupID ID, gs GroupSettings) (*Group, error) { httpReq.PostForm = data var resp Group - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -269,7 +269,7 @@ func (c *Client) DestroyGroup(groupID ID) error { return err } - return c.do(httpReq, nil) + return c.doWithAuthToken(httpReq, nil) } ///// Join ///// @@ -292,7 +292,7 @@ func (c *Client) JoinGroup(groupID ID, shareToken string) (*Group, error) { } var resp Group - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -321,7 +321,7 @@ func (c *Client) RejoinGroup(groupID ID) (*Group, error) { httpReq.PostForm = data var resp Group - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -357,7 +357,7 @@ func (c *Client) ChangeGroupOwner(reqs ChangeOwnerRequest) (ChangeOwnerResult, e Results []ChangeOwnerResult `json:"results"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return ChangeOwnerResult{}, err } diff --git a/leaderboard_api.go b/leaderboard_api.go index bb534e6..0d5b14b 100644 --- a/leaderboard_api.go +++ b/leaderboard_api.go @@ -60,7 +60,7 @@ func (c *Client) IndexLeaderboard(groupID ID, p period) ([]*Message, error) { var resp struct { Messages []*Message `json:"messages"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -90,7 +90,7 @@ func (c *Client) MyLikesLeaderboard(groupID ID) ([]*Message, error) { var resp struct { Messages []*Message `json:"messages"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -120,7 +120,7 @@ func (c *Client) MyHitsLeaderboard(groupID ID) ([]*Message, error) { var resp struct { Messages []*Message `json:"messages"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } diff --git a/likes_api.go b/likes_api.go index d712e7d..4107053 100644 --- a/likes_api.go +++ b/likes_api.go @@ -37,7 +37,7 @@ func (c *Client) CreateLike(conversationID, messageID ID) error { return err } - return c.do(httpReq, nil) + return c.doWithAuthToken(httpReq, nil) } // Destroy @@ -59,5 +59,5 @@ func (c *Client) DestroyLike(conversationID, messageID ID) error { return err } - return c.do(httpReq, nil) + return c.doWithAuthToken(httpReq, nil) } diff --git a/members_api.go b/members_api.go index da864d8..24da3b9 100644 --- a/members_api.go +++ b/members_api.go @@ -66,7 +66,7 @@ func (c *Client) AddMembers(groupID ID, members ...*Member) (string, error) { ResultsID string `json:"result_id"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return "", err } @@ -104,7 +104,7 @@ func (c *Client) AddMembersResults(groupID ID, resultID string) ([]*Member, erro Members []*Member `json:"members"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -133,7 +133,7 @@ func (c *Client) RemoveMember(groupID, membershipID ID) error { return err } - return c.do(httpReq, nil) + return c.doWithAuthToken(httpReq, nil) } ///// Update ///// @@ -165,7 +165,7 @@ func (c *Client) UpdateMember(groupID ID, nickname string) (*Member, error) { var resp Member - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } diff --git a/messages_api.go b/messages_api.go index 21c42fe..6d169ad 100644 --- a/messages_api.go +++ b/messages_api.go @@ -110,7 +110,7 @@ func (c *Client) IndexMessages(groupID ID, req *IndexMessagesQuery) (IndexMessag URL.RawQuery = query.Encode() var resp IndexMessagesResponse - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return IndexMessagesResponse{}, err } @@ -165,7 +165,7 @@ func (c *Client) CreateMessage(groupID ID, m *Message) (*Message, error) { var resp struct { *Message `json:"message"` } - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } diff --git a/sms_mode_api.go b/sms_mode_api.go index c05d4f5..7385bc0 100644 --- a/sms_mode_api.go +++ b/sms_mode_api.go @@ -50,7 +50,7 @@ func (c *Client) CreateSMSMode(duration int, registrationID *ID) error { httpReq.PostForm = data - err = c.do(httpReq, nil) + err = c.doWithAuthToken(httpReq, nil) if err != nil { return err } @@ -73,5 +73,5 @@ func (c *Client) DeleteSMSMode() error { return err } - return c.do(httpReq, nil) + return c.doWithAuthToken(httpReq, nil) } diff --git a/users_api.go b/users_api.go index b9a87fb..f773a49 100644 --- a/users_api.go +++ b/users_api.go @@ -39,7 +39,7 @@ func (c *Client) MyUser() (*User, error) { } var resp User - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err } @@ -96,7 +96,7 @@ func (c *Client) UpdateMyUser(us UserSettings) (*User, error) { httpReq.PostForm = data var resp User - err = c.do(httpReq, &resp) + err = c.doWithAuthToken(httpReq, &resp) if err != nil { return nil, err }