github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/pkg/cmd/factory/http.go (about) 1 package factory 2 3 import ( 4 "fmt" 5 "net/http" 6 "os" 7 "strings" 8 "time" 9 10 "github.com/abdfnx/gh-api/api" 11 "github.com/abdfnx/gh-api/internal/config" 12 "github.com/abdfnx/gh-api/internal/ghinstance" 13 "github.com/abdfnx/gh-api/pkg/iostreams" 14 ) 15 16 var timezoneNames = map[int]string{ 17 -39600: "Pacific/Niue", 18 -36000: "Pacific/Honolulu", 19 -34200: "Pacific/Marquesas", 20 -32400: "America/Anchorage", 21 -28800: "America/Los_Angeles", 22 -25200: "America/Chihuahua", 23 -21600: "America/Chicago", 24 -18000: "America/Bogota", 25 -14400: "America/Caracas", 26 -12600: "America/St_Johns", 27 -10800: "America/Argentina/Buenos_Aires", 28 -7200: "Atlantic/South_Georgia", 29 -3600: "Atlantic/Cape_Verde", 30 0: "Europe/London", 31 3600: "Europe/Amsterdam", 32 7200: "Europe/Athens", 33 10800: "Europe/Istanbul", 34 12600: "Asia/Tehran", 35 14400: "Asia/Dubai", 36 16200: "Asia/Kabul", 37 18000: "Asia/Tashkent", 38 19800: "Asia/Kolkata", 39 20700: "Asia/Kathmandu", 40 21600: "Asia/Dhaka", 41 23400: "Asia/Rangoon", 42 25200: "Asia/Bangkok", 43 28800: "Asia/Manila", 44 31500: "Australia/Eucla", 45 32400: "Asia/Tokyo", 46 34200: "Australia/Darwin", 47 36000: "Australia/Brisbane", 48 37800: "Australia/Adelaide", 49 39600: "Pacific/Guadalcanal", 50 43200: "Pacific/Nauru", 51 46800: "Pacific/Auckland", 52 49500: "Pacific/Chatham", 53 50400: "Pacific/Kiritimati", 54 } 55 56 // generic authenticated HTTP client for commands 57 func NewHTTPClient(io *iostreams.IOStreams, cfg config.Config, appVersion string, setAccept bool) *http.Client { 58 var opts []api.ClientOption 59 if verbose := os.Getenv("DEBUG"); verbose != "" { 60 logTraffic := strings.Contains(verbose, "api") 61 opts = append(opts, api.VerboseLog(io.ErrOut, logTraffic, io.IsStderrTTY())) 62 } 63 64 opts = append(opts, 65 api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", appVersion)), 66 api.AddHeaderFunc("Authorization", func(req *http.Request) (string, error) { 67 hostname := ghinstance.NormalizeHostname(req.URL.Hostname()) 68 if token, err := cfg.Get(hostname, "oauth_token"); err == nil && token != "" { 69 return fmt.Sprintf("token %s", token), nil 70 } 71 return "", nil 72 }), 73 api.AddHeaderFunc("Time-Zone", func(req *http.Request) (string, error) { 74 if req.Method != "GET" && req.Method != "HEAD" { 75 if time.Local.String() != "Local" { 76 return time.Local.String(), nil 77 } 78 _, offset := time.Now().Zone() 79 return timezoneNames[offset], nil 80 } 81 return "", nil 82 }), 83 ) 84 85 if setAccept { 86 opts = append(opts, 87 api.AddHeaderFunc("Accept", func(req *http.Request) (string, error) { 88 // antiope-preview: Checks 89 accept := "application/vnd.github.antiope-preview+json" 90 // introduced for #2952: pr branch up to date status 91 accept += ", application/vnd.github.merge-info-preview+json" 92 if ghinstance.IsEnterprise(req.URL.Hostname()) { 93 // shadow-cat-preview: Draft pull requests 94 accept += ", application/vnd.github.shadow-cat-preview" 95 } 96 return accept, nil 97 }), 98 ) 99 } 100 101 return api.NewHTTPClient(opts...) 102 }