Example #1
0
func (this *Base) Populate(ctx context.Context, row db.EffectRecord) {
	this.ID = row.ID()
	this.PT = row.PagingToken()
	this.Account = row.Account
	this.populateType(row)

	lb := hal.LinkBuilder{httpx.BaseURL(ctx)}
	this.Links.Operation = lb.Linkf("/operations/%d", row.HistoryOperationID)
	this.Links.Succeeds = lb.Linkf("/effects?order=desc&cursor=%s", this.PT)
	this.Links.Precedes = lb.Linkf("/effects?order=asc&cursor=%s", this.PT)
}
Example #2
0
// NewEffectResource initializes a new resource from an EffectRecord
func NewEffectResource(op db.EffectRecord) (EffectResource, error) {
	result, err := op.Details()

	if err != nil {
		return nil, err
	}

	if result == nil {
		result = make(map[string]interface{})
	}

	result["_links"] = halgo.Links{}.
		Link("precedes", "/effects?cursor=%s&order=asc", op.PagingToken()).
		Link("succeeds", "/effects?cursor=%s&order=desc", op.PagingToken()).
		Link("operation", "/operations/%d", op.HistoryOperationID).
		Items
	result["paging_token"] = op.PagingToken()
	result["type_i"] = op.Type
	result["account"] = op.Account

	ts, ok := effectResourceTypeNames[op.Type]

	if ok {
		result["type"] = ts
	} else {
		//TODO: log a warning when we encounter this... it implies our code is out of date
		result["type"] = "unknown"
	}

	return result, nil
}
Example #3
0
// Populate fills out the details
func (res *Trade) Populate(ctx context.Context, row db.EffectRecord) (err error) {
	if row.Type != db.EffectTrade {
		err = errors.New("invalid effect; not a trade")
		return
	}
	row.UnmarshalDetails(res)
	res.ID = row.PagingToken()
	res.PT = row.PagingToken()
	res.Buyer = row.Account

	lb := hal.LinkBuilder{httpx.BaseURL(ctx)}
	res.Links.Self = lb.Link("/accounts", res.Seller)
	res.Links.Seller = lb.Link("/accounts", res.Seller)
	res.Links.Buyer = lb.Link("/accounts", res.Buyer)
	return
}
Example #4
0
// NewTradeResource initializes a new resource from an EffectRecord
func NewTradeResource(r db.EffectRecord) (result TradeResource, err error) {

	if r.Type != db.EffectTrade {
		err = ErrInvalidTrade
		return
	}

	var details map[string]interface{}
	details, err = r.Details()

	if err != nil {
		return
	}

	seller, ok := details["seller"].(string)
	if !ok {
		err = ErrInvalidTrade
		return
	}

	result = TradeResource{
		Links: halgo.Links{}.
			Link("seller", "/accounts/%s", seller).
			Link("buyer", "/accounts/%s", r.Account).
			Link("order_book", "/order_book?TODO"),
		ID:                r.PagingToken(),
		PagingToken:       r.PagingToken(),
		Seller:            seller,
		SoldAssetType:     details["sold_asset_type"],
		SoldAssetCode:     details["sold_asset_code"],
		SoldAssetIssuer:   details["sold_asset_issuer"],
		Buyer:             r.Account,
		BoughtAssetType:   details["bought_asset_type"],
		BoughtAssetCode:   details["bought_asset_code"],
		BoughtAssetIssuer: details["bought_asset_issuer"],
	}

	return
}