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" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strconv"
"time" "time"
) )
@ -24,37 +25,42 @@ const (
// Base URLS // Base URLS
DISCORD = "http://discordapp.com" DISCORD = "http://discordapp.com"
API = DISCORD + "/api" API = DISCORD + "/api"
SERVERS = API + "/guilds" GUILDS = API + "/guilds" // Guilds()
CHANNELS = API + "/channels" CHANNELS = API + "/channels" // Channels()
USERS = API + "/users" USERS = API + "/users" // Users()
LOGIN = API + "/auth/login" LOGIN = API + "/auth/login" // Login()
LOGOUT = API + "/auth/logout" LOGOUT = API + "/auth/logout" // Logout()
GATEWAY = API + "/gateway" 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 // Authenticated User Info
AU = USERS + "/@me" AU = USERS + "/@me"
AU_DEVICES = AU + "/devices" AU_SETTINGS = AU + "/settings" // Call Settings with @me
AU_SETTINGS = AU + "/settings" AU_CHANNELS = AU + "/channels" // Call Channel with @me
AU_CONNECTIONS = AU + "/connections" AU_GUILDS = AU + "/guilds" // Call Guilds with @me
AU_CHANNELS = AU + "/channels"
AU_SERVERS = AU + "/guilds"
// Need a way to handle these here so the variables can be inserted. REGIONS = API + "/voice/regions" // VoiceRegions()
// Maybe defined as functions? 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?
/*
INTEGRATIONS_JOIN: integrationId => `/integrations/${integrationId}/join`, INTEGRATIONS_JOIN: integrationId => `/integrations/${integrationId}/join`,
AVATAR: (userId, hash) => `/users/${userId}/avatars/${hash}.jpg`, AVATAR: (userId, hash) => `/users/${userId}/avatars/${hash}.jpg`,
MESSAGES: channelId => `/channels/${channelId}/messages`, MESSAGES: channelId => `/channels/${channelId}/messages`,
@ -73,10 +79,18 @@ const (
GUILD_EMBED: guildId => `/guilds/${guildId}/embed`, GUILD_EMBED: guildId => `/guilds/${guildId}/embed`,
GUILD_PRUNE: guildId => `/guilds/${guildId}/prune`, GUILD_PRUNE: guildId => `/guilds/${guildId}/prune`,
GUILD_ICON: (guildId, hash) => `/guilds/${guildId}/icons/${hash}.jpg`, GUILD_ICON: (guildId, hash) => `/guilds/${guildId}/icons/${hash}.jpg`,
*/ */
) )
// 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. // Request makes a (GET/POST/?) Requests to Discord REST API.
// All the other functions in this file use this function. // All the other functions in this file use this function.
func (s *Session) Request(method, urlStr, body string) (response []byte, err error) { 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 return
} }
// USERS could pull users channels, servers, settings and so forth too? func (s *Session) VoiceRegions() (vr []VoiceRegion, err error) {
// you know, pull all the data for the user. update the user strut
// to house that data. Seems reasonable. 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 // PrivateChannels returns an array of Channel structures for all private
// channels for a user // channels for a user
@ -178,7 +211,7 @@ func (s *Session) Guilds(userId string) (servers []Guild, err error) {
// server. // server.
func (s *Session) Members(serverId int) (members []Member, err error) { 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) err = json.Unmarshal(body, &members)
return 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 // Channels returns an array of Channel structures for all channels of a given
// server. // 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) err = json.Unmarshal(body, &channels)
return return