1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/gitter/client.go
Chris Cummer 1bfca29d17
WTF-657 Add spec coverage for cfg/common_settings.go (#728)
* WTF-657 Add spec coverage for cfg/common_settings.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for cfg/position_validation.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for cfg/validations.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for checklist/checklist.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for checklist/checklist_item.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for utils/conversions.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Get rid of utils.Home() function

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for utils/homedir.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for utils/text.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Clean up utils/utils.go

Signed-off-by: Chris Cummer <chriscummer@me.com>
2019-10-30 17:35:00 -07:00

72 lines
1.3 KiB
Go

package gitter
import (
"fmt"
"net/http"
"strconv"
"github.com/wtfutil/wtf/utils"
)
func GetMessages(roomId string, numberOfMessages int, apiToken string) ([]Message, error) {
var messages []Message
resp, err := apiRequest("rooms/"+roomId+"/chatMessages?limit="+strconv.Itoa(numberOfMessages), apiToken)
if err != nil {
return nil, err
}
err = utils.ParseJSON(&messages, resp.Body)
if err != nil {
return nil, err
}
return messages, nil
}
func GetRoom(roomUri, apiToken string) (*Room, error) {
var rooms Rooms
resp, err := apiRequest("rooms?q="+roomUri, apiToken)
if err != nil {
return nil, err
}
err = utils.ParseJSON(&rooms, resp.Body)
if err != nil {
return nil, err
}
for _, room := range rooms.Results {
if room.URI == roomUri {
return &room, nil
}
}
return nil, nil
}
/* -------------------- Unexported Functions -------------------- */
var (
apiBaseURL = "https://api.gitter.im/v1/"
)
func apiRequest(path, apiToken string) (*http.Response, error) {
req, err := http.NewRequest("GET", apiBaseURL+path, nil)
bearer := fmt.Sprintf("Bearer %s", apiToken)
req.Header.Add("Authorization", bearer)
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf(resp.Status)
}
return resp, nil
}