Use installable Google APIs lib for nodejs

This commit is contained in:
Stephen McDonald
2024-02-20 12:27:00 +11:00
parent 3e49ff1085
commit f47706fcab
9 changed files with 421 additions and 511 deletions

View File

@@ -16,7 +16,7 @@
// [START setup]
// [START imports]
const { GoogleAuth } = require('google-auth-library');
const { google } = require('googleapis');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
// [END imports]
@@ -31,12 +31,6 @@ class DemoEventTicket {
* variable: GOOGLE_APPLICATION_CREDENTIALS.
*/
this.keyFilePath = process.env.GOOGLE_APPLICATION_CREDENTIALS || '/path/to/key.json';
this.baseUrl = 'https://walletobjects.googleapis.com/walletobjects/v1';
this.batchUrl = 'https://walletobjects.googleapis.com/batch';
this.classUrl = `${this.baseUrl}/eventTicketClass`;
this.objectUrl = `${this.baseUrl}/eventTicketObject`;
this.auth();
}
// [END setup]
@@ -46,11 +40,16 @@ class DemoEventTicket {
* Create authenticated HTTP client using a service account file.
*/
auth() {
const auth = new google.auth.GoogleAuth({
keyFile: this.keyFilePath,
scopes: ['https://www.googleapis.com/auth/wallet_object.issuer'],
});
this.credentials = require(this.keyFilePath);
this.httpClient = new GoogleAuth({
credentials: this.credentials,
scopes: 'https://www.googleapis.com/auth/wallet_object.issuer'
this.client = google.walletobjects({
version: 'v1',
auth: auth,
});
}
// [END auth]
@@ -69,9 +68,8 @@ class DemoEventTicket {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.eventticketclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
console.log(`Class ${issuerId}.${classSuffix} already exists!`);
@@ -100,10 +98,8 @@ class DemoEventTicket {
'reviewStatus': 'UNDER_REVIEW'
};
response = await this.httpClient.request({
url: this.classUrl,
method: 'POST',
data: newClass
response = await this.client.eventticketclass.insert({
requestBody: newClass
});
console.log('Class insert response');
@@ -129,9 +125,8 @@ class DemoEventTicket {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.eventticketclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -156,10 +151,9 @@ class DemoEventTicket {
// Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates
updatedClass['reviewStatus'] = 'UNDER_REVIEW';
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PUT',
data: updatedClass
response = await this.client.eventticketclass.update({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: updatedClass
});
console.log('Class update response');
@@ -185,9 +179,8 @@ class DemoEventTicket {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.eventticketclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -211,10 +204,9 @@ class DemoEventTicket {
'reviewStatus': 'UNDER_REVIEW'
};
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.eventticketclass.patch({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: patchBody
});
console.log('Class patch response');
@@ -240,9 +232,8 @@ class DemoEventTicket {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.eventticketclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -255,10 +246,9 @@ class DemoEventTicket {
}
}
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.eventticketclass.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -288,9 +278,8 @@ class DemoEventTicket {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.eventticketobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
console.log(`Object ${issuerId}.${objectSuffix} already exists!`);
@@ -398,10 +387,8 @@ class DemoEventTicket {
'ticketNumber': 'Ticket number'
};
response = await this.httpClient.request({
url: this.objectUrl,
method: 'POST',
data: newObject
response = await this.client.eventticketobject.insert({
requestBody: newObject
});
console.log('Object insert response');
@@ -427,9 +414,8 @@ class DemoEventTicket {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.eventticketobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -458,10 +444,9 @@ class DemoEventTicket {
updatedObject['linksModuleData']['uris'].push(newLink);
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PUT',
data: updatedObject
response = await this.client.eventticketobject.update({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: updatedObject
});
console.log('Object update response');
@@ -485,9 +470,8 @@ class DemoEventTicket {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.eventticketobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -521,10 +505,9 @@ class DemoEventTicket {
}
patchBody['linksModuleData']['uris'].push(newLink);
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.eventticketobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object patch response');
@@ -551,9 +534,8 @@ class DemoEventTicket {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.eventticketobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -571,10 +553,9 @@ class DemoEventTicket {
'state': 'EXPIRED'
};
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.eventticketobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object expiration response');
@@ -600,7 +581,7 @@ class DemoEventTicket {
// Check if the object exists
try {
response = await this.httpClient.request({
response = await this.client.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
});
@@ -615,10 +596,9 @@ class DemoEventTicket {
}
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.eventticketclass.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -989,8 +969,8 @@ class DemoEventTicket {
data += '--batch_createobjectbatch--';
// Invoke the batch API calls
let response = await this.httpClient.request({
url: this.batchUrl, // https://walletobjects.googleapis.com/batch
let response = await this.client.context._options.auth.request({
url: 'https://walletobjects.googleapis.com/batch',
method: 'POST',
data: data,
headers: {

View File

@@ -16,7 +16,7 @@
// [START setup]
// [START imports]
const { GoogleAuth } = require('google-auth-library');
const { google } = require('googleapis');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
// [END imports]
@@ -31,12 +31,6 @@ class DemoFlight {
* variable: GOOGLE_APPLICATION_CREDENTIALS.
*/
this.keyFilePath = process.env.GOOGLE_APPLICATION_CREDENTIALS || '/path/to/key.json';
this.baseUrl = 'https://walletobjects.googleapis.com/walletobjects/v1';
this.batchUrl = 'https://walletobjects.googleapis.com/batch';
this.classUrl = `${this.baseUrl}/flightClass`;
this.objectUrl = `${this.baseUrl}/flightObject`;
this.auth();
}
// [END setup]
@@ -46,11 +40,16 @@ class DemoFlight {
* Create authenticated HTTP client using a service account file.
*/
auth() {
const auth = new google.auth.GoogleAuth({
keyFile: this.keyFilePath,
scopes: ['https://www.googleapis.com/auth/wallet_object.issuer'],
});
this.credentials = require(this.keyFilePath);
this.httpClient = new GoogleAuth({
credentials: this.credentials,
scopes: 'https://www.googleapis.com/auth/wallet_object.issuer'
this.client = google.walletobjects({
version: 'v1',
auth: auth,
});
}
// [END auth]
@@ -69,9 +68,8 @@ class DemoFlight {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.flightclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
console.log(`Class ${issuerId}.${classSuffix} already exists!`);
@@ -110,10 +108,8 @@ class DemoFlight {
}
};
response = await this.httpClient.request({
url: this.classUrl,
method: 'POST',
data: newClass
response = await this.client.flightclass.insert({
requestBody: newClass
});
console.log('Class insert response');
@@ -139,9 +135,8 @@ class DemoFlight {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.flightclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -166,10 +161,9 @@ class DemoFlight {
// Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates
updatedClass['reviewStatus'] = 'UNDER_REVIEW';
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PUT',
data: updatedClass
response = await this.client.flightclass.update({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: updatedClass
});
console.log('Class update response');
@@ -195,9 +189,8 @@ class DemoFlight {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.flightclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -221,10 +214,9 @@ class DemoFlight {
'reviewStatus': 'UNDER_REVIEW'
};
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.flightclass.patch({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: patchBody
});
console.log('Class patch response');
@@ -250,9 +242,8 @@ class DemoFlight {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.flightclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -265,10 +256,9 @@ class DemoFlight {
}
}
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.flightclass.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -298,9 +288,8 @@ class DemoFlight {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.flightobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
console.log(`Object ${issuerId}.${objectSuffix} already exists!`);
@@ -388,10 +377,8 @@ class DemoFlight {
}
};
response = await this.httpClient.request({
url: this.objectUrl,
method: 'POST',
data: newObject
response = await this.client.flightobject.insert({
requestBody: newObject
});
console.log('Object insert response');
@@ -417,9 +404,8 @@ class DemoFlight {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.flightobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -448,10 +434,9 @@ class DemoFlight {
updatedObject['linksModuleData']['uris'].push(newLink);
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PUT',
data: updatedObject
response = await this.client.flightobject.update({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: updatedObject
});
console.log('Object update response');
@@ -475,9 +460,8 @@ class DemoFlight {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.flightobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -511,10 +495,9 @@ class DemoFlight {
}
patchBody['linksModuleData']['uris'].push(newLink);
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.flightobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object patch response');
@@ -541,9 +524,8 @@ class DemoFlight {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.flightobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -561,10 +543,9 @@ class DemoFlight {
'state': 'EXPIRED'
};
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.flightobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object expiration response');
@@ -590,9 +571,8 @@ class DemoFlight {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.flightobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -605,10 +585,9 @@ class DemoFlight {
}
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.flightobject.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -950,8 +929,8 @@ class DemoFlight {
data += '--batch_createobjectbatch--';
// Invoke the batch API calls
let response = await this.httpClient.request({
url: this.batchUrl, // https://walletobjects.googleapis.com/batch
let response = await this.client.context._options.auth.request({
url: 'https://walletobjects.googleapis.com/batch',
method: 'POST',
data: data,
headers: {

View File

@@ -16,7 +16,7 @@
// [START setup]
// [START imports]
const { GoogleAuth } = require('google-auth-library');
const { google } = require('googleapis');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
// [END imports]
@@ -31,12 +31,6 @@ class DemoGeneric {
* variable: GOOGLE_APPLICATION_CREDENTIALS.
*/
this.keyFilePath = process.env.GOOGLE_APPLICATION_CREDENTIALS || '/path/to/key.json';
this.baseUrl = 'https://walletobjects.googleapis.com/walletobjects/v1';
this.batchUrl = 'https://walletobjects.googleapis.com/batch';
this.classUrl = `${this.baseUrl}/genericClass`;
this.objectUrl = `${this.baseUrl}/genericObject`;
this.auth();
}
// [END setup]
@@ -46,11 +40,16 @@ class DemoGeneric {
* Create authenticated HTTP client using a service account file.
*/
auth() {
const auth = new google.auth.GoogleAuth({
keyFile: this.keyFilePath,
scopes: ['https://www.googleapis.com/auth/wallet_object.issuer'],
});
this.credentials = require(this.keyFilePath);
this.httpClient = new GoogleAuth({
credentials: this.credentials,
scopes: 'https://www.googleapis.com/auth/wallet_object.issuer'
this.client = google.walletobjects({
version: 'v1',
auth: auth,
});
}
// [END auth]
@@ -69,9 +68,8 @@ class DemoGeneric {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.genericclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
console.log(`Class ${issuerId}.${classSuffix} already exists!`);
@@ -91,10 +89,8 @@ class DemoGeneric {
'id': `${issuerId}.${classSuffix}`
};
response = await this.httpClient.request({
url: this.classUrl,
method: 'POST',
data: newClass
response = await this.client.genericclass.insert({
requestBody: newClass
});
console.log('Class insert response');
@@ -120,9 +116,8 @@ class DemoGeneric {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.genericclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -150,10 +145,9 @@ class DemoGeneric {
}
updatedClass['linksModuleData']['uris'].push(newLink);
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PUT',
data: updatedClass
response = await this.client.genericclass.update({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: updatedClass
});
console.log('Class update response');
@@ -179,9 +173,8 @@ class DemoGeneric {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.genericclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -216,10 +209,9 @@ class DemoGeneric {
}
patchBody['linksModuleData']['uris'].push(newLink);
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.genericclass.patch({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: patchBody
});
console.log('Class patch response');
@@ -244,9 +236,8 @@ class DemoGeneric {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.genericobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
console.log(`Object ${issuerId}.${objectSuffix} already exists!`);
@@ -344,10 +335,8 @@ class DemoGeneric {
}
};
response = await this.httpClient.request({
url: this.objectUrl,
method: 'POST',
data: newObject
response = await this.client.genericobject.insert({
requestBody: newObject
});
console.log('Object insert response');
@@ -373,9 +362,8 @@ class DemoGeneric {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.genericobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -404,10 +392,9 @@ class DemoGeneric {
updatedObject['linksModuleData']['uris'].push(newLink);
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PUT',
data: updatedObject
response = await this.client.genericobject.update({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: updatedObject
});
console.log('Object update response');
@@ -431,9 +418,8 @@ class DemoGeneric {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.genericobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -467,10 +453,9 @@ class DemoGeneric {
}
patchBody['linksModuleData']['uris'].push(newLink);
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.genericobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object patch response');
@@ -497,9 +482,8 @@ class DemoGeneric {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.genericobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -517,10 +501,9 @@ class DemoGeneric {
'state': 'EXPIRED'
};
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.genericobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object expiration response');
@@ -858,8 +841,8 @@ class DemoGeneric {
data += '--batch_createobjectbatch--';
// Invoke the batch API calls
let response = await this.httpClient.request({
url: this.batchUrl, // https://walletobjects.googleapis.com/batch
let response = await this.client.context._options.auth.request({
url: 'https://walletobjects.googleapis.com/batch',
method: 'POST',
data: data,
headers: {

View File

@@ -16,7 +16,7 @@
// [START setup]
// [START imports]
const { GoogleAuth } = require('google-auth-library');
const { google } = require('googleapis');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
// [END imports]
@@ -31,12 +31,6 @@ class DemoGiftCard {
* variable: GOOGLE_APPLICATION_CREDENTIALS.
*/
this.keyFilePath = process.env.GOOGLE_APPLICATION_CREDENTIALS || '/path/to/key.json';
this.baseUrl = 'https://walletobjects.googleapis.com/walletobjects/v1';
this.batchUrl = 'https://walletobjects.googleapis.com/batch';
this.classUrl = `${this.baseUrl}/giftCardClass`;
this.objectUrl = `${this.baseUrl}/giftCardObject`;
this.auth();
}
// [END setup]
@@ -46,11 +40,16 @@ class DemoGiftCard {
* Create authenticated HTTP client using a service account file.
*/
auth() {
const auth = new google.auth.GoogleAuth({
keyFile: this.keyFilePath,
scopes: ['https://www.googleapis.com/auth/wallet_object.issuer'],
});
this.credentials = require(this.keyFilePath);
this.httpClient = new GoogleAuth({
credentials: this.credentials,
scopes: 'https://www.googleapis.com/auth/wallet_object.issuer'
this.client = google.walletobjects({
version: 'v1',
auth: auth,
});
}
// [END auth]
@@ -69,9 +68,8 @@ class DemoGiftCard {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.giftcardclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
console.log(`Class ${issuerId}.${classSuffix} already exists!`);
@@ -93,10 +91,8 @@ class DemoGiftCard {
'reviewStatus': 'UNDER_REVIEW',
};
response = await this.httpClient.request({
url: this.classUrl,
method: 'POST',
data: newClass
response = await this.client.giftcardclass.insert({
requestBody: newClass
});
console.log('Class insert response');
@@ -122,9 +118,8 @@ class DemoGiftCard {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.giftcardclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -149,10 +144,9 @@ class DemoGiftCard {
// Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates
updatedClass['reviewStatus'] = 'UNDER_REVIEW';
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PUT',
data: updatedClass
response = await this.client.giftcardclass.update({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: updatedClass
});
console.log('Class update response');
@@ -178,9 +172,8 @@ class DemoGiftCard {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.giftcardclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -204,10 +197,9 @@ class DemoGiftCard {
'reviewStatus': 'UNDER_REVIEW'
};
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.giftcardclass.patch({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: patchBody
});
console.log('Class patch response');
@@ -233,9 +225,8 @@ class DemoGiftCard {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.giftcardclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -248,10 +239,9 @@ class DemoGiftCard {
}
}
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.giftcardclass.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -281,9 +271,8 @@ class DemoGiftCard {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.giftcardobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
console.log(`Object ${issuerId}.${objectSuffix} already exists!`);
@@ -372,10 +361,8 @@ class DemoGiftCard {
}
};
response = await this.httpClient.request({
url: this.objectUrl,
method: 'POST',
data: newObject
response = await this.client.giftcardobject.insert({
requestBody: newObject
});
console.log('Object insert response');
@@ -401,9 +388,8 @@ class DemoGiftCard {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.giftcardobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -432,10 +418,9 @@ class DemoGiftCard {
updatedObject['linksModuleData']['uris'].push(newLink);
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PUT',
data: updatedObject
response = await this.client.giftcardobject.update({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: updatedObject
});
console.log('Object update response');
@@ -459,9 +444,8 @@ class DemoGiftCard {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.giftcardobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -495,10 +479,9 @@ class DemoGiftCard {
}
patchBody['linksModuleData']['uris'].push(newLink);
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.giftcardobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object patch response');
@@ -525,9 +508,8 @@ class DemoGiftCard {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.giftcardobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -545,10 +527,9 @@ class DemoGiftCard {
'state': 'EXPIRED'
};
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.giftcardobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object expiration response');
@@ -574,9 +555,8 @@ class DemoGiftCard {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.giftcardobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -589,10 +569,9 @@ class DemoGiftCard {
}
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.giftcardobject.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -919,8 +898,8 @@ class DemoGiftCard {
data += '--batch_createobjectbatch--';
// Invoke the batch API calls
let response = await this.httpClient.request({
url: this.batchUrl, // https://walletobjects.googleapis.com/batch
let response = await this.client.context._options.auth.request({
url: 'https://walletobjects.googleapis.com/batch',
method: 'POST',
data: data,
headers: {

View File

@@ -16,7 +16,7 @@
// [START setup]
// [START imports]
const { GoogleAuth } = require('google-auth-library');
const { google } = require('googleapis');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
// [END imports]
@@ -31,12 +31,6 @@ class DemoLoyalty {
* variable: GOOGLE_APPLICATION_CREDENTIALS.
*/
this.keyFilePath = process.env.GOOGLE_APPLICATION_CREDENTIALS || '/path/to/key.json';
this.baseUrl = 'https://walletobjects.googleapis.com/walletobjects/v1';
this.batchUrl = 'https://walletobjects.googleapis.com/batch';
this.classUrl = `${this.baseUrl}/loyaltyClass`;
this.objectUrl = `${this.baseUrl}/loyaltyObject`;
this.auth();
}
// [END setup]
@@ -46,11 +40,16 @@ class DemoLoyalty {
* Create authenticated HTTP client using a service account file.
*/
auth() {
const auth = new google.auth.GoogleAuth({
keyFile: this.keyFilePath,
scopes: ['https://www.googleapis.com/auth/wallet_object.issuer'],
});
this.credentials = require(this.keyFilePath);
this.httpClient = new GoogleAuth({
credentials: this.credentials,
scopes: 'https://www.googleapis.com/auth/wallet_object.issuer'
this.client = google.walletobjects({
version: 'v1',
auth: auth,
});
}
// [END auth]
@@ -69,9 +68,8 @@ class DemoLoyalty {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.loyaltyclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
console.log(`Class ${issuerId}.${classSuffix} already exists!`);
@@ -105,10 +103,8 @@ class DemoLoyalty {
}
};
response = await this.httpClient.request({
url: this.classUrl,
method: 'POST',
data: newClass
response = await this.client.loyaltyclass.insert({
requestBody: newClass
});
console.log('Class insert response');
@@ -134,9 +130,8 @@ class DemoLoyalty {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.loyaltyclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -161,10 +156,9 @@ class DemoLoyalty {
// Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates
updatedClass['reviewStatus'] = 'UNDER_REVIEW';
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PUT',
data: updatedClass
response = await this.client.loyaltyclass.update({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: updatedClass
});
console.log('Class update response');
@@ -190,9 +184,8 @@ class DemoLoyalty {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.loyaltyclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -216,10 +209,9 @@ class DemoLoyalty {
'reviewStatus': 'UNDER_REVIEW'
};
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.loyaltyclass.patch({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: patchBody
});
console.log('Class patch response');
@@ -245,9 +237,8 @@ class DemoLoyalty {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.loyaltyclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -260,10 +251,9 @@ class DemoLoyalty {
}
}
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.loyaltyclass.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -293,9 +283,8 @@ class DemoLoyalty {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.loyaltyobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
console.log(`Object ${issuerId}.${objectSuffix} already exists!`);
@@ -383,10 +372,8 @@ class DemoLoyalty {
}
};
response = await this.httpClient.request({
url: this.objectUrl,
method: 'POST',
data: newObject
response = await this.client.loyaltyobject.insert({
requestBody: newObject
});
console.log('Object insert response');
@@ -412,9 +399,8 @@ class DemoLoyalty {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.loyaltyobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -443,10 +429,9 @@ class DemoLoyalty {
updatedObject['linksModuleData']['uris'].push(newLink);
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PUT',
data: updatedObject
response = await this.client.loyaltyobject.update({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: updatedObject
});
console.log('Object update response');
@@ -470,9 +455,8 @@ class DemoLoyalty {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.loyaltyobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -506,10 +490,9 @@ class DemoLoyalty {
}
patchBody['linksModuleData']['uris'].push(newLink);
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.loyaltyobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object patch response');
@@ -536,9 +519,8 @@ class DemoLoyalty {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.loyaltyobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -556,10 +538,9 @@ class DemoLoyalty {
'state': 'EXPIRED'
};
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.loyaltyobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object expiration response');
@@ -585,9 +566,8 @@ class DemoLoyalty {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.loyaltyobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -600,10 +580,9 @@ class DemoLoyalty {
}
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.loyaltyobject.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -940,8 +919,8 @@ class DemoLoyalty {
data += '--batch_createobjectbatch--';
// Invoke the batch API calls
let response = await this.httpClient.request({
url: this.batchUrl, // https://walletobjects.googleapis.com/batch
let response = await this.client.context._options.auth.request({
url: 'https://walletobjects.googleapis.com/batch',
method: 'POST',
data: data,
headers: {

View File

@@ -16,7 +16,7 @@
// [START setup]
// [START imports]
const { GoogleAuth } = require('google-auth-library');
const { google } = require('googleapis');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
// [END imports]
@@ -31,12 +31,6 @@ class DemoOffer {
* variable: GOOGLE_APPLICATION_CREDENTIALS.
*/
this.keyFilePath = process.env.GOOGLE_APPLICATION_CREDENTIALS || '/path/to/key.json';
this.baseUrl = 'https://walletobjects.googleapis.com/walletobjects/v1';
this.batchUrl = 'https://walletobjects.googleapis.com/batch';
this.classUrl = `${this.baseUrl}/offerClass`;
this.objectUrl = `${this.baseUrl}/offerObject`;
this.auth();
}
// [END setup]
@@ -46,11 +40,16 @@ class DemoOffer {
* Create authenticated HTTP client using a service account file.
*/
auth() {
const auth = new google.auth.GoogleAuth({
keyFile: this.keyFilePath,
scopes: ['https://www.googleapis.com/auth/wallet_object.issuer'],
});
this.credentials = require(this.keyFilePath);
this.httpClient = new GoogleAuth({
credentials: this.credentials,
scopes: 'https://www.googleapis.com/auth/wallet_object.issuer'
this.client = google.walletobjects({
version: 'v1',
auth: auth,
});
}
// [END auth]
@@ -69,9 +68,8 @@ class DemoOffer {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.offerclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
console.log(`Class ${issuerId}.${classSuffix} already exists!`);
@@ -96,10 +94,8 @@ class DemoOffer {
'redemptionChannel': 'ONLINE'
};
response = await this.httpClient.request({
url: this.classUrl,
method: 'POST',
data: newClass
response = await this.client.offerclass.insert({
requestBody: newClass
});
console.log('Class insert response');
@@ -125,9 +121,8 @@ class DemoOffer {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.offerclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -152,10 +147,9 @@ class DemoOffer {
// Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates
updatedClass['reviewStatus'] = 'UNDER_REVIEW';
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PUT',
data: updatedClass
response = await this.client.offerclass.update({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: updatedClass
});
console.log('Class update response');
@@ -181,9 +175,8 @@ class DemoOffer {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.offerclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -207,10 +200,9 @@ class DemoOffer {
'reviewStatus': 'UNDER_REVIEW'
};
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.offerclass.patch({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: patchBody
});
console.log('Class patch response');
@@ -236,9 +228,8 @@ class DemoOffer {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.offerclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -251,10 +242,9 @@ class DemoOffer {
}
}
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.offerclass.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -284,9 +274,8 @@ class DemoOffer {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.offerobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
console.log(`Object ${issuerId}.${objectSuffix} already exists!`);
@@ -374,10 +363,8 @@ class DemoOffer {
}
};
response = await this.httpClient.request({
url: this.objectUrl,
method: 'POST',
data: newObject
response = await this.client.offerobject.insert({
requestBody: newObject
});
console.log('Object insert response');
@@ -403,9 +390,8 @@ class DemoOffer {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.offerobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -434,10 +420,9 @@ class DemoOffer {
updatedObject['linksModuleData']['uris'].push(newLink);
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PUT',
data: updatedObject
response = await this.client.offerobject.update({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: updatedObject
});
console.log('Object update response');
@@ -461,9 +446,8 @@ class DemoOffer {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.offerobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -497,10 +481,9 @@ class DemoOffer {
}
patchBody['linksModuleData']['uris'].push(newLink);
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.offerobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object patch response');
@@ -527,9 +510,8 @@ class DemoOffer {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.offerobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -547,10 +529,9 @@ class DemoOffer {
'state': 'EXPIRED'
};
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.offerobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object expiration response');
@@ -576,9 +557,8 @@ class DemoOffer {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.offerobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -591,10 +571,9 @@ class DemoOffer {
}
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.offerobject.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -922,8 +901,8 @@ class DemoOffer {
data += '--batch_createobjectbatch--';
// Invoke the batch API calls
let response = await this.httpClient.request({
url: this.batchUrl, // https://walletobjects.googleapis.com/batch
let response = await this.client.context._options.auth.request({
url: 'https://walletobjects.googleapis.com/batch',
method: 'POST',
data: data,
headers: {

View File

@@ -16,7 +16,7 @@
// [START setup]
// [START imports]
const { GoogleAuth } = require('google-auth-library');
const { google } = require('googleapis');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
// [END imports]
@@ -31,12 +31,6 @@ class DemoTransit {
* variable: GOOGLE_APPLICATION_CREDENTIALS.
*/
this.keyFilePath = process.env.GOOGLE_APPLICATION_CREDENTIALS || '/path/to/key.json';
this.baseUrl = 'https://walletobjects.googleapis.com/walletobjects/v1';
this.batchUrl = 'https://walletobjects.googleapis.com/batch';
this.classUrl = `${this.baseUrl}/transitClass`;
this.objectUrl = `${this.baseUrl}/transitObject`;
this.auth();
}
// [END setup]
@@ -46,11 +40,16 @@ class DemoTransit {
* Create authenticated HTTP client using a service account file.
*/
auth() {
const auth = new google.auth.GoogleAuth({
keyFile: this.keyFilePath,
scopes: ['https://www.googleapis.com/auth/wallet_object.issuer'],
});
this.credentials = require(this.keyFilePath);
this.httpClient = new GoogleAuth({
credentials: this.credentials,
scopes: 'https://www.googleapis.com/auth/wallet_object.issuer'
this.client = google.walletobjects({
version: 'v1',
auth: auth,
});
}
// [END auth]
@@ -69,9 +68,8 @@ class DemoTransit {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.transitclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
console.log(`Class ${issuerId}.${classSuffix} already exists!`);
@@ -105,10 +103,8 @@ class DemoTransit {
'transitType': 'BUS'
};
response = await this.httpClient.request({
url: this.classUrl,
method: 'POST',
data: newClass
response = await this.client.transitclass.insert({
requestBody: newClass
});
console.log('Class insert response');
@@ -134,9 +130,8 @@ class DemoTransit {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.transitclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -161,10 +156,9 @@ class DemoTransit {
// Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates
updatedClass['reviewStatus'] = 'UNDER_REVIEW';
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PUT',
data: updatedClass
response = await this.client.transitclass.update({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: updatedClass
});
console.log('Class update response');
@@ -190,9 +184,8 @@ class DemoTransit {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.transitclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -216,10 +209,9 @@ class DemoTransit {
'reviewStatus': 'UNDER_REVIEW'
};
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.transitclass.patch({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: patchBody
});
console.log('Class patch response');
@@ -245,9 +237,8 @@ class DemoTransit {
// Check if the class exists
try {
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}`,
method: 'GET'
response = await this.client.transitclass.get({
resourceId: `${issuerId}.${classSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -260,10 +251,9 @@ class DemoTransit {
}
}
response = await this.httpClient.request({
url: `${this.classUrl}/${issuerId}.${classSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.transitclass.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -293,9 +283,8 @@ class DemoTransit {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.transitobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
console.log(`Object ${issuerId}.${objectSuffix} already exists!`);
@@ -402,10 +391,8 @@ class DemoTransit {
}
};
response = await this.httpClient.request({
url: this.objectUrl,
method: 'POST',
data: newObject
response = await this.client.transitobject.insert({
requestBody: newObject
});
console.log('Object insert response');
@@ -431,9 +418,8 @@ class DemoTransit {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.transitobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -462,10 +448,9 @@ class DemoTransit {
updatedObject['linksModuleData']['uris'].push(newLink);
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PUT',
data: updatedObject
response = await this.client.transitobject.update({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: updatedObject
});
console.log('Object update response');
@@ -489,9 +474,8 @@ class DemoTransit {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.transitobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -525,10 +509,9 @@ class DemoTransit {
}
patchBody['linksModuleData']['uris'].push(newLink);
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.transitobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object patch response');
@@ -555,9 +538,8 @@ class DemoTransit {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.transitobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -575,10 +557,9 @@ class DemoTransit {
'state': 'EXPIRED'
};
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'PATCH',
data: patchBody
response = await this.client.transitobject.patch({
resourceId: `${issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object expiration response');
@@ -604,9 +585,8 @@ class DemoTransit {
// Check if the object exists
try {
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}`,
method: 'GET'
response = await this.client.transitobject.get({
resourceId: `${issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
@@ -619,10 +599,9 @@ class DemoTransit {
}
}
response = await this.httpClient.request({
url: `${this.objectUrl}/${issuerId}.${objectSuffix}/addMessage`,
method: 'POST',
data: {
response = await this.client.transitobject.addmessage({
resourceId: `${issuerId}.${classSuffix}`,
requestBody: {
'message': {
'header': header,
'body': body
@@ -997,8 +976,8 @@ class DemoTransit {
data += '--batch_createobjectbatch--';
// Invoke the batch API calls
let response = await this.httpClient.request({
url: this.batchUrl, // https://walletobjects.googleapis.com/batch
let response = await this.client.context._options.auth.request({
url: 'https://walletobjects.googleapis.com/batch',
method: 'POST',
data: data,
headers: {

51
nodejs/demo.js Normal file
View File

@@ -0,0 +1,51 @@
const { DemoEventTicket } = require('./demo-eventticket.js');
const { DemoFlight } = require('./demo-flight.js');
const { DemoGeneric } = require('./demo-generic.js');
const { DemoGiftCard } = require('./demo-giftcard.js');
const { DemoLoyalty } = require('./demo-loyalty.js');
const { DemoOffer } = require('./demo-offer.js');
const { DemoTransit } = require('./demo-transit.js');
async function main() {
// Create a demo class instance
// Creates the authenticated HTTP client
let demo = new DemoEventTicket(); // change to demo a different pass type
const issuer_id = process.env.WALLET_ISSUER_ID || 'your-issuer-id';
const class_suffix = (process.env.WALLET_CLASS_SUFFIX || 'your-class-suffix') + demo.constructor.name;
const object_suffix = (process.env.WALLET_OBJECT_SUFFIX || 'your-object-suffix') + demo.constructor.name;
// Create a pass class
demo.createClass(issuer_id, class_suffix);
// Update a pass class
demo.updateClass(issuer_id, class_suffix);
// Patch a pass class
demo.patchClass(issuer_id, class_suffix);
// // Create a pass object
demo.createObject(issuer_id, class_suffix, object_suffix);
// Update a pass object
demo.updateObject(issuer_id, object_suffix);
// Patch a pass object
demo.patchObject(issuer_id, object_suffix);
// Expire a pass object
demo.expireObject(issuer_id, object_suffix);
// Generate an Add to Google Wallet link that creates a new pass class and object
demo.createJwtNewObjects(issuer_id, class_suffix, object_suffix);
// Generate an Add to Google Wallet link that references existing pass object(s)
demo.createJwtExistingObjects(issuer_id);
// // Create pass objects in batch
demo.batchCreateObjects(issuer_id, class_suffix);
}
main().catch(console.error);

View File

@@ -1,7 +1,8 @@
{
"dependencies": {
"google-auth-library": "^5.9.2",
"jsonwebtoken": "^8.5.1",
"@googleapis/walletobjects": "^1.0.0",
"googleapis": "^133.0.0",
"jsonwebtoken": "^9.0.2",
"uuidv4": "^6.2.13"
}
}