Files
legislature-tracker/backend/Leg/utah.go

74 lines
2.5 KiB
Go

package Leg
import (
"encoding/json"
"fmt"
"os"
"time"
"git.sa.vin/legislature-tracker/backend/cachedAPI"
"git.sa.vin/legislature-tracker/backend/types"
)
type UtahLeg interface {
GetBillList(year, session string) (types.UtahBillList, error)
GetBillDetails(year, session, billID string) (types.UtahBill, error)
}
type utahLeg struct {
cache cachedAPI.CachedAPI
}
var developerToken string
func NewUtahLeg(cache cachedAPI.CachedAPI) UtahLeg {
developerToken = os.Getenv("UTAH_DEV_TOKEN")
return &utahLeg{
cache: cache,
}
}
// GetBillList gets the list of bills for a given year and session,
// session should be one of "GS", "S#" where # is the session number
func (u utahLeg) GetBillList(year, session string) (types.UtahBillList, error) {
// if session is not GS it must start with S and end with a number
if session != "GS" && (session[0] != 'S' || session[1] < '0' || session[1] > '9') {
return types.UtahBillList{}, fmt.Errorf("session must be one of GS or S with some number")
}
respString, err := u.cache.Get(fmt.Sprintf("https://glen.le.utah.gov/bills/%v%v/billlist/%v", year, session, developerToken), time.Hour)
if err != nil {
return types.UtahBillList{}, fmt.Errorf("error getting bill list: %w", err)
}
if respString == "Invalid request" {
return types.UtahBillList{}, fmt.Errorf("invalid request")
}
var billList types.UtahBillList
err = json.Unmarshal([]byte(respString), &billList)
if err != nil {
return types.UtahBillList{}, fmt.Errorf("error unmarshalling bill list: %w", err)
}
return billList, nil
}
// GetBillDetails gets the details of a bill for a given year, session, and billID
// session should be one of "GS", "S2"
func (u utahLeg) GetBillDetails(year, session, billID string) (types.UtahBill, error) {
// if session is not GS it must start with S and end with a number
if session != "GS" && (session[0] != 'S' || session[1] < '0' || session[1] > '9') {
return types.UtahBill{}, fmt.Errorf("session must be one of GS or S with some number")
}
respString, err := u.cache.Get(fmt.Sprintf("https://glen.le.utah.gov/bills/%v%v/%v/%v", year, session, billID, developerToken), time.Hour)
if err != nil {
return types.UtahBill{}, fmt.Errorf("error getting bill details: %w", err)
}
if respString == "Invalid request" {
return types.UtahBill{}, fmt.Errorf("invalid request")
}
var bill types.UtahBill
err = json.Unmarshal([]byte(respString), &bill)
if err != nil {
return types.UtahBill{}, fmt.Errorf("error unmarshalling bill details: %w", err)
}
return bill, nil
}