Browse Source

Begin work on generate-structs for flexible assets

master
Steven Polley 3 years ago
parent
commit
9a9079e42d
  1. 60
      generate-structs/main.go
  2. 19
      itglue/flexible-assets.go

60
generate-structs/main.go

@ -0,0 +1,60 @@
package main
import (
"bytes"
"fmt"
"log"
"os"
"strconv"
"deadbeef.codes/steven/go-itg/itglue"
"github.com/ChimeraCoder/gojson"
)
var itg *itglue.ITGAPI
func init() {
apiKey := os.Getenv("itgapikey")
if apiKey == "" {
log.Fatalf("API key is not set")
}
itg = itglue.NewITGAPI(apiKey)
}
func main() {
fats, err := itg.GetFlexibleAssetTypes()
if err != nil {
log.Fatalf("could get get FATs: %s", err)
}
for _, fat := range fats.Data {
fmt.Printf("%s - %s\n", fat.FlexibleAssetTypeData.ID, fat.FlexibleAssetTypeData.Attributes.Name)
id, err := strconv.Atoi(fat.ID)
if err != nil {
log.Fatalf("could not convert %s to integer: %s", fat.ID, err)
}
fa, err := itg.GetFlexibleAssetsJSON(id)
if err != nil {
log.Fatalf("could get flexible asset with type ID %d: %s", id, err)
}
name := &fat.FlexibleAssetTypeData.Attributes.Name
pkg := "itglue"
subStruct := false // try changing to true?
tagList := make([]string, 0)
tagList = append(tagList, "json")
var convertFloats bool
var parser gojson.Parser
parser = gojson.ParseJson
convertFloats = true
input := bytes.NewReader(fa)
output, err := gojson.Generate(input, parser, *name, pkg, tagList, subStruct, convertFloats)
fmt.Print(string(output))
}
}

19
itglue/flexible-assets.go

@ -32,25 +32,16 @@ type FlexibleAssetList struct {
Links struct{ Links } `json:"links"`
}
//Rather than a "GetFlexibleAssets", we need to implement getters for each flexible asset type. This should probably be automated and go code should be generated by some external program.
func (itg *ITGAPI) GetFlexibleAssets(flexibleAssetTypeID int) error {
//GetFlexibleAssetsJSON is a special function. All flexible assets will return different data depending
//on which fields make up the flexible asset. Because of this, this function simply returns the JSON bytes.
func (itg *ITGAPI) GetFlexibleAssetsJSON(flexibleAssetTypeID int) ([]byte, error) {
req := itg.NewRequest("/flexible_assets", "GET", nil)
req.RawURLValues = fmt.Sprintf("filter[flexible_asset_type_id]=%d", flexibleAssetTypeID)
err := req.Do()
if err != nil {
return fmt.Errorf("request failed for %s: %s", req.RestAction, err)
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
fmt.Println(string(req.Body))
/*
flexibleAssets := &FlexibleAssetList{}
err = json.Unmarshal(req.Body, flexibleAssets)
if err != nil {
return nil, fmt.Errorf("could not get flexible asset: %s", err)
}
return flexibleAssets, nil
*/
return nil
return req.Body, nil
}

Loading…
Cancel
Save