提交 d6430af4 编写于 作者: J Jingwen Owen Ou

Extract out logic of building api host

上级 e84a6d6f
package github
import (
"net/url"
"os"
)
type apiHost struct {
Host string
}
func (ah *apiHost) String() string {
host := os.Getenv("GH_API_HOST")
if host == "" && ah.Host != "" {
host = ah.Host
}
if host == GitHubHost {
host = GitHubApiHost
}
return absolute(host)
}
func absolute(endpoint string) string {
u, _ := url.Parse(endpoint)
if u.Scheme == "" {
u.Scheme = "https"
}
return u.String()
}
package github
import (
"testing"
"github.com/bmizerany/assert"
)
func TestApiHost_String(t *testing.T) {
ah := &apiHost{"github.com"}
assert.Equal(t, "https://api.github.com", ah.String())
ah = &apiHost{"github.corporate.com"}
assert.Equal(t, "https://github.corporate.com", ah.String())
ah = &apiHost{"http://github.corporate.com"}
assert.Equal(t, "http://github.corporate.com", ah.String())
}
......@@ -339,7 +339,7 @@ func (client *Client) GhLatestTagName() (tagName string, err error) {
return
}
c := octokit.NewClientWith(client.apiEndpoint(), UserAgent, nil, nil)
c := octokit.NewClientWith(client.apiHost(), UserAgent, nil, nil)
releases, result := c.Releases(client.requestURL(url)).All()
if result.HasError() {
err = fmt.Errorf("Error getting gh release: %s", result.Err)
......@@ -385,7 +385,7 @@ func (client *Client) FindOrCreateToken(user, password, twoFactorCode string) (t
}
basicAuth := octokit.BasicAuth{Login: user, Password: password, OneTimePassword: twoFactorCode}
c := octokit.NewClientWith(client.apiEndpoint(), UserAgent, basicAuth, nil)
c := octokit.NewClientWith(client.apiHost(), UserAgent, basicAuth, nil)
authsService := c.Authorizations(client.requestURL(url))
auths, result := authsService.All()
......@@ -453,7 +453,7 @@ func (client *Client) api() (c *octokit.Client, err error) {
tokenAuth := octokit.TokenAuth{AccessToken: client.Host.AccessToken}
tr := &http.Transport{Proxy: proxyFromEnvironment}
httpClient := &http.Client{Transport: tr}
c = octokit.NewClientWith(client.apiEndpoint(), UserAgent, tokenAuth, httpClient)
c = octokit.NewClientWith(client.apiHost(), UserAgent, tokenAuth, httpClient)
return
}
......@@ -467,26 +467,9 @@ func (client *Client) requestURL(u *url.URL) (uu *url.URL) {
return
}
func (client *Client) apiEndpoint() string {
host := os.Getenv("GH_API_HOST")
if host == "" && client.Host != nil {
host = client.Host.Host
}
if host == GitHubHost {
host = GitHubApiHost
}
return absolute(host)
}
func absolute(endpoint string) string {
u, _ := url.Parse(endpoint)
if u.Scheme == "" {
u.Scheme = "https"
}
return u.String()
func (client *Client) apiHost() string {
ah := &apiHost{client.Host.Host}
return ah.String()
}
func FormatError(action string, err error) (ee error) {
......
......@@ -9,18 +9,7 @@ import (
"github.com/octokit/go-octokit/octokit"
)
func TestClient_ApiEndpoint(t *testing.T) {
gh := &Client{Host: &Host{Host: "github.com"}}
assert.Equal(t, "https://api.github.com", gh.apiEndpoint())
gh = &Client{Host: &Host{Host: "github.corporate.com"}}
assert.Equal(t, "https://github.corporate.com", gh.apiEndpoint())
gh = &Client{Host: &Host{Host: "http://github.corporate.com"}}
assert.Equal(t, "http://github.corporate.com", gh.apiEndpoint())
}
func TestClient_formatError(t *testing.T) {
func TestClient_FormatError(t *testing.T) {
e := &octokit.ResponseError{
Response: &http.Response{
StatusCode: 401,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册