Begin work on generate-structs for flexible assets
This commit is contained in:
parent
c2d7d160a3
commit
9a9079e42d
60
generate-structs/main.go
Normal file
60
generate-structs/main.go
Normal file
@ -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))
|
||||
|
||||
}
|
||||
}
|
@ -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…
x
Reference in New Issue
Block a user