From 98e60edb195c2f29b929530c9c5e41abf281b6c5 Mon Sep 17 00:00:00 2001 From: Anish Mukherjee Date: Fri, 9 Dec 2022 19:55:27 +0530 Subject: [PATCH] retry only once for refreshing JWT --- cli/functions/http_client.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cli/functions/http_client.go b/cli/functions/http_client.go index e1c4b265..e470b7cc 100644 --- a/cli/functions/http_client.go +++ b/cli/functions/http_client.go @@ -29,7 +29,7 @@ func getAuthToken(ctx config.Context, force bool) string { log.Fatalf("Client could not read response body: %s", err) } if res.StatusCode != http.StatusOK { - log.Fatalf("Error response: %s", string(resBodyBytes)) + log.Fatalf("Error Status: %d Response: %s", res.StatusCode, string(resBodyBytes)) } body := new(models.SuccessResponse) if err := json.Unmarshal(resBodyBytes, body); err != nil { @@ -67,14 +67,16 @@ func request[T any](method, route string, payload any) *T { } else { req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, false)) } + retried := false retry: res, err := http.DefaultClient.Do(req) if err != nil { log.Fatalf("Client error making http request: %s", err) } // refresh JWT token - if res.StatusCode == http.StatusUnauthorized { + if res.StatusCode == http.StatusUnauthorized && !retried { req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true)) + retried = true goto retry } resBodyBytes, err := io.ReadAll(res.Body) @@ -82,7 +84,7 @@ retry: log.Fatalf("Client could not read response body: %s", err) } if res.StatusCode != http.StatusOK { - log.Fatalf("Error Status: %d Response: %s", http.StatusOK, string(resBodyBytes)) + log.Fatalf("Error Status: %d Response: %s", res.StatusCode, string(resBodyBytes)) } body := new(T) if len(resBodyBytes) > 0 {