Added VoiceRegions and VoiceIce. Clean up endpoint constants

This commit is contained in:
Bruce Marriner 2015-11-14 11:22:00 -06:00
parent 4fc845b7eb
commit 9076028987

View file

@ -15,6 +15,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
@ -24,33 +25,38 @@ const (
// Base URLS
DISCORD = "http://discordapp.com"
API = DISCORD + "/api"
SERVERS = API + "/guilds"
CHANNELS = API + "/channels"
USERS = API + "/users"
LOGIN = API + "/auth/login"
LOGOUT = API + "/auth/logout"
GATEWAY = API + "/gateway"
GUILDS = API + "/guilds" // Guilds()
CHANNELS = API + "/channels" // Channels()
USERS = API + "/users" // Users()
LOGIN = API + "/auth/login" // Login()
LOGOUT = API + "/auth/logout" // Logout()
GATEWAY = API + "/gateway" // Gateway()
// Constants not implemented below yet. TODO tracker :)
REGISTER = API + "/auth/register"
INVITE = API + "/invite"
TRACK = API + "/track"
SSO = API + "/sso"
VERIFY = API + "/auth/verify"
VERIFY_RESEND = API + "/auth/verify/resend"
FORGOT_PASSWORD = API + "/auth/forgot"
RESET_PASSWORD = API + "/auth/reset"
REGIONS = API + "/voice/regions"
ICE = API + "/voice/ice"
REPORT = API + "/report"
INTEGRATIONS = API + "/integrations"
// Authenticated User Info
AU = USERS + "/@me"
AU_DEVICES = AU + "/devices"
AU_SETTINGS = AU + "/settings"
AU_CONNECTIONS = AU + "/connections"
AU_CHANNELS = AU + "/channels"
AU_SERVERS = AU + "/guilds"
AU_SETTINGS = AU + "/settings" // Call Settings with @me
AU_CHANNELS = AU + "/channels" // Call Channel with @me
AU_GUILDS = AU + "/guilds" // Call Guilds with @me
REGIONS = API + "/voice/regions" // VoiceRegions()
ICE = API + "/voice/ice" // VoiceIce()
// : guildId => `/guilds/${guildId}/channels`,
// GUILD_CHANNELS: guildId => `/guilds/${guildId}/channels`,
// TODO: Test below
// AU_DEVICES = AU + "/devices"
// AU_CONNECTIONS = AU + "/connections"
// REGISTER = API + "/auth/register"
// INVITE = API + "/invite"
// TRACK = API + "/track"
// SSO = API + "/sso"
// VERIFY = API + "/auth/verify"
// VERIFY_RESEND = API + "/auth/verify/resend"
// FORGOT_PASSWORD = API + "/auth/forgot"
// RESET_PASSWORD = API + "/auth/reset"
// REPORT = API + "/report"
// INTEGRATIONS = API + "/integrations"
// Need a way to handle these here so the variables can be inserted.
// Maybe defined as functions?
@ -77,6 +83,14 @@ const (
)
// Almost like the constants above :) Dynamic Variables?
var (
GUILD_CHANNELS = func(i int) (s string) {
s = GUILDS + "/" + strconv.Itoa(i) + "/channels"
return
}
)
// Request makes a (GET/POST/?) Requests to Discord REST API.
// All the other functions in this file use this function.
func (s *Session) Request(method, urlStr, body string) (response []byte, err error) {
@ -148,9 +162,28 @@ func (s *Session) Users(userId string) (user User, err error) {
return
}
// USERS could pull users channels, servers, settings and so forth too?
// you know, pull all the data for the user. update the user strut
// to house that data. Seems reasonable.
func (s *Session) VoiceRegions() (vr []VoiceRegion, err error) {
body, err := s.Request("GET", REGIONS, ``)
err = json.Unmarshal(body, &vr)
return
}
func (s *Session) VoiceIce() (ice VoiceIce, err error) {
body, err := s.Request("GET", ICE, ``)
err = json.Unmarshal(body, &ice)
return
}
// Settings returns the settings for a given user
// This seems to only return a result for "@me"
func (s *Session) Settings(userId string) (settings Settings, err error) {
body, err := s.Request("GET", fmt.Sprintf("%s/%s/settings", USERS, userId), ``)
err = json.Unmarshal(body, &settings)
return
}
// PrivateChannels returns an array of Channel structures for all private
// channels for a user
@ -178,7 +211,7 @@ func (s *Session) Guilds(userId string) (servers []Guild, err error) {
// server.
func (s *Session) Members(serverId int) (members []Member, err error) {
body, err := s.Request("GET", fmt.Sprintf("%s/%d/members", SERVERS, serverId), ``)
body, err := s.Request("GET", fmt.Sprintf("%s/%d/members", GUILDS, serverId), ``)
err = json.Unmarshal(body, &members)
return
@ -186,9 +219,10 @@ func (s *Session) Members(serverId int) (members []Member, err error) {
// Channels returns an array of Channel structures for all channels of a given
// server.
func (s *Session) Channels(serverId int) (channels []Channel, err error) {
func (s *Session) Channels(Id int) (channels []Channel, err error) {
body, err := s.Request("GET", fmt.Sprintf("%s/%d/channels", SERVERS, serverId), ``)
// body, err := s.Request("GET", fmt.Sprintf("%s/%d/channels", GUILDS, serverId), ``)
body, err := s.Request("GET", GUILD_CHANNELS(Id), ``)
err = json.Unmarshal(body, &channels)
return