Skip to content

Commit

Permalink
Fix creating *http.Client logic
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Feb 13, 2023
1 parent 32acd25 commit 9b5e304
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,11 @@ func makeFS(owner, repo string, a *github.ReleaseAsset) (fs.FS, error) {
}

func downloadAsset(owner, repo string, a *github.ReleaseAsset) ([]byte, error) {
var (
client *http.Client
err error
)
token, v3ep, _, _ := factory.GetTokenAndEndpoints()
if token == "" {
client = &http.Client{
Timeout: 30 * time.Second,
Transport: http.DefaultTransport.(*http.Transport).Clone(),
}
} else {
client, err = gh.HTTPClient(&api.ClientOptions{})
if err != nil {
return nil, err
}
client, err := httpClient()
if err != nil {
return nil, err
}
_, v3ep, _, _ := factory.GetTokenAndEndpoints()
u := fmt.Sprintf("%s/repos/%s/%s/releases/assets/%d", v3ep, owner, repo, a.GetID())
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
Expand Down Expand Up @@ -275,3 +264,34 @@ func client(ctx context.Context) (*github.Client, error) {
}
return c, nil
}

func httpClient() (*http.Client, error) {
token, v3ep, _, _ := factory.GetTokenAndEndpoints()
if token == "" {
return &http.Client{
Timeout: 30 * time.Second,
Transport: http.DefaultTransport.(*http.Transport).Clone(),
}, nil
}
client, err := gh.HTTPClient(&api.ClientOptions{})
if err != nil {
return nil, err
}
u := fmt.Sprintf("%s/user", v3ep)
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
client = &http.Client{
Timeout: 30 * time.Second,
Transport: http.DefaultTransport.(*http.Transport).Clone(),
}
}
return client, nil
}

0 comments on commit 9b5e304

Please sign in to comment.