コード例 #1
0
ファイル: openfec_test.go プロジェクト: tmc/openfec
func ExampleClient_GetCandidates() {
	apiKey := os.Getenv("DATA_GOV_API_KEY")
	if apiKey == "" {
		apiKey = "DEMO_KEY"
	}
	client, err := openfec.NewClient(apiKey)
	if err != nil {
		log.Fatalln(client)
	}
	query := &openfec.CandidateQuery{
		Sort:   "name",
		Office: []openfec.Office{openfec.President},
		Cycle:  []int{2016},
	}
	candidates, err := client.GetCandidates(query)
	if err != nil {
		log.Fatalln(err)
	}
	for candidates.Next() {
		c := candidates.Value()
		_ = c
		// do something with each candidate here
	}
	fmt.Println(err)
	// output:
	// <nil>
}
コード例 #2
0
ファイル: openfec_test.go プロジェクト: WesternStar/openfec
func ExampleNewClient() {
	apiKey := os.Getenv("DATA_GOV_API_KEY")
	client, err := openfec.NewClient(apiKey)
	if err != nil {
		log.Fatalln(client)
	}
	candidates, err := client.GetCandidates(nil)
	fmt.Printf("err:%v\ntype1:%T\n", err, candidates)
	// output:
	// err:<nil>
	// type1:*openfec.CandidateIter
}
コード例 #3
0
ファイル: main.go プロジェクト: tmc/openfec
func main() {
	apiKey := os.Getenv("DATA_GOV_API_KEY")
	if apiKey == "" {
		apiKey = "DEMO_KEY"
	}
	flag.Parse()
	client, err := openfec.NewClient(apiKey)
	if err != nil {
		log.Fatalln(client)
	}
	if *verbose {
		client.TraceOn(log.New(os.Stderr, "openfec: ", log.LstdFlags))
	}
	query := &openfec.CandidateQuery{
		Sort:            "name",
		Office:          []openfec.Office{openfec.President},
		CandidateStatus: []openfec.CandidateStatus{openfec.PresentCandidate},
		Cycle:           []int{*year},
		Party:           *party,
	}
	candidates, err := client.GetCandidates(query)
	if err != nil {
		if err == openfec.ErrUnauthorized {
			fmt.Println("Authorization failure. Check the value of the 'DATA_GOV_API_KEY' environment variable.")
			os.Exit(1)
		} else {
			log.Fatalln(err)
		}
	}
	tmpl, err := template.New("format string").Parse(*format)
	if err != nil {
		log.Fatalln(err)
	}
	for candidates.Next() {
		value := candidates.Value()
		if *format == "json" {
			buf, _ := json.Marshal(value)
			fmt.Println(string(buf))
		} else {
			tmpl.Execute(os.Stdout, value)
			fmt.Println()
		}
	}
	if candidates.Err() != nil {
		fmt.Println("Issue iterating candidates:", candidates.Err())
	}
}
コード例 #4
0
ファイル: main.go プロジェクト: tmc/openfec
func main() {
	apiKey := os.Getenv("DATA_GOV_API_KEY")
	if apiKey == "" {
		apiKey = "DEMO_KEY"
	}
	flag.Parse()
	client, err := openfec.NewClient(apiKey)
	if err != nil {
		log.Fatalln(client)
	}
	if *verbose {
		client.TraceOn(log.New(os.Stderr, "openfec: ", log.LstdFlags))
	}
	filings, err := client.GetCandidateFilings(*candidate)
	if err != nil {
		if err == openfec.ErrUnauthorized {
			fmt.Println("Authorization failure. Check the value of the 'DATA_GOV_API_KEY' environment variable.")
			os.Exit(1)
		} else {
			log.Fatalln(err)
		}
	}
	tmpl, err := template.New("format string").Parse(*format)
	if err != nil {
		log.Fatalln(err)
	}
	for filings.Next() {
		value := filings.Value()
		if *format == "json" {
			buf, _ := json.Marshal(value)
			fmt.Println(string(buf))
		} else {
			tmpl.Execute(os.Stdout, value)
			fmt.Println()
		}
	}
	if filings.Err() != nil {
		fmt.Println("Issue iterating filings:", filings.Err())
	}
}