Commit a67607b0 authored by Brian Lindner's avatar Brian Lindner

RESTCommand now allows headers to be specified

parent 344c9d3c
......@@ -25,12 +25,13 @@ type RESTCommand struct {
// RESTConfig is the configuration for the RESTCommand.
type RESTConfig struct {
TriggerRegex string `json:"triggerregex"`
Endpoint []interface{} `json:"endpoint"`
Method string `json:"method"`
Response []string `json:"response"`
Responses [][]string `json:"responses"`
ErrorMessage string `json:"errorMessage"`
TriggerRegex string `json:"triggerregex"`
Endpoint []interface{} `json:"endpoint"`
Method string `json:"method"`
Response []string `json:"response"`
Responses [][]string `json:"responses"`
ErrorMessage string `json:"errorMessage"`
Headers map[string]string `json:"headers"`
}
// NewRESTCommand generates a new RESTCommand.
......@@ -82,7 +83,7 @@ func NewRESTCommand(config BaseCommand) (command RESTCommand, err error) {
return command, nil
}
func (r RESTCommand) SendErrorMessage(bot *discordgo.Session, evt *discordgo.MessageCreate) {
func (r RESTCommand) sendErrorMessage(bot *discordgo.Session, evt *discordgo.MessageCreate) {
if len(r.ErrorMessage) > 0 {
bot.ChannelMessageSend(evt.Message.ChannelID, r.ErrorMessage)
}
......@@ -105,9 +106,13 @@ func (r RESTCommand) Run(bot *discordgo.Session, evt *discordgo.MessageCreate) (
// Construct request based on this endpoint
request, err := http.NewRequest(r.Method, endpoint, nil)
if err != nil {
r.SendErrorMessage(bot, evt)
r.sendErrorMessage(bot, evt)
return err
}
// Set headers
for key, value := range r.Headers {
request.Header.Set(key, value)
}
// Log that we're about to send the request, in case someone's trying something nasty
log.WithFields(log.Fields{
"endpoint": endpoint,
......@@ -116,7 +121,7 @@ func (r RESTCommand) Run(bot *discordgo.Session, evt *discordgo.MessageCreate) (
// Send request, ensure nothing failed, get JSON bytes
resp, err := r.client.Do(request)
if err != nil {
r.SendErrorMessage(bot, evt)
r.sendErrorMessage(bot, evt)
return errors.New("could not make request: " + err.Error())
}
// Log some response metadata, again, in case someone's being nasty
......@@ -125,13 +130,13 @@ func (r RESTCommand) Run(bot *discordgo.Session, evt *discordgo.MessageCreate) (
"response": resp.Status,
}).Info("HTTP request result")
if resp.StatusCode >= 400 {
r.SendErrorMessage(bot, evt)
r.sendErrorMessage(bot, evt)
return errors.New("request failed with status " + resp.Status)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
r.SendErrorMessage(bot, evt)
r.sendErrorMessage(bot, evt)
return errors.New("could not parse request body: " + err.Error())
}
// For single-response:
......@@ -170,6 +175,6 @@ func (r RESTCommand) Run(bot *discordgo.Session, evt *discordgo.MessageCreate) (
}
}
// If this code is reached, no response was valid, which probably shouldn't happen, so we'll throw an error
r.SendErrorMessage(bot, evt)
r.sendErrorMessage(bot, evt)
return errors.New("No valid response schema")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment