From 6fdf4977030a657b5ae23d653a80b678cc7d9229 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 16 Jun 2016 12:26:36 -0500 Subject: [PATCH] All legislator calls complete --- example/example.go | 5 ++- openstates/legislator_structs.go | 59 ++++++++++++++++++++++++- openstates/legislators.go | 74 +++++++++++++++++++++++++++++++- openstates/openstates.go | 22 ++-------- 4 files changed, 137 insertions(+), 23 deletions(-) diff --git a/example/example.go b/example/example.go index 4ec355e..8614b5a 100644 --- a/example/example.go +++ b/example/example.go @@ -17,8 +17,11 @@ func main() { } o := openstates.Create(apiKey) //d, err := o.StateMetadata(states.Kansas) - d, err := o.GetBillDetailFromID("KSB00002165") + //d, err := o.GetBillDetailFromID("KSB00002165") //d, err := o.GetBillDetail(states.Kansas, "2013-2014", "HR 6020") + //d, err := o.GetLegislatorsForState(states.Kansas) + //d, err := o.GetLegislatorDetail("KSL000018") + d, err := o.GetLegislatorsForGeo(35.79, -78.78) if err != nil { fmt.Println(err.Error()) return diff --git a/openstates/legislator_structs.go b/openstates/legislator_structs.go index ad6e474..8a2c74b 100644 --- a/openstates/legislator_structs.go +++ b/openstates/legislator_structs.go @@ -1,7 +1,64 @@ package openstates +import "time" + // Legislator is a legislator type Legislator struct { - LegID string `json:"leg_id"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + MiddleName string `json:"middle_name"` + District string `json:"district"` + Chamber string `json:"chamber"` + URL string `json:"url"` + CreatedAtStr string `json:"created_at"` + CreatedAt time.Time `json:"-"` + UpdatedAtStr string `json:"updated_at"` + UpdatedAt time.Time `json:"-"` + Email string `json:"email"` + Active bool `json:"active"` + State string `json:"state"` + Offices []Office `json:"offices"` + OfficeAddress string `json:"office_address"` + VotesmartID string `json:"votesmart_id"` + FullName string `json:"full_name"` + LegID string `json:"leg_id"` + Party string `json:"party"` + Suffixes string `json:"suffixes"` + ID string `json:"id"` + PhotoURL string `json:"photo_url"` + Fax string `json:"+fax"` + Level string `json:"level"` + Phone string `json:"+phone"` + OldRoles map[string][]Role `json:"old_roles"` + Roles []Role `json:"roles"` + NameOnBill string `json:"name"` // Shows up when pulling bill detail } + +// Office is a legislators office +type Office struct { + Fax string `json:"fax"` + Name string `json:"name"` + Phone string `json:"phone"` + Address string `json:"address"` + Type string `json:"type"` + Email string `json:"email"` +} + +// Role is a legislators role +type Role struct { + Term string `json:"term"` + StartDateStr string `json:"start_date"` + StartDate time.Time `json:"-"` + EndDateStr string `json:"end_date"` + EndDate time.Time `json:"-"` + District string `json:"district"` + Chamber string `json:"chamber"` + State string `json:"state"` + Party string `json:"party"` + Type string `json:"type"` + CommitteeID string `json:"committee_id"` + SubCommittee string `json:"subcommittee"` + Committee string `json:"committee"` + Position string `json:"position"` +} diff --git a/openstates/legislators.go b/openstates/legislators.go index 3f9f093..370ef01 100644 --- a/openstates/legislators.go +++ b/openstates/legislators.go @@ -2,7 +2,11 @@ package openstates import ( "encoding/json" + "errors" "net/url" + "strconv" + + "github.com/br0xen/sunlight-api/openstates/states" ) // SearchLegislators retrieves a list of legislators based on a number of @@ -27,8 +31,74 @@ func (o *OpenStates) SearchLegislators(v url.Values) ([]Legislator, error) { } err = json.Unmarshal(getVal, &ret) if err == nil { - //for i := range ret { - //} + for i := range ret { + o.fixLegislatorTimes(&ret[i]) + } } return ret, err } + +// GetLegislatorsForState returns all legislators for a specific state +func (o *OpenStates) GetLegislatorsForState(st string) ([]Legislator, error) { + st, err := states.ScrubToAbbr(st) + if err != nil { + return []Legislator{}, err + } + v := url.Values{} + v.Set("state", st) + return o.SearchLegislators(v) +} + +// GetLegislatorDetail returns the detail for a legislator +func (o *OpenStates) GetLegislatorDetail(id string) (*Legislator, error) { + var ret *Legislator + var err error + var getVal []byte + v := url.Values{} + if getVal, err = o.call("legislators/"+id, v); err != nil { + return ret, err + } + err = json.Unmarshal(getVal, &ret) + o.fixLegislatorTimes(ret) + return ret, err +} + +// GetLegislatorsForGeo return all legislators for a latitude & longitude +func (o *OpenStates) GetLegislatorsForGeo(lat, lng float64) ([]Legislator, error) { + var ret []Legislator + var err error + var getVal []byte + v := url.Values{} + v.Set("lat", strconv.FormatFloat(lat, 'f', -1, 64)) + v.Set("long", strconv.FormatFloat(lng, 'f', -1, 64)) + if getVal, err = o.call("legislators/geo/", v); err != nil { + return ret, err + } + err = json.Unmarshal(getVal, &ret) + if err == nil { + for i := range ret { + o.fixLegislatorTimes(&ret[i]) + } + } + return ret, err +} + +func (o *OpenStates) fixLegislatorTimes(l *Legislator) error { + if err := UnmarshalTimeString(l.CreatedAtStr, &l.CreatedAt); err != nil { + return errors.New("No Created At Time") + } + if err := UnmarshalTimeString(l.UpdatedAtStr, &l.UpdatedAt); err != nil { + return errors.New("No Updated At Time") + } + for i := range l.Roles { + UnmarshalTimeString(l.Roles[i].EndDateStr, &l.Roles[i].EndDate) + UnmarshalTimeString(l.Roles[i].StartDateStr, &l.Roles[i].StartDate) + } + for k := range l.OldRoles { + for i := range l.OldRoles[k] { + UnmarshalTimeString(l.OldRoles[k][i].EndDateStr, &l.OldRoles[k][i].EndDate) + UnmarshalTimeString(l.OldRoles[k][i].StartDateStr, &l.OldRoles[k][i].StartDate) + } + } + return nil +} diff --git a/openstates/openstates.go b/openstates/openstates.go index 1561ae5..905fbe1 100644 --- a/openstates/openstates.go +++ b/openstates/openstates.go @@ -56,12 +56,9 @@ func (o *OpenStates) AllMetadata() ([]StateMeta, error) { func (o *OpenStates) StateMetadata(st string) (*StateMeta, error) { var ret *StateMeta var err error - orig := st - if states.IsValidName(st) { - st, err = states.NameToAbbr(st) - } - if !states.IsValidAbbr(st) { - return ret, errors.New("Invalid State: " + orig) + st, err = states.ScrubToAbbr(st) + if err != nil { + return ret, err } var getVal []byte vals := url.Values{} @@ -84,16 +81,3 @@ func UnmarshalTimeString(s string, t *time.Time) error { *t, err = time.Parse("2006-01-02 15:04:05", s) return err } - -/* - resp, err := http.Get("http://openstates.org/api/v1/legislators/geo/?apikey=" + site.OpenStatesKey + "&lat=" + lat + "&long=" + lng) - if err != nil { - fmt.Fprint(w, "{\"status\":\"error\"}") - return - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - fmt.Fprint(w, string(body)) - // http://openstates.org/api/v1/legislators/geo/?lat=35.79&long=-78.78 - // https://sunlightlabs.github.io/openstates-api/legislators.html#examples/geo-lookup -*/