diff --git a/go/demo_loyalty.go b/go/demo_loyalty.go index c641e85..14e7d3a 100644 --- a/go/demo_loyalty.go +++ b/go/demo_loyalty.go @@ -41,16 +41,14 @@ import ( const ( batchUrl = "https://walletobjects.googleapis.com/batch" - classUrl = "https://walletobjects.googleapis.com/walletobjects/v1/loyaltyClass" - objectUrl = "https://walletobjects.googleapis.com/walletobjects/v1/loyaltyObject" ) // [END setup] type demoLoyalty struct { - credentials *oauthJwt.Config - service *walletobjects.Service - batchUrl, classUrl, objectUrl string + credentials *oauthJwt.Config + service *walletobjects.Service + batchUrl string } // [START auth] @@ -92,9 +90,9 @@ func (d *demoLoyalty) createClass(issuerId, classSuffix string) { // [END createClass] -// [START createObject] -// Create an object. -func (d *demoLoyalty) createObject(issuerId, classSuffix, objectSuffix string) { +// [START setupObject] +// Build a full loyaltyObject. +func (d *demoLoyalty) setupObject(issuerId, classSuffix, objectSuffix string) *walletobjects.LoyaltyObject { loyaltyObject := new(walletobjects.LoyaltyObject) loyaltyObject.Id = fmt.Sprintf("%s.%s", issuerId, objectSuffix) loyaltyObject.ClassId = fmt.Sprintf("%s.%s", issuerId, classSuffix) @@ -151,6 +149,14 @@ func (d *demoLoyalty) createObject(issuerId, classSuffix, objectSuffix string) { Id: "TEXT_MODULE_ID", }, } + return loyaltyObject +} +// [END setupObject] + +// [START createObject] +// Create an object. +func (d *demoLoyalty) createObject(issuerId, classSuffix, objectSuffix string) { + loyaltyObject := d.setupObject(issuerId, classSuffix, objectSuffix) res, err := d.service.Loyaltyobject.Insert(loyaltyObject).Do() if err != nil { log.Fatalf("Unable to insert object: %v", err) @@ -261,80 +267,10 @@ func (d *demoLoyalty) batchCreateObjects(issuerId, classSuffix string) { data := "" for i := 0; i < 3; i++ { objectSuffix := strings.ReplaceAll(uuid.New().String(), "-", "_") - - batchObject := fmt.Sprintf(` - { - "classId": "%s.%s", - "heroImage": { - "contentDescription": { - "defaultValue": { - "value": "Hero image description", - "language": "en-US" - } - }, - "sourceUri": { - "uri": "https://farm4.staticflickr.com/3723/11177041115_6e6a3b6f49_o.jpg" - } - }, - "barcode": { - "type": "QR_CODE", - "value": "QR code" - }, - "locations": [ - { - "latitude": 37.424015499999996, - "longitude": -122.09259560000001 - } - ], - "accountName": "Account name", - "state": "ACTIVE", - "linksModuleData": { - "uris": [ - { - "id": "LINK_MODULE_URI_ID", - "uri": "http://maps.google.com/", - "description": "Link module URI description" - }, - { - "id": "LINK_MODULE_TEL_ID", - "uri": "tel:6505555555", - "description": "Link module tel description" - } - ] - }, - "imageModulesData": [ - { - "id": "IMAGE_MODULE_ID", - "mainImage": { - "contentDescription": { - "defaultValue": { - "value": "Image module description", - "language": "en-US" - } - }, - "sourceUri": { - "uri": "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg" - } - } - } - ], - "textModulesData": [ - { - "body": "Text module body", - "header": "Text module header", - "id": "TEXT_MODULE_ID" - } - ], - "id": "%s.%s", - "loyaltyPoints": { - "balance": { - "int": 800 - }, - "label": "Points" - }, - "accountId": "Account id" - } - `, issuerId, classSuffix, issuerId, objectSuffix) + loyaltyObject := d.setupObject(issuerId, classSuffix, objectSuffix) + loyaltyJson, _ := json.Marshal(loyaltyObject) + + batchObject := fmt.Sprintf("%s", loyaltyJson) data += "--batch_createobjectbatch\n" data += "Content-Type: application/json\n\n"