Skip to content

Commit

Permalink
Fix: "all_teams" data resource causes 429 status code requests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Dec 8, 2024
1 parent 8a80a6a commit 0a351aa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 4 additions & 1 deletion client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ApiClient struct {
http http.HttpClientInterface
cachedOrganizationId string
defaultOrganizationId string
memoizedGetTeams func(string) ([]Team, error)
}

type ApiClientInterface interface {
Expand Down Expand Up @@ -172,9 +173,11 @@ type ApiClientInterface interface {
}

func NewApiClient(client http.HttpClientInterface, defaultOrganizationId string) ApiClientInterface {
return &ApiClient{
apiClient := &ApiClient{
http: client,
cachedOrganizationId: "",
defaultOrganizationId: defaultOrganizationId,
}
apiClient.memoizedGetTeams = memoize(apiClient.GetTeams)
return apiClient
}
18 changes: 18 additions & 0 deletions client/memoize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package client

type memoizedResult[V any] struct {
value V
err error
}

func memoize[K comparable, V any](f func(K) (V, error)) func(K) (V, error) {
cache := make(map[K]memoizedResult[V])
return func(key K) (V, error) {
if res, ok := cache[key]; ok {
return res.value, res.err
}
value, err := f(key)
cache[key] = memoizedResult[V]{value: value, err: err}
return value, err
}
}
11 changes: 8 additions & 3 deletions client/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ func (client *ApiClient) TeamUpdate(id string, payload TeamUpdatePayload) (Team,
return result, nil
}

func (client *ApiClient) GetTeams(params map[string]string) ([]Team, error) {
func (client *ApiClient) GetTeams(name string) ([]Team, error) {
params := map[string]string{"limit": "100"}
if name != "" {
params["name"] = name
}

organizationId, err := client.OrganizationId()
if err != nil {
return nil, err
Expand Down Expand Up @@ -112,9 +117,9 @@ func (client *ApiClient) GetTeams(params map[string]string) ([]Team, error) {
}

func (client *ApiClient) Teams() ([]Team, error) {
return client.GetTeams(map[string]string{"limit": "100"})
return client.memoizedGetTeams("")
}

func (client *ApiClient) TeamsByName(name string) ([]Team, error) {
return client.GetTeams(map[string]string{"name": name, "limit": "100"})
return client.memoizedGetTeams(name)
}

0 comments on commit 0a351aa

Please sign in to comment.