More work on the library

json tags for all structs
This commit is contained in:
Brian Buller 2019-04-08 16:25:31 -05:00
parent d56ccc469a
commit 432d44cbf5
14 changed files with 220 additions and 199 deletions

View File

@ -3,40 +3,30 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"git.bullercodeworks.com/brian/openstates" "git.bullercodeworks.com/brian/openstates"
"github.com/machinebox/graphql"
) )
func main() { func main() {
req := graphql.NewRequest(` //req := graphql.NewRequest(`query Reps($lat:Float,$long:Float){people(latitude:$lat,longitude:$long,first:100){edges{node{id name sortName familyName givenName image birthDate deathDate createdAt updatedAt extras links}}}}`)
query Reps($lat: Float, $long: Float) { //req.Var("lat", 37.700824)
people(latitude: $lat, longitude: $long, first: 100) { //req.Var("long")
edges {
node {
id
name
sortName
familyName
givenName
image
birthDate
deathDate
createdAt
updatedAt
extras
}
}
}
}
`)
req.Var("lat", 37.700824)
req.Var("long", -97.352906)
var res response var res response
o := openstates.NewOpenStatesApi(ApiKey) o := openstates.NewOpenStatesApi(ApiKey)
o.TurnOnLogging() o.SetLogger(func(s string) { log.Println(s) })
o.MakeRequest(req, &res) props := make(map[string]interface{})
props["lat"] = 37.700824
props["long"] = -97.352906
o.MakeRequest(&res, props)
//o.MakeRequest(req, &res)
//fmt.Println("= Fields =")
//for _, v := range res.People.Edges {
// v.Node.GetFields()
// break
//}
fmt.Println("= Result =")
j, _ := json.Marshal(res) j, _ := json.Marshal(res)
fmt.Println(string(j)) fmt.Println(string(j))
} }
@ -48,7 +38,7 @@ type people struct {
Edges []edge Edges []edge
} }
type edge struct { type edge struct {
Node node Node openstates.PersonNode
} }
type node struct { type node struct {
Name string Name string

View File

@ -1,117 +1,117 @@
package openstates package openstates
type BillNode struct { type BillNode struct {
Id string Id string `json:"id"`
LegislativeSession *LegislativeSessionNode LegislativeSession *LegislativeSessionNode `json:"legislativeSession"`
Identifier string Identifier string `json:"identifier"`
Title string Title string `json:"title"`
FromOrganization string FromOrganization string `json:"fromOrganization"`
Classification []string Classification []string `json:"classification"`
Subject []string Subject []string `json:"subject"`
Abstracts []BillAbstractNode Abstracts []BillAbstractNode `json:"abstracts"`
OtherTitles []BillTitleNode OtherTitles []BillTitleNode `json:"otherTitles"`
OtherIdentifiers []BillIdentifierNode OtherIdentifiers []BillIdentifierNode `json:"otherIdentifiers"`
Actions []BillActionNode Actions []BillActionNode `json:"actions"`
Sponsorships []BillSponsorshipNode Sponsorships []BillSponsorshipNode `json:"sponsorships"`
RelatedBills []RelatedBillNode RelatedBills []RelatedBillNode `json:"relatedBills"`
Versions []BillDocumentNode Versions []BillDocumentNode `json:"versions"`
Documents []BillDocumentNode Documents []BillDocumentNode `json:"documents"`
Votes []VoteEventNode Votes []VoteEventNode `json:"votes"`
Sources []LinkNode Sources []LinkNode `json:"sources"`
OpenStatesUrl string OpenStatesUrl string `json:"openStatesUrl"`
CreatedAt string CreatedAt string `json:"createdAt"`
UpdatedAt string UpdatedAt string `json:"updatedAt"`
Extras string Extras string `json:"extras"`
} }
type BillAbstractNode struct { type BillAbstractNode struct {
Abstract string Abstract string `json:"abstract"`
Note string Note string `json:"note"`
Date string Date string `json:"date"`
} }
type BillTitleNode struct { type BillTitleNode struct {
Title string Title string `json:"title"`
Note string Note string `json:"note"`
} }
type BillIdentifierNode struct { type BillIdentifierNode struct {
Identifier string Identifier string `json:"identifier"`
Scheme string Scheme string `json:"scheme"`
Note string Note string `json:"note"`
} }
type BillActionNode struct { type BillActionNode struct {
Organization *OrganizationNode Organization *OrganizationNode `json:"organization"`
Description string Description string `json:"description"`
Date string Date string `json:"date"`
Classification []ActionType Classification []ActionType `json:"classification"`
Order int Order int `json:"order"`
Extras string Extras string `json:"extras"`
Vote *VoteEventNode Vote *VoteEventNode `json:"vote"`
RelatedEntities []RelatedEntityNode RelatedEntities []RelatedEntityNode `json:"relatedEntities"`
} }
type RelatedEntityNode struct { type RelatedEntityNode struct {
Name string Name string `json:"name"`
EntityType EntityType EntityType EntityType `json:"entityType"`
Organization *OrganizationNode Organization *OrganizationNode `json:"organization"`
Person *PersonNode Person *PersonNode `json:"person"`
} }
type BillSponsorshipNode struct { type BillSponsorshipNode struct {
Name string Name string `json:"name"`
EntityType EntityType EntityType EntityType `json:"entityType"`
Organization *OrganizationNode Organization *OrganizationNode `json:"organization"`
Person *PersonNode Person *PersonNode `json:"person"`
Primary bool Primary bool `json:"primary"`
Classification string Classification string `json:"classification"`
} }
type RelatedBillNode struct { type RelatedBillNode struct {
Identifier string Identifier string `json:"identifier"`
LegislativeSession string LegislativeSession string `json:"legislativeSession"`
RelationType string RelationType string `json:"relationType"`
RelatedBill *BillNode RelatedBill *BillNode `json:"relatedBill"`
} }
type BillDocumentNode struct { type BillDocumentNode struct {
Note string Note string `json:"note"`
Date string Date string `json:"date"`
Links []MimetypeLinkNode Links []MimetypeLinkNode `json:"links"`
} }
type MimetypeLinkNode struct { type MimetypeLinkNode struct {
MediaType string MediaType string `json:"mediaType"`
Url string Url string `json:"url"`
Text string Text string `json:"text"`
} }
type VoteEventNode struct { type VoteEventNode struct {
Id string Id string `json:"id"`
Identifier string Identifier string `json:"identifier"`
MotionText string MotionText string `json:"motionText"`
MotionClassification []string MotionClassification []string `json:"motionClassification"`
StartDate string StartDate string `json:"startDate"`
Result string Result string `json:"result"`
Organization *OrganizationNode Organization *OrganizationNode `json:"organization"`
BillAction *BillActionNode BillAction *BillActionNode `json:"billAction"`
Votes []PersonVoteNode Votes []PersonVoteNode `json:"votes"`
Counts []VoteCountNode Counts []VoteCountNode `json:"counts"`
Sources []string Sources []string `json:"sources"`
CreatedAt string CreatedAt string `json:"createdAt"`
UpdatedAt string UpdatedAt string `json:"updatedAt"`
Extras string Extras string `json:"extras"`
} }
type PersonVoteNode struct { type PersonVoteNode struct {
Option string Option string `json:"option"`
VoterName string VoterName string `json:"voterName"`
Voter *PersonNode Voter *PersonNode `json:"voter"`
Note string Note string `json:"note"`
} }
type VoteCountNode struct { type VoteCountNode struct {
Option string Option string `json:"option"`
Value int Value int `json:"value"`
} }

View File

@ -1,8 +1,8 @@
package openstates package openstates
type ContactDetailNode struct { type ContactDetailNode struct {
Type string Type string `json:"type"`
Value string Value string `json:"value"`
Note string Note string `json:"note"`
Label string Label string `json:"label"`
} }

View File

@ -1,13 +1,13 @@
package openstates package openstates
type DivisionNode struct { type DivisionNode struct {
Id string Id string `json:"id"`
Name string Name string `json:"name"`
Redirect string Redirect string `json:"redirect"`
Country string Country string `json:"country"`
CreatedAt string CreatedAt string `json:"createdAt"`
UpdatedAt string UpdatedAt string `json:"updatedAt"`
Extras string Extras string `json:"extras"`
} }
const ( const (

View File

@ -1,6 +1,6 @@
package openstates package openstates
type IdentifierNode struct { type IdentifierNode struct {
Identifier string Identifier string `json:"identifier"`
Scheme string Scheme string `json:"scheme"`
} }

View File

@ -1,10 +1,10 @@
package openstates package openstates
type JurisdictionNode struct { type JurisdictionNode struct {
Id string Id string `json:"id"`
Name string Name string `json:"name"`
Url string Url string `json:"url"`
// FeatureFlags -- Reserved for future user // FeatureFlags -- Reserved for future user
LegislativeSessions []LegislativeSessionNode LegislativeSessions []LegislativeSessionNode `json:"legislativeSessions"`
Organizations []OrganizationNode Organizations []OrganizationNode `json:"organizations"`
} }

View File

@ -1,12 +1,12 @@
package openstates package openstates
type LegislativeSessionNode struct { type LegislativeSessionNode struct {
Jurisdiction *JurisdictionNode Jurisdiction *JurisdictionNode `json:"jurisdiction"`
Identifier string Identifier string `json:"identifier"`
Name string Name string `json:"name"`
Classification string Classification string `json:"classification"`
StartDate string StartDate string `json:"startDate"`
EndDate string EndDate string `json:"endDate"`
} }
const ( const (

View File

@ -1,6 +1,6 @@
package openstates package openstates
type LinkNode struct { type LinkNode struct {
Url string Url string `json:"url"`
Text string Text string `json:"text"`
} }

View File

@ -1,16 +1,16 @@
package openstates package openstates
type MembershipNode struct { type MembershipNode struct {
Id string Id string `json:"id"`
PersonName string PersonName string `json:"personName"`
Person *PersonNode Person *PersonNode `json:"person"`
Organization *OrganizationNode Organization *OrganizationNode `json:"organization"`
Post *PostNode Post *PostNode `json:"post"`
Label string Label string `json:"label"`
Role string Role string `json:"role"`
StartDate string StartDate string `json:"startDate"`
EndDate string EndDate string `json:"endDate"`
CreatedAt string CreatedAt string `json:"createdAt"`
UpdatedAt string UpdatedAt string `json:"updatedAt"`
Extras string Extras string `json:"extras"`
} }

View File

@ -1,8 +1,8 @@
package openstates package openstates
type NameNode struct { type NameNode struct {
Name string Name string `json:"name"`
Note string Note string `json:"note"`
StartDate string StartDate string `json:"startDate"`
EndDate string EndDate string `json:"endDate"`
} }

View File

@ -2,7 +2,7 @@ package openstates
import ( import (
"context" "context"
"log" "reflect"
"github.com/machinebox/graphql" "github.com/machinebox/graphql"
) )
@ -20,17 +20,48 @@ func NewOpenStatesApi(key string) *OpenStatesApi {
return &o return &o
} }
func (o *OpenStatesApi) TurnOnLogging() { func (o *OpenStatesApi) SetLogger(l func(s string)) {
o.Client.Log = func(s string) { log.Println(s) } o.Client.Log = l
} }
func (o *OpenStatesApi) MakeRequest(req *graphql.Request, res interface{}) { func (o *OpenStatesApi) MakeQueryRequest(query string, vars map[string]interface{}, res interface{}) error {
req := graphql.NewRequest(query)
for k, v := range vars {
req.Var(k, v)
}
return o.MakeRawRequest(req, &res)
}
func (o *OpenStatesApi) MakeRequest(res interface{}, vars map[string]interface{}) error {
req := o.buildRequest(res, vars)
return o.MakeRawRequest(req, res)
}
func (o *OpenStatesApi) MakeRawRequest(req *graphql.Request, res interface{}) error {
ctx := context.Background() ctx := context.Background()
req.Header.Add("X-API-KEY", o.ApiKey) req.Header.Add("X-API-KEY", o.ApiKey)
if err := o.Client.Run(ctx, req, &res); err != nil { return o.Client.Run(ctx, req, &res)
log.Fatal(err)
}
} }
//func (o *OpenStatesApi) GetLegislatorsForArea(lat float, long float) []PersonNode { func (o *OpenStatesApi) buildRequest(res interface{}, vars map[string]interface{}) *graphql.Request {
//} // TODO: Build query from res
req := graphql.NewRequest(`query Reps($lat:Float,$long:Float){people(latitude:$lat,longitude:$long,first:100){edges{node{id name sortName familyName givenName image birthDate deathDate createdAt updatedAt extras }}}}`)
for k, v := range vars {
req.Var(k, v)
}
return req
}
func GetFields(in interface{}) []string {
var ret []string
val := reflect.ValueOf(in)
for i := 0; i < val.Type().NumField(); i++ {
t := val.Type().Field(i)
fieldName := t.Name
if jsonTag := t.Tag.Get("json"); jsonTag != "" && jsonTag != "-" {
fieldName = jsonTag
}
ret = append(ret, fieldName)
}
return ret
}

View File

@ -1,20 +1,20 @@
package openstates package openstates
type OrganizationNode struct { type OrganizationNode struct {
Id string Id string `json:"id"`
Name string Name string `json:"name"`
Image string Image string `json:"image"`
Classification string Classification string `json:"classification"`
FoundingDate string FoundingDate string `json:"foundingDate"`
DissolutionDate string DissolutionDate string `json:"dissolutionDate"`
Parent *OrganizationNode Parent *OrganizationNode `json:"parent"`
Children []OrganizationNode Children []OrganizationNode `json:"children"`
CurrentMemberships []MembershipNode CurrentMemberships []MembershipNode `json:"currentMemberships"`
Identifiers []IdentifierNode Identifiers []IdentifierNode `json:"identifiers"`
OtherNames []NameNode OtherNames []NameNode `json:"otherNames"`
Links []LinkNode Links []LinkNode `json:"links"`
Sources []string Sources []string `json:"sources"`
CreatedAt string CreatedAt string `json:"createdAt"`
UpdatedAt string UpdatedAt string `json:"updatedAt"`
Extras string Extras string `json:"extras"`
} }

View File

@ -1,22 +1,22 @@
package openstates package openstates
type PersonNode struct { type PersonNode struct {
Id string Id string `json:"id"`
Name string Name string `json:"name"`
SortName string SortName string `json:"sortName"`
FamilyName string FamilyName string `json:"familyName"`
GivenName string GivenName string `json:"givenName"`
Image string Image string `json:"image"`
BirthDate string BirthDate string `json:"birthDate"`
DeathDate string DeathDate string `json:"deathDate"`
Identifiers []IdentifierNode Identifiers []IdentifierNode `json:"identifiers"`
OtherNames []NameNode OtherNames []NameNode `json:"otherNames"`
Links []LinkNode Links []LinkNode `json:"links"`
ContactDetails []ContactDetailNode ContactDetails []ContactDetailNode `json:"contactDetails"`
CurrentMemberships []MembershipNode CurrentMemberships []MembershipNode `json:"currentMemberships"`
OldMemberships []MembershipNode OldMemberships []MembershipNode `json:"oldMemberships"`
Sources []LinkNode Sources []LinkNode `json:"sources"`
CreatedAt string CreatedAt string `json:"createdAt"`
UpdatedAt string UpdatedAt string `json:"updatedAt"`
Extras string Extras string `json:"extras"`
} }

View File

@ -1,14 +1,14 @@
package openstates package openstates
type PostNode struct { type PostNode struct {
Id string Id string `json:"id"`
Label string Label string `json:"label"`
Role string Role string `json:"role"`
Division *DivisionNode Division *DivisionNode `json:"division"`
StartDate string StartDate string `json:"startDate"`
EndDate string EndDate string `json:"endDate"`
MaximumMberships int MaximumMemberships int `json:"maximumMemberships"`
CreatedAt string CreatedAt string `json:"createdAt"`
UpdatedAt string UpdatedAt string `json:"updatedAt"`
Extras string Extras string `json:"extras"`
} }