Skip to content

Commit

Permalink
create getOrGenerateToken
Browse files Browse the repository at this point in the history
  • Loading branch information
aviau committed Sep 14, 2024
1 parent d6687f4 commit 85629c4
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,22 @@ func (client *ApiClient) isApiTokenExpired() bool {
return client.apiTokenExp.Before(time.Now())
}

func (client *ApiClient) do(request *http.Request) (*http.Response, error) {
if client.isApiTokenExpired() {
if _, err := client.GenerateToken(); err != nil {
return nil, err
}
func (client *ApiClient) getOrGenerateToken() (string, error) {
if !client.isApiTokenExpired() {
return client.apiToken, nil
}
apiToken := client.apiToken

request.Header.Add(
"Authorization",
fmt.Sprintf("Bearer %s", apiToken),
)
return client.GenerateToken()
}

func (client *ApiClient) do(request *http.Request) (*http.Response, error) {
if apiToken, err := client.getOrGenerateToken(); err != nil {
return nil, err
} else {
request.Header.Add(
"Authorization",
fmt.Sprintf("Bearer %s", apiToken),
)
}
return client.httpClient.Do(request)
}

Expand Down

0 comments on commit 85629c4

Please sign in to comment.