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 (
"encoding/json"
"fmt"
"log"
"git.bullercodeworks.com/brian/openstates"
"github.com/machinebox/graphql"
)
func main() {
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
}
}
}
}
`)
req.Var("lat", 37.700824)
req.Var("long", -97.352906)
//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}}}}`)
//req.Var("lat", 37.700824)
//req.Var("long")
var res response
o := openstates.NewOpenStatesApi(ApiKey)
o.TurnOnLogging()
o.MakeRequest(req, &res)
o.SetLogger(func(s string) { log.Println(s) })
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)
fmt.Println(string(j))
}
@ -48,7 +38,7 @@ type people struct {
Edges []edge
}
type edge struct {
Node node
Node openstates.PersonNode
}
type node struct {
Name string

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,7 +2,7 @@ package openstates
import (
"context"
"log"
"reflect"
"github.com/machinebox/graphql"
)
@ -20,17 +20,48 @@ func NewOpenStatesApi(key string) *OpenStatesApi {
return &o
}
func (o *OpenStatesApi) TurnOnLogging() {
o.Client.Log = func(s string) { log.Println(s) }
func (o *OpenStatesApi) SetLogger(l func(s string)) {
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()
req.Header.Add("X-API-KEY", o.ApiKey)
if err := o.Client.Run(ctx, req, &res); err != nil {
log.Fatal(err)
}
return o.Client.Run(ctx, req, &res)
}
//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
type OrganizationNode struct {
Id string
Name string
Image string
Classification string
FoundingDate string
DissolutionDate string
Parent *OrganizationNode
Children []OrganizationNode
CurrentMemberships []MembershipNode
Identifiers []IdentifierNode
OtherNames []NameNode
Links []LinkNode
Sources []string
CreatedAt string
UpdatedAt string
Extras string
Id string `json:"id"`
Name string `json:"name"`
Image string `json:"image"`
Classification string `json:"classification"`
FoundingDate string `json:"foundingDate"`
DissolutionDate string `json:"dissolutionDate"`
Parent *OrganizationNode `json:"parent"`
Children []OrganizationNode `json:"children"`
CurrentMemberships []MembershipNode `json:"currentMemberships"`
Identifiers []IdentifierNode `json:"identifiers"`
OtherNames []NameNode `json:"otherNames"`
Links []LinkNode `json:"links"`
Sources []string `json:"sources"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Extras string `json:"extras"`
}

View File

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

View File

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