func InitAPI(userAgentIn string) { client.UserAgent = userAgentIn tempDir, err := ioutil.TempDir("", "goztivo_") if err != nil { panic(err) } cache := diskcache.New(tempDir) transport = httpcache.NewTransport(cache) client = ozClient{} client.Client = transport.Client() client.Mutex = &sync.Mutex{} client.LastFetch = time.Now() dataList = &DataList{} dataList.Mutex = &sync.Mutex{} go func() { for { dataList.Mutex.Lock() err := getDataList() dataList.Mutex.Unlock() if err != nil { log.Println(err) return } time.Sleep(time.Hour * 24) } }() }
func client(owner, repository, token string) (*gphr.GitHub, error) { cache := httpcache.NewMemoryCache() client := httpcache.NewTransport(cache).Client() gh := gphr.NewGitHub(owner, repository, client, token) return gh, nil }
func newTransportContext() context.Context { var cache httpcache.Cache = httpcache.NewMemoryCache() if cachePath := os.Getenv("GITHUB_CACHE_PATH"); cachePath != "" { cache = diskcache.New(cachePath) } tr := httpcache.NewTransport(cache) c := &http.Client{Transport: tr} return context.WithValue(context.Background(), oauth2.HTTPClient, c) }
func CachingHttpClient() *http.Client { if *diskCacheDir == "" { return nil } if *diskCacheDir == defaultDiskCacheDir { user, err := user.Current() d.Chk.NoError(err) *diskCacheDir = path.Join(user.HomeDir, "noms", "httpcache") } return httpcache.NewTransport(diskcache.New(*diskCacheDir)).Client() }
// Client initializes the authorization with the GitHub API func (g GitHub) Client() *octokat.Client { var cache httpcache.Cache if cachePath := os.Getenv("GITHUB_CACHE_PATH"); cachePath != "" { cache = diskcache.New(cachePath) } else { cache = httpcache.NewMemoryCache() } tr := httpcache.NewTransport(cache) c := &http.Client{Transport: tr} gh := octokat.NewClient() gh = gh.WithToken(g.AuthToken) gh = gh.WithHTTPClient(c) return gh }
func newCacheTransport() *httpcache.Transport { // host and port are set by GAE Flex runtime, can be left blank locally. host := os.Getenv("MEMCACHE_PORT_11211_TCP_ADDR") if host == "" { host = "localhost" } port := os.Getenv("MEMCACHE_PORT_11211_TCP_PORT") if port == "" { port = "11211" } addr := fmt.Sprintf("%s:%s", host, port) return httpcache.NewTransport( memcache.New(addr), ) }
func main() { clientID := flag.String("id", "", "Github client ID") clientSecret := flag.String("secret", "", "Github client secret") output := flag.String("output", "gddoexp.out", "Output file") progress := flag.Bool("progress", false, "Show a progress bar") flag.Parse() var auth *gddoexp.GithubAuth if (clientID != nil && *clientID != "") || (clientSecret != nil && *clientSecret != "") { if *clientID == "" || *clientSecret == "" { fmt.Println("to enable Gthub authentication, you need to inform the id and secret") flag.PrintDefaults() return } auth = &gddoexp.GithubAuth{ ID: *clientID, Secret: *clientSecret, } } // add cache to avoid repeated requests to Github gddoexp.HTTPClient = &http.Client{ Transport: httpcache.NewTransport( diskcache.New(path.Join(os.Getenv("HOME"), ".gddoexp")), ), } db, err := database.New() if err != nil { fmt.Println("error connecting to database:", err) return } pkgs, err := db.AllPackages() if err != nil { fmt.Println("error retrieving all packages:", err) return } file, err := os.OpenFile(*output, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { fmt.Println("error creating output file:", err) return } defer file.Close() log.SetOutput(file) log.Println("BEGIN") log.Printf("%d packages will be analyzed", len(pkgs)) var progressBar *pb.ProgressBar if progress != nil && *progress { progressBar = pb.StartNew(len(pkgs)) } var cache int for response := range gddoexp.ShouldSuppressPackages(pkgs, db, auth) { if progress != nil && *progress { progressBar.Increment() } if response.Cache { cache++ } if response.Error != nil { log.Println(response.Error) } else if response.Suppress { log.Printf("package “%s” should be suppressed\n", response.Package.Path) if progress != nil && !*progress { fmt.Println(response.Package.Path) } } } if progress != nil && *progress { progressBar.Finish() } log.Println("Cache hits:", cache) log.Println("END") }