Skip to content

A Go client for the OpenSecrets federal campaign finance REST API

License

Notifications You must be signed in to change notification settings

KiaFarhang/opensecrets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenSecrets API Go Client

Go Report Card

This is a Go client for the OpenSecrets campaign finance API. It handles HTTP calls and response marshalling, so you can simply call a method and get a struct representing the OpenSecrets response back.

Installation

go get github.com/KiaFarhang/opensecrets

Usage

Constructing a client

First, instantiate an OpenSecretsClient by passing your API key to the constructor function:

package whatever

import (
	"github.com/KiaFarhang/opensecrets/pkg/client"
	"github.com/KiaFarhang/opensecrets/pkg/models"
)

openSecretsClient := client.NewOpenSecretsClient("YOUR_API_KEY")

If you'd like to customize the HTTP client the library uses, call the NewOpenSecretsClientWithHttpClient constructor instead:

httpClient := &http.Client{Timeout: time.Second * 3} // Whatever other configuration you want here...

openSecretsClient := client.NewOpenSecretsClientWithHttpClient("YOUR_API_KEY", httpClient)

The custom HTTP client can be anything that satisfies the following interface:

type OpenSecretsHttpClient interface {
	Do(req *http.Request) (*http.Response, error)
}

The default client has a 5-second HTTP timeout.

The OpenSecretsClient is thread safe; you should construct one and share it throughout your application.

Making API calls

The client has public methods for each of the OpenSecrets API methods. To call one, just pass it a context.Contexrt and the appropriate request object from the models package:

request := models.LegislatorsRequest{Id: "TX"}
legislators, err := client.GetLegislators(context.Background(), request)

The client will either return a struct containing the data from the API call or an error if something went wrong.

The client throws an error if you pass it a request that's missing a required parameter. Required parameters are the same as those noted in the docs for each method, listed in the table below. (Each request struct also includes comments noting the required and optional fields)

Note you never need to pass the apikey or output arguments to the client. It sends the API key passed at construction with every request, and it always requests output in JSON so it can marshal that response into the struct each method returns.

For a full example of each API call, see the end-to-end tests at pkg/client/client_end_to_end_test.go. You can run them locally by pulling down this repo and using the following command from its root directory:

API_KEY=your_key_here go test ./...

Available methods

API method Client method Description Docs
getLegislators GetLegislators Provides a list of Congressional legislators for a specified subset (state or specific CID) Link
memPFDProfile GetMemberPFDProfile Returns data on the personal finances of a member of Congress, as well as judicial + executive branches Link
candSummary GetCandidateSummary Provides summary fundraising information for a politician Link
candContrib GetCandidateContributors Returns top contributors to a candidate for/sitting member of Congress Link
candIndustry GetCandidateIndustries Provides the top 10 industries contributing to a candidate Link
candIndByInd GetCandidateIndustryDetails Provides total contributed to a candidate from an industry. Link
candSector GetCandidateTopSectorDetails Provides sector total of a candidate's receipts Link
congCmteIndus GetCommitteeFundraisingDetails Provides fundraising details for all members of a given committee from the provided industry Link
getOrgs SearchForOrganization Searches for an organization by name or partial name Link
orgSummary GetOrganizationSummary Provides summary fundraising information for an organization Link
independentExpend GetLatestIndependentExpenditures Get the latest 50 independent expenditures reported. Link

Development

Run unit tests with go test -short ./...

Run unit and end-to-end tests with API_KEY=your_key_here go test ./...

About

A Go client for the OpenSecrets federal campaign finance REST API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages