-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RELTEC-12262: reinsert accidentally removed caching
- Loading branch information
Showing
8 changed files
with
78 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package githubclient | ||
|
||
import ( | ||
"github.com/gregjones/httpcache" | ||
"net/http" | ||
) | ||
|
||
type CacheConditionCallback func(method string, url string, requestBody interface{}) bool | ||
|
||
type Caching struct { | ||
Wrapped *http.Client | ||
Cache *httpcache.Transport | ||
UseCacheCondition CacheConditionCallback | ||
} | ||
|
||
func NewCaching( | ||
wrapped *http.Client, | ||
useCacheCondition CacheConditionCallback, | ||
) *Caching { | ||
if wrapped == nil { | ||
wrapped = http.DefaultClient | ||
wrapped.Transport = http.DefaultTransport | ||
} | ||
return &Caching{ | ||
Wrapped: wrapped, | ||
Cache: httpcache.NewMemoryCacheTransport(), | ||
UseCacheCondition: useCacheCondition, | ||
} | ||
} | ||
|
||
func (c *Caching) Client() *http.Client { | ||
return &http.Client{Transport: c} | ||
} | ||
|
||
func (c *Caching) RoundTrip(req *http.Request) (resp *http.Response, err error) { | ||
canCache := c.UseCacheCondition(req.Method, req.URL.EscapedPath(), req.Body) | ||
if canCache { | ||
return c.Cache.RoundTrip(req) | ||
} | ||
return c.Wrapped.Transport.RoundTrip(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
package githubclient | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Interhyp/metadata-service/internal/acorn/config" | ||
"github.com/google/go-github/v66/github" | ||
"net/http" | ||
"regexp" | ||
) | ||
|
||
func NewClient(client *http.Client, accessToken string) (*github.Client, error) { | ||
return github.NewClient(client).WithAuthToken(accessToken), nil | ||
func NewClient(client *http.Client, accessToken string, customConfig config.CustomConfiguration) (*github.Client, error) { | ||
matcher := regexp.MustCompile(fmt.Sprintf("/users/[a-z_-]*%s", customConfig.UserPrefix())) | ||
cacheClient := NewCaching( | ||
client, | ||
func(method string, url string, requestBody interface{}) bool { | ||
return method == http.MethodGet && matcher.MatchString(url) | ||
}, | ||
).Client() | ||
return github.NewClient(cacheClient).WithAuthToken(accessToken), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters