diff --git a/python/Pipfile b/python/Pipfile index 4dff796..8f4d8cb 100644 --- a/python/Pipfile +++ b/python/Pipfile @@ -4,6 +4,7 @@ verify_ssl = true name = "pypi" [packages] +googleapiclient = "*" google-auth = "*" requests = "*" diff --git a/python/demo.py b/python/demo.py new file mode 100644 index 0000000..4b185fb --- /dev/null +++ b/python/demo.py @@ -0,0 +1,62 @@ +import os + +from demo_eventticket import DemoEventTicket +from demo_flight import DemoFlight +from demo_generic import DemoGeneric +from demo_giftcard import DemoGiftCard +from demo_loyalty import DemoLoyalty +from demo_offer import DemoOffer +from demo_transit import DemoTransit + +if __name__ == "__main__": + + # Create a demo class instance + # Creates the authenticated HTTP client + demo = DemoTransit() # change to demo a different pass type + + issuer_id = os.environ.get("WALLET_ISSUER_ID", "your-issuer-id") + class_suffix = os.environ.get("WALLET_CLASS_SUFFIX", "your-class-suffix") + demo.__class__.__name__ + object_suffix = os.environ.get("WALLET_OBJECT_SUFFIX", "your-object-suffix") + demo.__class__.__name__ + + # Create a pass class + demo.create_class(issuer_id=issuer_id, + class_suffix=class_suffix) + + # Update a pass class + demo.update_class(issuer_id=issuer_id, + class_suffix=class_suffix) + + # Patch a pass class + demo.patch_class(issuer_id=issuer_id, + class_suffix=class_suffix) + + # Create a pass object + demo.create_object(issuer_id=issuer_id, + class_suffix=class_suffix, + object_suffix=object_suffix) + + # Update a pass object + demo.update_object(issuer_id=issuer_id, + object_suffix=object_suffix) + + # Patch a pass object + demo.patch_object(issuer_id=issuer_id, + object_suffix=object_suffix) + + # Expire a pass object + demo.expire_object(issuer_id=issuer_id, + object_suffix=object_suffix) + + # Create an "Add to Google Wallet" link + # that generates a new pass class and object + demo.create_jwt_new_objects(issuer_id=issuer_id, + class_suffix=class_suffix, + object_suffix=object_suffix) + + # Create an "Add to Google Wallet" link + # that references existing pass classes and objects + demo.create_jwt_existing_objects(issuer_id=issuer_id) + + # Create pass objects in batch + demo.batch_create_objects(issuer_id=issuer_id, + class_suffix=class_suffix) diff --git a/python/demo_eventticket.py b/python/demo_eventticket.py index 0a0a6c6..dd4e122 100644 --- a/python/demo_eventticket.py +++ b/python/demo_eventticket.py @@ -21,7 +21,8 @@ import json import os import uuid -from google.auth.transport.requests import AuthorizedSession +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError from google.oauth2.service_account import Credentials from google.auth import jwt, crypt # [END imports] @@ -39,11 +40,6 @@ class DemoEventTicket: def __init__(self): self.key_file_path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/key.json') - self.base_url = 'https://walletobjects.googleapis.com/walletobjects/v1' - self.batch_url = 'https://walletobjects.googleapis.com/batch' - self.class_url = f'{self.base_url}/eventTicketClass' - self.object_url = f'{self.base_url}/eventTicketObject' - # Set up authenticated client self.auth() @@ -56,7 +52,7 @@ class DemoEventTicket: self.key_file_path, scopes=['https://www.googleapis.com/auth/wallet_object.issuer']) - self.http_client = AuthorizedSession(self.credentials) + self.client = build('walletobjects', 'v1', credentials=self.credentials) # [END auth] @@ -73,16 +69,16 @@ class DemoEventTicket: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 200: + try: + self.client.eventticketclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' + else: print(f'Class {issuer_id}.{class_suffix} already exists!') return f'{issuer_id}.{class_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' # See link below for more information on required properties # https://developers.google.com/wallet/tickets/events/rest/v1/eventticketclass @@ -99,12 +95,12 @@ class DemoEventTicket: 'reviewStatus': 'UNDER_REVIEW' } - response = self.http_client.post(url=self.class_url, json=new_class) + response = self.client.eventticketclass().insert(body=new_class).execute() print('Class insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END createClass] @@ -123,19 +119,19 @@ class DemoEventTicket: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.eventticketclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Class exists - updated_class = response.json() + updated_class = response # Update the class by adding a homepage updated_class['homepageUri'] = { @@ -146,14 +142,14 @@ class DemoEventTicket: # Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updated_class['reviewStatus'] = 'UNDER_REVIEW' - response = self.http_client.put( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', - json=updated_class) + response = self.client.eventticketclass().update( + resourceId=f'{issuer_id}.{class_suffix}', + body=updated_class).execute() print('Class update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END updateClass] @@ -172,16 +168,16 @@ class DemoEventTicket: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.eventticketclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Patch the class by adding a homepage patch_body = { @@ -194,13 +190,14 @@ class DemoEventTicket: 'reviewStatus': 'UNDER_REVIEW' } - response = self.http_client.patch( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', json=patch_body) + response = self.client.eventticketclass().patch( + resourceId=f'{issuer_id}.{class_suffix}', + body=patch_body).execute() print('Class patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END patchClass] @@ -220,28 +217,28 @@ class DemoEventTicket: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') + try: + response = self.client.eventticketclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' - - response = self.http_client.post( - url=f'{self.class_url}/{issuer_id}.{class_suffix}/addMessage', - json={'message': { + response = self.client.eventticketclass().addmessage( + resourceId=f'{issuer_id}.{class_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Class addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END addMessageClass] @@ -260,16 +257,15 @@ class DemoEventTicket: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 200: + try: + self.client.eventticketobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' + else: print(f'Object {issuer_id}.{object_suffix} already exists!') - print(response.text) - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) return f'{issuer_id}.{object_suffix}' # See link below for more information on required properties @@ -360,12 +356,12 @@ class DemoEventTicket: } # Create the object - response = self.http_client.post(url=self.object_url, json=new_object) + response = self.client.eventticketobject().insert(body=new_object).execute() print('Object insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END createObject] @@ -384,19 +380,19 @@ class DemoEventTicket: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.eventticketobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - updated_object = response.json() + updated_object = response # Update the object by adding a link new_link = { @@ -407,14 +403,14 @@ class DemoEventTicket: updated_object['linksModuleData'] = {'uris': []} updated_object['linksModuleData']['uris'].append(new_link) - response = self.http_client.put( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=updated_object) + response = self.client.eventticketobject().update( + resourceId=f'{issuer_id}.{object_suffix}', + body=updated_object).execute() print('Object update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END updateObject] @@ -431,19 +427,19 @@ class DemoEventTicket: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.eventticketobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - existing_object = response.json() + existing_object = response # Patch the object by adding a link patch_body = {} @@ -458,14 +454,14 @@ class DemoEventTicket: patch_body['linksModuleData'] = {'uris': []} patch_body['linksModuleData']['uris'].append(new_link) - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.eventticketobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END patchObject] @@ -485,28 +481,28 @@ class DemoEventTicket: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.eventticketobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Patch the object, setting the pass as expired patch_body = {'state': 'EXPIRED'} - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.eventticketobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object expiration response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END expireObject] @@ -526,28 +522,28 @@ class DemoEventTicket: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') + try: + response = self.client.eventticketobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' - - response = self.http_client.post( - url=f'{self.object_url}/{issuer_id}.{object_suffix}/addMessage', - json={'message': { + response = self.client.eventticketobject().addmessage( + resourceId=f'{issuer_id}.{object_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Object addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END addMessageObject] @@ -797,7 +793,7 @@ class DemoEventTicket: issuer_id (str): The issuer ID being used for this request. class_suffix (str): Developer-defined unique ID for this pass class. """ - data = '' + batch = self.client.new_batch_http_request() # Example: Generate three new pass objects for _ in range(3): @@ -891,25 +887,11 @@ class DemoEventTicket: 'ticketNumber': 'Ticket number' } - data += '--batch_createobjectbatch\n' - data += 'Content-Type: application/json\n\n' - data += 'POST /walletobjects/v1/eventTicketObject/\n\n' - - data += json.dumps(batch_object) + '\n\n' - - data += '--batch_createobjectbatch--' + batch.add(self.client.eventticketobject().insert(body=batch_object)) # Invoke the batch API calls - response = self.http_client.post( - url=self.batch_url, # https://walletobjects.googleapis.com/batch - data=data, - headers={ - # `boundary` is the delimiter between API calls in the batch request - 'Content-Type': - 'multipart/mixed; boundary=batch_createobjectbatch' - }) + response = batch.execute() - print('Batch insert response') - print(response.content.decode('UTF-8')) + print('Batch complete') # [END batch] diff --git a/python/demo_flight.py b/python/demo_flight.py index 7d50fab..03462b2 100644 --- a/python/demo_flight.py +++ b/python/demo_flight.py @@ -21,7 +21,8 @@ import json import os import uuid -from google.auth.transport.requests import AuthorizedSession +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError from google.oauth2.service_account import Credentials from google.auth import jwt, crypt # [END imports] @@ -39,11 +40,6 @@ class DemoFlight: def __init__(self): self.key_file_path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/key.json') - self.base_url = 'https://walletobjects.googleapis.com/walletobjects/v1' - self.batch_url = 'https://walletobjects.googleapis.com/batch' - self.class_url = f'{self.base_url}/flightClass' - self.object_url = f'{self.base_url}/flightObject' - # Set up authenticated client self.auth() @@ -56,7 +52,7 @@ class DemoFlight: self.key_file_path, scopes=['https://www.googleapis.com/auth/wallet_object.issuer']) - self.http_client = AuthorizedSession(self.credentials) + self.client = build('walletobjects', 'v1', credentials=self.credentials) # [END auth] @@ -73,16 +69,16 @@ class DemoFlight: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 200: + try: + self.client.flightclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' + else: print(f'Class {issuer_id}.{class_suffix} already exists!') return f'{issuer_id}.{class_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' # See link below for more information on required properties # https://developers.google.com/wallet/tickets/events/rest/v1/eventticketclass @@ -109,12 +105,12 @@ class DemoFlight: } } - response = self.http_client.post(url=self.class_url, json=new_class) + response = self.client.flightclass().insert(body=new_class).execute() print('Class insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END createClass] @@ -133,19 +129,19 @@ class DemoFlight: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.flightclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Class exists - updated_class = response.json() + updated_class = response # Update the class by adding a homepage updated_class['homepageUri'] = { @@ -156,14 +152,14 @@ class DemoFlight: # Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updated_class['reviewStatus'] = 'UNDER_REVIEW' - response = self.http_client.put( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', - json=updated_class) + response = self.client.flightclass().update( + resourceId=f'{issuer_id}.{class_suffix}', + body=updated_class).execute() print('Class update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END updateClass] @@ -182,16 +178,16 @@ class DemoFlight: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.flightclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Patch the class by adding a homepage patch_body = { @@ -204,13 +200,14 @@ class DemoFlight: 'reviewStatus': 'UNDER_REVIEW' } - response = self.http_client.patch( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', json=patch_body) + response = self.client.flightclass().patch( + resourceId=f'{issuer_id}.{class_suffix}', + body=patch_body).execute() print('Class patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END patchClass] @@ -230,28 +227,28 @@ class DemoFlight: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') + try: + response = self.client.flightclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' - - response = self.http_client.post( - url=f'{self.class_url}/{issuer_id}.{class_suffix}/addMessage', - json={'message': { + response = self.client.flightclass().addmessage( + resourceId=f'{issuer_id}.{class_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Class addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END addMessageClass] @@ -270,16 +267,15 @@ class DemoFlight: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 200: + try: + self.client.flightobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' + else: print(f'Object {issuer_id}.{object_suffix} already exists!') - print(response.text) - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) return f'{issuer_id}.{object_suffix}' # See link below for more information on required properties @@ -350,12 +346,12 @@ class DemoFlight: } # Create the object - response = self.http_client.post(url=self.object_url, json=new_object) + response = self.client.flightobject().insert(body=new_object).execute() print('Object insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END createObject] @@ -374,19 +370,19 @@ class DemoFlight: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.flightobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - updated_object = response.json() + updated_object = response # Update the object by adding a link new_link = { @@ -397,14 +393,14 @@ class DemoFlight: updated_object['linksModuleData'] = {'uris': []} updated_object['linksModuleData']['uris'].append(new_link) - response = self.http_client.put( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=updated_object) + response = self.client.flightobject().update( + resourceId=f'{issuer_id}.{object_suffix}', + body=updated_object).execute() print('Object update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END updateObject] @@ -421,19 +417,19 @@ class DemoFlight: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.flightobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - existing_object = response.json() + existing_object = response # Patch the object by adding a link patch_body = {} @@ -448,14 +444,14 @@ class DemoFlight: patch_body['linksModuleData'] = {'uris': []} patch_body['linksModuleData']['uris'].append(new_link) - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.flightobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END patchObject] @@ -475,28 +471,28 @@ class DemoFlight: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.flightobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Patch the object, setting the pass as expired patch_body = {'state': 'EXPIRED'} - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.flightobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object expiration response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END expireObject] @@ -516,28 +512,28 @@ class DemoFlight: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') + try: + response = self.client.flightobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' - - response = self.http_client.post( - url=f'{self.object_url}/{issuer_id}.{object_suffix}/addMessage', - json={'message': { + response = self.client.flightobject().addmessage( + resourceId=f'{issuer_id}.{object_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Object addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END addMessageObject] @@ -778,7 +774,7 @@ class DemoFlight: issuer_id (str): The issuer ID being used for this request. class_suffix (str): Developer-defined unique ID for this pass class. """ - data = '' + batch = self.client.new_batch_http_request() # Example: Generate three new pass objects for _ in range(3): @@ -852,25 +848,11 @@ class DemoFlight: } } - data += '--batch_createobjectbatch\n' - data += 'Content-Type: application/json\n\n' - data += 'POST /walletobjects/v1/flightObject/\n\n' - - data += json.dumps(batch_object) + '\n\n' - - data += '--batch_createobjectbatch--' + batch.add(self.client.flightobject().insert(body=batch_object)) # Invoke the batch API calls - response = self.http_client.post( - url=self.batch_url, # https://walletobjects.googleapis.com/batch - data=data, - headers={ - # `boundary` is the delimiter between API calls in the batch request - 'Content-Type': - 'multipart/mixed; boundary=batch_createobjectbatch' - }) + response = batch.execute() - print('Batch insert response') - print(response.content.decode('UTF-8')) + print('Batch complete') # [END batch] diff --git a/python/demo_generic.py b/python/demo_generic.py index a151e55..9dc29b4 100644 --- a/python/demo_generic.py +++ b/python/demo_generic.py @@ -21,7 +21,8 @@ import json import os import uuid -from google.auth.transport.requests import AuthorizedSession +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError from google.oauth2.service_account import Credentials from google.auth import jwt, crypt # [END imports] @@ -39,11 +40,6 @@ class DemoGeneric: def __init__(self): self.key_file_path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/key.json') - self.base_url = 'https://walletobjects.googleapis.com/walletobjects/v1' - self.batch_url = 'https://walletobjects.googleapis.com/batch' - self.class_url = f'{self.base_url}/genericClass' - self.object_url = f'{self.base_url}/genericObject' - # Set up authenticated client self.auth() @@ -56,7 +52,7 @@ class DemoGeneric: self.key_file_path, scopes=['https://www.googleapis.com/auth/wallet_object.issuer']) - self.http_client = AuthorizedSession(self.credentials) + self.client = build('walletobjects', 'v1', credentials=self.credentials) # [END auth] @@ -73,27 +69,27 @@ class DemoGeneric: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 200: + try: + self.client.genericclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' + else: print(f'Class {issuer_id}.{class_suffix} already exists!') return f'{issuer_id}.{class_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' # See link below for more information on required properties # https://developers.google.com/wallet/generic/rest/v1/genericclass new_class = {'id': f'{issuer_id}.{class_suffix}'} - response = self.http_client.post(url=self.class_url, json=new_class) + response = self.client.genericclass().insert(body=new_class).execute() print('Class insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END createClass] @@ -112,19 +108,19 @@ class DemoGeneric: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.genericclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Class exists - updated_class = response.json() + updated_class = response # Update the class by adding a link new_link = { @@ -138,14 +134,14 @@ class DemoGeneric: # Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updated_class['reviewStatus'] = 'UNDER_REVIEW' - response = self.http_client.put( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', - json=updated_class) + response = self.client.genericclass().update( + resourceId=f'{issuer_id}.{class_suffix}', + body=updated_class).execute() print('Class update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END updateClass] @@ -164,19 +160,19 @@ class DemoGeneric: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.genericclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Class exists - existing_class = response.json() + existing_class = response # Patch the class by adding a link patch_body = {} @@ -195,57 +191,17 @@ class DemoGeneric: # Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for patches patch_body['reviewStatus'] = 'UNDER_REVIEW' - response = self.http_client.patch( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', json=patch_body) + response = self.client.genericclass().patch( + resourceId=f'{issuer_id}.{class_suffix}', + body=patch_body).execute() print('Class patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END patchClass] - # [START addMessageClass] - def add_class_message(self, issuer_id: str, class_suffix: str, header: str, - body: str) -> str: - """Add a message to a pass class. - - Args: - issuer_id (str): The issuer ID being used for this request. - class_suffix (str): Developer-defined unique ID for this pass class. - header (str): The message header. - body (str): The message body. - - Returns: - The pass class ID: f"{issuer_id}.{class_suffix}" - """ - - # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' - - response = self.http_client.post( - url=f'{self.class_url}/{issuer_id}.{class_suffix}/addMessage', - json={'message': { - 'header': header, - 'body': body - }}) - - print('Class addMessage response') - print(response.text) - - return response.json().get('id') - - # [END addMessageClass] - # [START createObject] def create_object(self, issuer_id: str, class_suffix: str, object_suffix: str) -> str: @@ -261,16 +217,15 @@ class DemoGeneric: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 200: + try: + self.client.genericobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' + else: print(f'Object {issuer_id}.{object_suffix} already exists!') - print(response.text) - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) return f'{issuer_id}.{object_suffix}' # See link below for more information on required properties @@ -354,12 +309,12 @@ class DemoGeneric: } # Create the object - response = self.http_client.post(url=self.object_url, json=new_object) + response = self.client.genericobject().insert(body=new_object).execute() print('Object insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END createObject] @@ -378,19 +333,19 @@ class DemoGeneric: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.genericobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - updated_object = response.json() + updated_object = response # Update the object by adding a link new_link = { @@ -401,14 +356,14 @@ class DemoGeneric: updated_object['linksModuleData'] = {'uris': []} updated_object['linksModuleData']['uris'].append(new_link) - response = self.http_client.put( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=updated_object) + response = self.client.genericobject().update( + resourceId=f'{issuer_id}.{object_suffix}', + body=updated_object).execute() print('Object update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END updateObject] @@ -425,19 +380,19 @@ class DemoGeneric: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.genericobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - existing_object = response.json() + existing_object = response # Patch the object by adding a link patch_body = {} @@ -452,14 +407,14 @@ class DemoGeneric: patch_body['linksModuleData'] = {'uris': []} patch_body['linksModuleData']['uris'].append(new_link) - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.genericobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END patchObject] @@ -479,72 +434,31 @@ class DemoGeneric: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.genericobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Patch the object, setting the pass as expired patch_body = {'state': 'EXPIRED'} - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.genericobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object expiration response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END expireObject] - # [START addMessageObject] - def add_object_message(self, issuer_id: str, object_suffix: str, - header: str, body: str) -> str: - """Add a message to a pass object. - - Args: - issuer_id (str): The issuer ID being used for this request. - object_suffix (str): Developer-defined unique ID for this pass object. - header (str): The message header. - body (str): The message body. - - Returns: - The pass class ID: f"{issuer_id}.{class_suffix}" - """ - - # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' - - response = self.http_client.post( - url=f'{self.object_url}/{issuer_id}.{object_suffix}/addMessage', - json={'message': { - 'header': header, - 'body': body - }}) - - print('Object addMessage response') - print(response.text) - - return response.json().get('id') - - # [END addMessageObject] - # [START jwtNew] def create_jwt_new_objects(self, issuer_id: str, class_suffix: str, object_suffix: str) -> str: @@ -774,7 +688,7 @@ class DemoGeneric: issuer_id (str): The issuer ID being used for this request. class_suffix (str): Developer-defined unique ID for this pass class. """ - data = '' + batch = self.client.new_batch_http_request() # Example: Generate three new pass objects for _ in range(3): @@ -861,25 +775,11 @@ class DemoGeneric: } } - data += '--batch_createobjectbatch\n' - data += 'Content-Type: application/json\n\n' - data += 'POST /walletobjects/v1/genericObject/\n\n' - - data += json.dumps(batch_object) + '\n\n' - - data += '--batch_createobjectbatch--' + batch.add(self.client.genericobject().insert(body=batch_object)) # Invoke the batch API calls - response = self.http_client.post( - url=self.batch_url, # https://walletobjects.googleapis.com/batch - data=data, - headers={ - # `boundary` is the delimiter between API calls in the batch request - 'Content-Type': - 'multipart/mixed; boundary=batch_createobjectbatch' - }) + response = batch.execute() - print('Batch insert response') - print(response.content.decode('UTF-8')) + print('Batch complete') # [END batch] diff --git a/python/demo_giftcard.py b/python/demo_giftcard.py index 020af1f..2b3b05e 100644 --- a/python/demo_giftcard.py +++ b/python/demo_giftcard.py @@ -21,7 +21,8 @@ import json import os import uuid -from google.auth.transport.requests import AuthorizedSession +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError from google.oauth2.service_account import Credentials from google.auth import jwt, crypt # [END imports] @@ -39,11 +40,6 @@ class DemoGiftCard: def __init__(self): self.key_file_path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/key.json') - self.base_url = 'https://walletobjects.googleapis.com/walletobjects/v1' - self.batch_url = 'https://walletobjects.googleapis.com/batch' - self.class_url = f'{self.base_url}/giftCardClass' - self.object_url = f'{self.base_url}/giftCardObject' - # Set up authenticated client self.auth() @@ -56,7 +52,7 @@ class DemoGiftCard: self.key_file_path, scopes=['https://www.googleapis.com/auth/wallet_object.issuer']) - self.http_client = AuthorizedSession(self.credentials) + self.client = build('walletobjects', 'v1', credentials=self.credentials) # [END auth] @@ -73,16 +69,16 @@ class DemoGiftCard: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 200: + try: + self.client.giftcardclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' + else: print(f'Class {issuer_id}.{class_suffix} already exists!') return f'{issuer_id}.{class_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' # See link below for more information on required properties # https://developers.google.com/wallet/retail/gift-cards/rest/v1/giftcardclass @@ -92,12 +88,12 @@ class DemoGiftCard: 'reviewStatus': 'UNDER_REVIEW' } - response = self.http_client.post(url=self.class_url, json=new_class) + response = self.client.giftcardclass().insert(body=new_class).execute() print('Class insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END createClass] @@ -116,19 +112,19 @@ class DemoGiftCard: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.giftcardclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Class exists - updated_class = response.json() + updated_class = response # Update the class by adding a homepage updated_class['homepageUri'] = { @@ -139,14 +135,14 @@ class DemoGiftCard: # Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updated_class['reviewStatus'] = 'UNDER_REVIEW' - response = self.http_client.put( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', - json=updated_class) + response = self.client.giftcardclass().update( + resourceId=f'{issuer_id}.{class_suffix}', + body=updated_class).execute() print('Class update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END updateClass] @@ -165,16 +161,16 @@ class DemoGiftCard: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.giftcardclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Patch the class by adding a homepage patch_body = { @@ -187,13 +183,14 @@ class DemoGiftCard: 'reviewStatus': 'UNDER_REVIEW' } - response = self.http_client.patch( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', json=patch_body) + response = self.client.giftcardclass().patch( + resourceId=f'{issuer_id}.{class_suffix}', + body=patch_body).execute() print('Class patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END patchClass] @@ -213,28 +210,28 @@ class DemoGiftCard: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') + try: + response = self.client.giftcardclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' - - response = self.http_client.post( - url=f'{self.class_url}/{issuer_id}.{class_suffix}/addMessage', - json={'message': { + response = self.client.giftcardclass().addmessage( + resourceId=f'{issuer_id}.{class_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Class addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END addMessageClass] @@ -253,16 +250,15 @@ class DemoGiftCard: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 200: + try: + self.client.giftcardobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' + else: print(f'Object {issuer_id}.{object_suffix} already exists!') - print(response.text) - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) return f'{issuer_id}.{object_suffix}' # See link below for more information on required properties @@ -334,12 +330,12 @@ class DemoGiftCard: } # Create the object - response = self.http_client.post(url=self.object_url, json=new_object) + response = self.client.giftcardobject().insert(body=new_object).execute() print('Object insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END createObject] @@ -358,19 +354,19 @@ class DemoGiftCard: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.giftcardobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - updated_object = response.json() + updated_object = response # Update the object by adding a link new_link = { @@ -381,14 +377,14 @@ class DemoGiftCard: updated_object['linksModuleData'] = {'uris': []} updated_object['linksModuleData']['uris'].append(new_link) - response = self.http_client.put( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=updated_object) + response = self.client.giftcardobject().update( + resourceId=f'{issuer_id}.{object_suffix}', + body=updated_object).execute() print('Object update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END updateObject] @@ -405,19 +401,19 @@ class DemoGiftCard: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.giftcardobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - existing_object = response.json() + existing_object = response # Patch the object by adding a link patch_body = {} @@ -432,14 +428,14 @@ class DemoGiftCard: patch_body['linksModuleData'] = {'uris': []} patch_body['linksModuleData']['uris'].append(new_link) - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.giftcardobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END patchObject] @@ -459,28 +455,28 @@ class DemoGiftCard: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.giftcardobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Patch the object, setting the pass as expired patch_body = {'state': 'EXPIRED'} - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.giftcardobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object expiration response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END expireObject] @@ -500,28 +496,28 @@ class DemoGiftCard: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') + try: + response = self.client.giftcardobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' - - response = self.http_client.post( - url=f'{self.object_url}/{issuer_id}.{object_suffix}/addMessage', - json={'message': { + response = self.client.giftcardobject().addmessage( + resourceId=f'{issuer_id}.{object_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Object addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END addMessageObject] @@ -746,7 +742,7 @@ class DemoGiftCard: issuer_id (str): The issuer ID being used for this request. class_suffix (str): Developer-defined unique ID for this pass class. """ - data = '' + batch = self.client.new_batch_http_request() # Example: Generate three new pass objects for _ in range(3): @@ -821,25 +817,11 @@ class DemoGiftCard: } } - data += '--batch_createobjectbatch\n' - data += 'Content-Type: application/json\n\n' - data += 'POST /walletobjects/v1/giftCardObject/\n\n' - - data += json.dumps(batch_object) + '\n\n' - - data += '--batch_createobjectbatch--' + batch.add(self.client.giftcardobject().insert(body=batch_object)) # Invoke the batch API calls - response = self.http_client.post( - url=self.batch_url, # https://walletobjects.googleapis.com/batch - data=data, - headers={ - # `boundary` is the delimiter between API calls in the batch request - 'Content-Type': - 'multipart/mixed; boundary=batch_createobjectbatch' - }) + response = batch.execute() - print('Batch insert response') - print(response.content.decode('UTF-8')) + print('Batch complete') # [END batch] diff --git a/python/demo_loyalty.py b/python/demo_loyalty.py index 3439b0a..a0b4656 100644 --- a/python/demo_loyalty.py +++ b/python/demo_loyalty.py @@ -21,7 +21,8 @@ import json import os import uuid -from google.auth.transport.requests import AuthorizedSession +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError from google.oauth2.service_account import Credentials from google.auth import jwt, crypt # [END imports] @@ -39,11 +40,6 @@ class DemoLoyalty: def __init__(self): self.key_file_path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/key.json') - self.base_url = 'https://walletobjects.googleapis.com/walletobjects/v1' - self.batch_url = 'https://walletobjects.googleapis.com/batch' - self.class_url = f'{self.base_url}/loyaltyClass' - self.object_url = f'{self.base_url}/loyaltyObject' - # Set up authenticated client self.auth() @@ -56,7 +52,7 @@ class DemoLoyalty: self.key_file_path, scopes=['https://www.googleapis.com/auth/wallet_object.issuer']) - self.http_client = AuthorizedSession(self.credentials) + self.client = build('walletobjects', 'v1', credentials=self.credentials) # [END auth] @@ -73,16 +69,16 @@ class DemoLoyalty: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 200: + try: + self.client.loyaltyclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' + else: print(f'Class {issuer_id}.{class_suffix} already exists!') return f'{issuer_id}.{class_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' # See link below for more information on required properties # https://developers.google.com/wallet/retail/loyalty-cards/rest/v1/loyaltyclass @@ -105,12 +101,12 @@ class DemoLoyalty: } } - response = self.http_client.post(url=self.class_url, json=new_class) + response = self.client.loyaltyclass().insert(body=new_class).execute() print('Class insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END createClass] @@ -129,19 +125,19 @@ class DemoLoyalty: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.loyaltyclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Class exists - updated_class = response.json() + updated_class = response # Update the class by adding a homepage updated_class['homepageUri'] = { @@ -152,14 +148,14 @@ class DemoLoyalty: # Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updated_class['reviewStatus'] = 'UNDER_REVIEW' - response = self.http_client.put( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', - json=updated_class) + response = self.client.loyaltyclass().update( + resourceId=f'{issuer_id}.{class_suffix}', + body=updated_class).execute() print('Class update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END updateClass] @@ -178,16 +174,16 @@ class DemoLoyalty: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.loyaltyclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Patch the class by adding a homepage patch_body = { @@ -200,13 +196,14 @@ class DemoLoyalty: 'reviewStatus': 'UNDER_REVIEW' } - response = self.http_client.patch( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', json=patch_body) + response = self.client.loyaltyclass().patch( + resourceId=f'{issuer_id}.{class_suffix}', + body=patch_body).execute() print('Class patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END patchClass] @@ -226,28 +223,28 @@ class DemoLoyalty: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') + try: + response = self.client.loyaltyclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' - - response = self.http_client.post( - url=f'{self.class_url}/{issuer_id}.{class_suffix}/addMessage', - json={'message': { + response = self.client.loyaltyclass().addmessage( + resourceId=f'{issuer_id}.{class_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Class addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END addMessageClass] @@ -266,16 +263,15 @@ class DemoLoyalty: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 200: + try: + self.client.loyaltyobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' + else: print(f'Object {issuer_id}.{object_suffix} already exists!') - print(response.text) - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) return f'{issuer_id}.{object_suffix}' # See link below for more information on required properties @@ -346,12 +342,12 @@ class DemoLoyalty: } # Create the object - response = self.http_client.post(url=self.object_url, json=new_object) + response = self.client.loyaltyobject().insert(body=new_object).execute() print('Object insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END createObject] @@ -370,19 +366,19 @@ class DemoLoyalty: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.loyaltyobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - updated_object = response.json() + updated_object = response # Update the object by adding a link new_link = { @@ -393,14 +389,14 @@ class DemoLoyalty: updated_object['linksModuleData'] = {'uris': []} updated_object['linksModuleData']['uris'].append(new_link) - response = self.http_client.put( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=updated_object) + response = self.client.loyaltyobject().update( + resourceId=f'{issuer_id}.{object_suffix}', + body=updated_object).execute() print('Object update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END updateObject] @@ -417,19 +413,19 @@ class DemoLoyalty: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.loyaltyobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - existing_object = response.json() + existing_object = response # Patch the object by adding a link patch_body = {} @@ -444,14 +440,14 @@ class DemoLoyalty: patch_body['linksModuleData'] = {'uris': []} patch_body['linksModuleData']['uris'].append(new_link) - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.loyaltyobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END patchObject] @@ -471,28 +467,28 @@ class DemoLoyalty: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.loyaltyobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Patch the object, setting the pass as expired patch_body = {'state': 'EXPIRED'} - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.loyaltyobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object expiration response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END expireObject] @@ -512,28 +508,28 @@ class DemoLoyalty: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') + try: + response = self.client.loyaltyobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' - - response = self.http_client.post( - url=f'{self.object_url}/{issuer_id}.{object_suffix}/addMessage', - json={'message': { + response = self.client.loyaltyobject().addmessage( + resourceId=f'{issuer_id}.{object_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Object addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END addMessageObject] @@ -770,7 +766,7 @@ class DemoLoyalty: issuer_id (str): The issuer ID being used for this request. class_suffix (str): Developer-defined unique ID for this pass class. """ - data = '' + batch = self.client.new_batch_http_request() # Example: Generate three new pass objects for _ in range(3): @@ -844,25 +840,11 @@ class DemoLoyalty: } } - data += '--batch_createobjectbatch\n' - data += 'Content-Type: application/json\n\n' - data += 'POST /walletobjects/v1/loyaltyObject/\n\n' - - data += json.dumps(batch_object) + '\n\n' - - data += '--batch_createobjectbatch--' + batch.add(self.client.loyaltyobject().insert(body=batch_object)) # Invoke the batch API calls - response = self.http_client.post( - url=self.batch_url, # https://walletobjects.googleapis.com/batch - data=data, - headers={ - # `boundary` is the delimiter between API calls in the batch request - 'Content-Type': - 'multipart/mixed; boundary=batch_createobjectbatch' - }) + response = batch.execute() - print('Batch insert response') - print(response.content.decode('UTF-8')) + print('Batch complete') # [END batch] diff --git a/python/demo_offer.py b/python/demo_offer.py index f70dd39..8aa3417 100644 --- a/python/demo_offer.py +++ b/python/demo_offer.py @@ -21,7 +21,8 @@ import json import os import uuid -from google.auth.transport.requests import AuthorizedSession +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError from google.oauth2.service_account import Credentials from google.auth import jwt, crypt # [END imports] @@ -39,11 +40,6 @@ class DemoOffer: def __init__(self): self.key_file_path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/key.json') - self.base_url = 'https://walletobjects.googleapis.com/walletobjects/v1' - self.batch_url = 'https://walletobjects.googleapis.com/batch' - self.class_url = f'{self.base_url}/offerClass' - self.object_url = f'{self.base_url}/offerObject' - # Set up authenticated client self.auth() @@ -56,7 +52,7 @@ class DemoOffer: self.key_file_path, scopes=['https://www.googleapis.com/auth/wallet_object.issuer']) - self.http_client = AuthorizedSession(self.credentials) + self.client = build('walletobjects', 'v1', credentials=self.credentials) # [END auth] @@ -73,16 +69,16 @@ class DemoOffer: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 200: + try: + self.client.offerclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' + else: print(f'Class {issuer_id}.{class_suffix} already exists!') return f'{issuer_id}.{class_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' # See link below for more information on required properties # https://developers.google.com/wallet/retail/offers/rest/v1/offerclass @@ -95,12 +91,12 @@ class DemoOffer: 'redemptionChannel': 'ONLINE' } - response = self.http_client.post(url=self.class_url, json=new_class) + response = self.client.offerclass().insert(body=new_class).execute() print('Class insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END createClass] @@ -119,19 +115,19 @@ class DemoOffer: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.offerclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Class exists - updated_class = response.json() + updated_class = response # Update the class by adding a homepage updated_class['homepageUri'] = { @@ -142,14 +138,14 @@ class DemoOffer: # Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updated_class['reviewStatus'] = 'UNDER_REVIEW' - response = self.http_client.put( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', - json=updated_class) + response = self.client.offerclass().update( + resourceId=f'{issuer_id}.{class_suffix}', + body=updated_class).execute() print('Class update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END updateClass] @@ -168,16 +164,16 @@ class DemoOffer: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.offerclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Patch the class by adding a homepage patch_body = { @@ -190,13 +186,14 @@ class DemoOffer: 'reviewStatus': 'UNDER_REVIEW' } - response = self.http_client.patch( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', json=patch_body) + response = self.client.offerclass().patch( + resourceId=f'{issuer_id}.{class_suffix}', + body=patch_body).execute() print('Class patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END patchClass] @@ -216,28 +213,28 @@ class DemoOffer: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') + try: + response = self.client.offerclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' - - response = self.http_client.post( - url=f'{self.class_url}/{issuer_id}.{class_suffix}/addMessage', - json={'message': { + response = self.client.offerclass().addmessage( + resourceId=f'{issuer_id}.{class_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Class addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END addMessageClass] @@ -256,16 +253,15 @@ class DemoOffer: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 200: + try: + self.client.offerobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' + else: print(f'Object {issuer_id}.{object_suffix} already exists!') - print(response.text) - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) return f'{issuer_id}.{object_suffix}' # See link below for more information on required properties @@ -336,12 +332,12 @@ class DemoOffer: } # Create the object - response = self.http_client.post(url=self.object_url, json=new_object) + response = self.client.offerobject().insert(body=new_object).execute() print('Object insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END createObject] @@ -360,19 +356,19 @@ class DemoOffer: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.offerobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - updated_object = response.json() + updated_object = response # Update the object by adding a link new_link = { @@ -383,14 +379,14 @@ class DemoOffer: updated_object['linksModuleData'] = {'uris': []} updated_object['linksModuleData']['uris'].append(new_link) - response = self.http_client.put( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=updated_object) + response = self.client.offerobject().update( + resourceId=f'{issuer_id}.{object_suffix}', + body=updated_object).execute() print('Object update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END updateObject] @@ -407,19 +403,19 @@ class DemoOffer: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.offerobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - existing_object = response.json() + existing_object = response # Patch the object by adding a link patch_body = {} @@ -434,14 +430,14 @@ class DemoOffer: patch_body['linksModuleData'] = {'uris': []} patch_body['linksModuleData']['uris'].append(new_link) - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.offerobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END patchObject] @@ -461,28 +457,28 @@ class DemoOffer: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.offerobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Patch the object, setting the pass as expired patch_body = {'state': 'EXPIRED'} - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.offerobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object expiration response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END expireObject] @@ -502,28 +498,28 @@ class DemoOffer: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') + try: + response = self.client.offerobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' - - response = self.http_client.post( - url=f'{self.object_url}/{issuer_id}.{object_suffix}/addMessage', - json={'message': { + response = self.client.offerobject().addmessage( + resourceId=f'{issuer_id}.{object_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Object addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END addMessageObject] @@ -750,7 +746,7 @@ class DemoOffer: issuer_id (str): The issuer ID being used for this request. class_suffix (str): Developer-defined unique ID for this pass class. """ - data = '' + batch = self.client.new_batch_http_request() # Example: Generate three new pass objects for _ in range(3): @@ -824,25 +820,11 @@ class DemoOffer: } } - data += '--batch_createobjectbatch\n' - data += 'Content-Type: application/json\n\n' - data += 'POST /walletobjects/v1/offerObject/\n\n' - - data += json.dumps(batch_object) + '\n\n' - - data += '--batch_createobjectbatch--' + batch.add(self.client.offerobject().insert(body=batch_object)) # Invoke the batch API calls - response = self.http_client.post( - url=self.batch_url, # https://walletobjects.googleapis.com/batch - data=data, - headers={ - # `boundary` is the delimiter between API calls in the batch request - 'Content-Type': - 'multipart/mixed; boundary=batch_createobjectbatch' - }) + response = batch.execute() - print('Batch insert response') - print(response.content.decode('UTF-8')) + print('Batch complete') # [END batch] diff --git a/python/demo_transit.py b/python/demo_transit.py index 2b4371f..57b04b3 100644 --- a/python/demo_transit.py +++ b/python/demo_transit.py @@ -21,7 +21,9 @@ import json import os import uuid -from google.auth.transport.requests import AuthorizedSession +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError +from googleapiclient.http import BatchHttpRequest from google.oauth2.service_account import Credentials from google.auth import jwt, crypt # [END imports] @@ -39,11 +41,6 @@ class DemoTransit: def __init__(self): self.key_file_path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/key.json') - self.base_url = 'https://walletobjects.googleapis.com/walletobjects/v1' - self.batch_url = 'https://walletobjects.googleapis.com/batch' - self.class_url = f'{self.base_url}/transitClass' - self.object_url = f'{self.base_url}/transitObject' - # Set up authenticated client self.auth() @@ -56,7 +53,7 @@ class DemoTransit: self.key_file_path, scopes=['https://www.googleapis.com/auth/wallet_object.issuer']) - self.http_client = AuthorizedSession(self.credentials) + self.client = build('walletobjects', 'v1', credentials=self.credentials) # [END auth] @@ -73,16 +70,16 @@ class DemoTransit: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 200: + try: + self.client.transitclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' + else: print(f'Class {issuer_id}.{class_suffix} already exists!') return f'{issuer_id}.{class_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' # See link below for more information on required properties # https://developers.google.com/wallet/tickets/transit-passes/qr-code/rest/v1/transitclass @@ -105,12 +102,12 @@ class DemoTransit: 'transitType': 'BUS' } - response = self.http_client.post(url=self.class_url, json=new_class) + response = self.client.transitclass().insert(body=new_class).execute() print('Class insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END createClass] @@ -129,19 +126,19 @@ class DemoTransit: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.transitclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Class exists - updated_class = response.json() + updated_class = response # Update the class by adding a homepage updated_class['homepageUri'] = { @@ -152,14 +149,14 @@ class DemoTransit: # Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updated_class['reviewStatus'] = 'UNDER_REVIEW' - response = self.http_client.put( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', - json=updated_class) + response = self.client.transitclass().update( + resourceId=f'{issuer_id}.{class_suffix}', + body=updated_class).execute() print('Class update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END updateClass] @@ -178,16 +175,16 @@ class DemoTransit: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') - - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' + try: + response = self.client.transitclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' # Patch the class by adding a homepage patch_body = { @@ -200,13 +197,14 @@ class DemoTransit: 'reviewStatus': 'UNDER_REVIEW' } - response = self.http_client.patch( - url=f'{self.class_url}/{issuer_id}.{class_suffix}', json=patch_body) + response = self.client.transitclass().patch( + resourceId=f'{issuer_id}.{class_suffix}', + body=patch_body).execute() print('Class patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END patchClass] @@ -226,28 +224,28 @@ class DemoTransit: """ # Check if the class exists - response = self.http_client.get( - url=f'{self.class_url}/{issuer_id}.{class_suffix}') + try: + response = self.client.transitclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Class {issuer_id}.{class_suffix} not found!') + return f'{issuer_id}.{class_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{class_suffix}' - if response.status_code == 404: - print(f'Class {issuer_id}.{class_suffix} not found!') - return f'{issuer_id}.{class_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{class_suffix}' - - response = self.http_client.post( - url=f'{self.class_url}/{issuer_id}.{class_suffix}/addMessage', - json={'message': { + response = self.client.transitclass().addmessage( + resourceId=f'{issuer_id}.{class_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Class addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{class_suffix}' # [END addMessageClass] @@ -266,16 +264,15 @@ class DemoTransit: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 200: + try: + self.client.transitobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code != 404: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' + else: print(f'Object {issuer_id}.{object_suffix} already exists!') - print(response.text) - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 404: - # Something else went wrong... - print(response.text) return f'{issuer_id}.{object_suffix}' # See link below for more information on required properties @@ -365,12 +362,12 @@ class DemoTransit: } # Create the object - response = self.http_client.post(url=self.object_url, json=new_object) + response = self.client.transitobject().insert(body=new_object).execute() print('Object insert response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END createObject] @@ -389,19 +386,19 @@ class DemoTransit: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.transitobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - updated_object = response.json() + updated_object = response # Update the object by adding a link new_link = { @@ -412,14 +409,14 @@ class DemoTransit: updated_object['linksModuleData'] = {'uris': []} updated_object['linksModuleData']['uris'].append(new_link) - response = self.http_client.put( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=updated_object) + response = self.client.transitobject().update( + resourceId=f'{issuer_id}.{object_suffix}', + body=updated_object).execute() print('Object update response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END updateObject] @@ -436,19 +433,19 @@ class DemoTransit: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.transitobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Object exists - existing_object = response.json() + existing_object = response # Patch the object by adding a link patch_body = {} @@ -463,14 +460,14 @@ class DemoTransit: patch_body['linksModuleData'] = {'uris': []} patch_body['linksModuleData']['uris'].append(new_link) - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.transitobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object patch response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END patchObject] @@ -490,28 +487,28 @@ class DemoTransit: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') - - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' + try: + response = self.client.transitobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' # Patch the object, setting the pass as expired patch_body = {'state': 'EXPIRED'} - response = self.http_client.patch( - url=f'{self.object_url}/{issuer_id}.{object_suffix}', - json=patch_body) + response = self.client.transitobject().patch( + resourceId=f'{issuer_id}.{object_suffix}', + body=patch_body).execute() print('Object expiration response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END expireObject] @@ -531,28 +528,28 @@ class DemoTransit: """ # Check if the object exists - response = self.http_client.get( - url=f'{self.object_url}/{issuer_id}.{object_suffix}') + try: + response = self.client.transitobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() + except HttpError as e: + if e.status_code == 404: + print(f'Object {issuer_id}.{object_suffix} not found!') + return f'{issuer_id}.{object_suffix}' + else: + # Something else went wrong... + print(e.error_details) + return f'{issuer_id}.{object_suffix}' - if response.status_code == 404: - print(f'Object {issuer_id}.{object_suffix} not found!') - return f'{issuer_id}.{object_suffix}' - elif response.status_code != 200: - # Something else went wrong... - print(response.text) - return f'{issuer_id}.{object_suffix}' - - response = self.http_client.post( - url=f'{self.object_url}/{issuer_id}.{object_suffix}/addMessage', - json={'message': { + response = self.client.transitobject().addmessage( + resourceId=f'{issuer_id}.{object_suffix}', + body={'message': { 'header': header, 'body': body - }}) + }}).execute() print('Object addMessage response') - print(response.text) + print(response) - return response.json().get('id') + return f'{issuer_id}.{object_suffix}' # [END addMessageObject] @@ -808,7 +805,7 @@ class DemoTransit: issuer_id (str): The issuer ID being used for this request. class_suffix (str): Developer-defined unique ID for this pass class. """ - data = '' + batch = self.client.new_batch_http_request() # Example: Generate three new pass objects for _ in range(3): @@ -901,25 +898,11 @@ class DemoTransit: } } - data += '--batch_createobjectbatch\n' - data += 'Content-Type: application/json\n\n' - data += 'POST /walletobjects/v1/transitObject/\n\n' - - data += json.dumps(batch_object) + '\n\n' - - data += '--batch_createobjectbatch--' + batch.add(self.client.transitobject().insert(body=batch_object)) # Invoke the batch API calls - response = self.http_client.post( - url=self.batch_url, # https://walletobjects.googleapis.com/batch - data=data, - headers={ - # `boundary` is the delimiter between API calls in the batch request - 'Content-Type': - 'multipart/mixed; boundary=batch_createobjectbatch' - }) + response = batch.execute() - print('Batch insert response') - print(response.content.decode('UTF-8')) + print('Batch complete') # [END batch]