The new logbot, not committed from the wrong terminal window this time.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.7 KiB

6 years ago
6 years ago
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "time"
  11. "git.aiterp.net/rpdata/logbot3/internal/config"
  12. jwt "github.com/dgrijalva/jwt-go"
  13. )
  14. type queryData struct {
  15. Query string `json:"query"`
  16. Variables map[string]interface{} `json:"variables"`
  17. }
  18. type queryResponse struct {
  19. Errors QueryErrorList `json:"errors"`
  20. Data json.RawMessage `json:"data"`
  21. }
  22. // A Client is a client for the rpdata API
  23. type Client struct {
  24. http *http.Client
  25. endpoint string
  26. username string
  27. keyID string
  28. keySecret string
  29. }
  30. func (client *Client) sign(permissions []string) (token string, err error) {
  31. jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
  32. "kid": "bar",
  33. "user": client.username,
  34. "permissions": permissions,
  35. "exp": time.Now().Add(time.Second * 30).Unix(),
  36. })
  37. jwtToken.Header["kid"] = client.keyID
  38. return jwtToken.SignedString([]byte(client.keySecret))
  39. }
  40. // Query sends a query or mutation to the server. The returned value is the data element.
  41. func (client *Client) Query(ctx context.Context, query string, variables map[string]interface{}, permissions []string) (data json.RawMessage, err error) {
  42. // Get token
  43. token, err := client.sign(permissions)
  44. if err != nil {
  45. return
  46. }
  47. // Make body
  48. qdata := queryData{
  49. Query: query,
  50. Variables: variables,
  51. }
  52. buffer := bytes.NewBuffer(make([]byte, 0, 256))
  53. err = json.NewEncoder(buffer).Encode(&qdata)
  54. if err != nil {
  55. return
  56. }
  57. // Make request
  58. req, err := http.NewRequest("POST", client.endpoint, buffer)
  59. if err != nil {
  60. return
  61. }
  62. req = req.WithContext(ctx)
  63. req.Header.Set("Content-Type", "application/json")
  64. req.Header.Set("Authorization", "Bearer "+token)
  65. // Run request
  66. res, err := client.http.Do(req)
  67. if err != nil {
  68. return
  69. }
  70. defer res.Body.Close()
  71. if res.StatusCode >= 500 {
  72. stuff, _ := ioutil.ReadAll(res.Body)
  73. fmt.Println(string(stuff))
  74. return nil, errors.New("Internal server error")
  75. }
  76. // Parse response
  77. resData := queryResponse{}
  78. err = json.NewDecoder(res.Body).Decode(&resData)
  79. if err != nil {
  80. return
  81. }
  82. if len(resData.Errors) > 0 {
  83. err = resData.Errors
  84. return
  85. }
  86. return resData.Data, nil
  87. }
  88. // New makes a new client.
  89. func New(endpoint, username, keyID, keySecret string) *Client {
  90. return &Client{
  91. http: &http.Client{Timeout: 60 * time.Second},
  92. endpoint: endpoint,
  93. username: username,
  94. keyID: keyID,
  95. keySecret: keySecret,
  96. }
  97. }
  98. // Global gets the global client, from the config file.
  99. func Global() *Client {
  100. conf := config.Get()
  101. return &Client{
  102. http: &http.Client{Timeout: 60 * time.Second},
  103. endpoint: conf.API.Endpoint,
  104. username: conf.API.Username,
  105. keyID: conf.API.Key.ID,
  106. keySecret: conf.API.Key.Secret,
  107. }
  108. }