http_client.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 response: %s", 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. retry:
  67. res, err := http.DefaultClient.Do(req)
  68. if err != nil {
  69. log.Fatalf("Client error making http request: %s", err)
  70. }
  71. // refresh JWT token
  72. if res.StatusCode == http.StatusUnauthorized {
  73. req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
  74. goto retry
  75. }
  76. resBodyBytes, err := io.ReadAll(res.Body)
  77. if err != nil {
  78. log.Fatalf("Client could not read response body: %s", err)
  79. }
  80. if res.StatusCode != http.StatusOK {
  81. log.Fatalf("Error Status: %d Response: %s", http.StatusOK, string(resBodyBytes))
  82. }
  83. body := new(T)
  84. if len(resBodyBytes) > 0 {
  85. if err := json.Unmarshal(resBodyBytes, body); err != nil {
  86. log.Fatalf("Error unmarshalling JSON: %s", err)
  87. }
  88. }
  89. return body
  90. }