http_client.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package functions
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "log"
  7. "net/http"
  8. "github.com/gravitl/netmaker/cli/config"
  9. "github.com/gravitl/netmaker/models"
  10. )
  11. func getAuthToken(ctx config.Context, force bool) string {
  12. if !force && ctx.AuthToken != "" {
  13. return ctx.AuthToken
  14. }
  15. authParams := &models.UserAuthParams{UserName: ctx.Username, Password: ctx.Password}
  16. payload, err := json.Marshal(authParams)
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. res, err := http.Post(ctx.Endpoint+"/api/users/adm/authenticate", "application/json", bytes.NewReader(payload))
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. resBodyBytes, err := io.ReadAll(res.Body)
  25. if err != nil {
  26. log.Fatalf("Client could not read response body: %s", err)
  27. }
  28. if res.StatusCode != http.StatusOK {
  29. log.Fatalf("Error Status: %d Response: %s", res.StatusCode, string(resBodyBytes))
  30. }
  31. body := new(models.SuccessResponse)
  32. if err := json.Unmarshal(resBodyBytes, body); err != nil {
  33. log.Fatalf("Error unmarshalling JSON: %s", err)
  34. }
  35. authToken := body.Response.(map[string]any)["AuthToken"].(string)
  36. config.SetAuthToken(authToken)
  37. return authToken
  38. }
  39. func request[T any](method, route string, payload any) *T {
  40. var (
  41. _, ctx = config.GetCurrentContext()
  42. req *http.Request
  43. err error
  44. )
  45. if payload == nil {
  46. req, err = http.NewRequest(method, ctx.Endpoint+route, nil)
  47. if err != nil {
  48. log.Fatalf("Client could not create request: %s", err)
  49. }
  50. } else {
  51. payloadBytes, jsonErr := json.Marshal(payload)
  52. if jsonErr != nil {
  53. log.Fatalf("Error in request JSON marshalling: %s", err)
  54. }
  55. req, err = http.NewRequest(method, ctx.Endpoint+route, bytes.NewReader(payloadBytes))
  56. if err != nil {
  57. log.Fatalf("Client could not create request: %s", err)
  58. }
  59. req.Header.Set("Content-Type", "application/json")
  60. }
  61. if ctx.MasterKey != "" {
  62. req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
  63. } else {
  64. req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, false))
  65. }
  66. retried := false
  67. retry:
  68. res, err := http.DefaultClient.Do(req)
  69. if err != nil {
  70. log.Fatalf("Client error making http request: %s", err)
  71. }
  72. // refresh JWT token
  73. if res.StatusCode == http.StatusUnauthorized && !retried && ctx.MasterKey == "" {
  74. req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
  75. retried = true
  76. goto retry
  77. }
  78. resBodyBytes, err := io.ReadAll(res.Body)
  79. if err != nil {
  80. log.Fatalf("Client could not read response body: %s", err)
  81. }
  82. if res.StatusCode != http.StatusOK {
  83. log.Fatalf("Error Status: %d Response: %s", res.StatusCode, string(resBodyBytes))
  84. }
  85. body := new(T)
  86. if len(resBodyBytes) > 0 {
  87. if err := json.Unmarshal(resBodyBytes, body); err != nil {
  88. log.Fatalf("Error unmarshalling JSON: %s", err)
  89. }
  90. }
  91. return body
  92. }
  93. func get(route string) string {
  94. _, ctx := config.GetCurrentContext()
  95. req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+route, nil)
  96. if err != nil {
  97. log.Fatal(err)
  98. }
  99. if ctx.MasterKey != "" {
  100. req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
  101. } else {
  102. req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
  103. }
  104. res, err := http.DefaultClient.Do(req)
  105. if err != nil {
  106. log.Fatal(err)
  107. }
  108. bodyBytes, err := io.ReadAll(res.Body)
  109. if err != nil {
  110. log.Fatal(err)
  111. }
  112. return string(bodyBytes)
  113. }