client.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /******************************************************************************
  2. * Discordgo by Bruce Marriner <bruce@sqls.net>
  3. * A Discord API for Golang.
  4. * See discord.go for more information.
  5. *
  6. * This file contains functions for interacting with the Discord API
  7. * at the lowest level. See other files for easier methods of access.
  8. */
  9. package discordgo
  10. import (
  11. "bytes"
  12. "encoding/json"
  13. "errors"
  14. "fmt"
  15. "io/ioutil"
  16. "net/http"
  17. "time"
  18. )
  19. // RequestToken asks the Discord server for an authentication token
  20. func Login(session *Session, email string, password string) (token string, err error) {
  21. var urlStr string = fmt.Sprintf("%s/%s", discordApi, "auth/login")
  22. req, err := http.NewRequest("POST", urlStr, bytes.NewBuffer([]byte(fmt.Sprintf(`{"email":"%s", "password":"%s"}`, email, password))))
  23. if err != nil {
  24. return
  25. }
  26. req.Header.Set("Content-Type", "application/json")
  27. client := &http.Client{Timeout: (20 * time.Second)}
  28. resp, err := client.Do(req)
  29. if err != nil {
  30. return
  31. }
  32. defer resp.Body.Close()
  33. body, _ := ioutil.ReadAll(resp.Body)
  34. if resp.StatusCode != 200 {
  35. err = errors.New(fmt.Sprintf("StatusCode: %d, %s", resp.StatusCode, string(body)))
  36. return
  37. }
  38. if session.Debug {
  39. var prettyJSON bytes.Buffer
  40. error := json.Indent(&prettyJSON, body, "", "\t")
  41. if error != nil {
  42. fmt.Print("JSON parse error: ", error)
  43. return
  44. }
  45. fmt.Println("requestToken Response:\n", string(prettyJSON.Bytes()))
  46. }
  47. temp := &Session{} // TODO Must be a better way
  48. err = json.Unmarshal(body, &temp)
  49. token = temp.Token
  50. return
  51. }
  52. // Request makes a REST API GET Request with Discord.
  53. func Request(session *Session, urlStr string) (body []byte, err error) {
  54. req, err := http.NewRequest("GET", urlStr, bytes.NewBuffer([]byte(fmt.Sprintf(``))))
  55. if err != nil {
  56. return
  57. }
  58. req.Header.Set("authorization", session.Token)
  59. req.Header.Set("Content-Type", "application/json")
  60. client := &http.Client{Timeout: (20 * time.Second)}
  61. resp, err := client.Do(req)
  62. if err != nil {
  63. return
  64. }
  65. body, err = ioutil.ReadAll(resp.Body)
  66. resp.Body.Close()
  67. if err != nil {
  68. return
  69. }
  70. if resp.StatusCode != 200 {
  71. err = errors.New(fmt.Sprintf("StatusCode: %d, %s", resp.StatusCode, string(body)))
  72. return
  73. }
  74. if session.Debug {
  75. var prettyJSON bytes.Buffer
  76. error := json.Indent(&prettyJSON, body, "", "\t")
  77. if error != nil {
  78. fmt.Print("JSON parse error: ", error)
  79. return
  80. }
  81. fmt.Println(urlStr+" Response:\n", string(prettyJSON.Bytes()))
  82. }
  83. return
  84. }
  85. // Returns the user details of the given userId
  86. // session : An active session connection to Discord
  87. // user : A user Id or name
  88. func Users(session *Session, userId string) (user User, err error) {
  89. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, "users/%s", userId))
  90. err = json.Unmarshal(body, &user)
  91. return
  92. }
  93. // PrivateChannels returns an array of Channel structures for all private
  94. // channels for a user
  95. func PrivateChannels(session *Session, userId string) (channels []Channel, err error) {
  96. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("users/%s/channels", userId)))
  97. err = json.Unmarshal(body, &channels)
  98. return
  99. }
  100. // Servers returns an array of Server structures for all servers for a user
  101. func Servers(session *Session, userId string) (servers []Server, err error) {
  102. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("users/%s/guilds", userId)))
  103. err = json.Unmarshal(body, &servers)
  104. return
  105. }
  106. // Channels returns an array of Channel structures for all channels of a given
  107. // server.
  108. func Channels(session *Session, serverId int) (channels []Channel, err error) {
  109. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("guilds/%d/channels", serverId)))
  110. err = json.Unmarshal(body, &channels)
  111. return
  112. }
  113. // Close ends a session and logs out from the Discord REST API.
  114. func Close(session *Session) (err error) {
  115. req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("auth/logout")), bytes.NewBuffer([]byte(fmt.Sprintf(``))))
  116. if err != nil {
  117. return
  118. }
  119. req.Header.Set("authorization", session.Token)
  120. req.Header.Set("Content-Type", "application/json")
  121. client := &http.Client{Timeout: (20 * time.Second)}
  122. resp, err := client.Do(req)
  123. if err != nil {
  124. return
  125. }
  126. body, err := ioutil.ReadAll(resp.Body)
  127. if err != nil {
  128. return
  129. }
  130. resp.Body.Close()
  131. if resp.StatusCode != 204 && resp.StatusCode != 200 {
  132. err = errors.New(fmt.Sprintf("StatusCode: %d, %s", resp.StatusCode, string(body)))
  133. return
  134. }
  135. return
  136. }