New creates a new API client connecting to the configured endpoints with the HTTP client.
(f *cmdutil.Factory)
| 69 | |
| 70 | // New creates a new API client connecting to the configured endpoints with the HTTP client. |
| 71 | func New(f *cmdutil.Factory) *API { |
| 72 | apiURL := os.Getenv("GITHUB_API_URL") |
| 73 | if apiURL == "" { |
| 74 | cfg, err := f.Config() |
| 75 | if err != nil { |
| 76 | // fallback to the default api endpoint |
| 77 | apiURL = defaultAPIURL |
| 78 | } else { |
| 79 | host, _ := cfg.Authentication().DefaultHost() |
| 80 | apiURL = ghinstance.RESTPrefix(host) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | serverURL := os.Getenv("GITHUB_SERVER_URL") |
| 85 | if serverURL == "" { |
| 86 | cfg, err := f.Config() |
| 87 | if err != nil { |
| 88 | // fallback to the default server endpoint |
| 89 | serverURL = defaultServerURL |
| 90 | } else { |
| 91 | host, _ := cfg.Authentication().DefaultHost() |
| 92 | serverURL = ghinstance.HostPrefix(host) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return &API{ |
| 97 | client: f.HttpClient, |
| 98 | externalClient: f.ExternalHttpClient, |
| 99 | githubAPI: strings.TrimSuffix(apiURL, "/"), |
| 100 | githubServer: strings.TrimSuffix(serverURL, "/"), |
| 101 | retryBackoff: 100 * time.Millisecond, |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // User represents a GitHub user. |
| 106 | type User struct { |