# STEP 2:
# You'll always configure the api, providing your api key.
# This is required!
PassTools.configure(api_key = my_api_key)

# Our model DB...
user_db = [{"first_name": "James", "last_name":"Bond"},
           {"first_name": "Jimi", "last_name":"Hendrix"},
           {"first_name": "Johnny", "last_name":"Appleseed"}]

# You'll have selected the template you want to use...you can find the template ID in the Template Builder UI
selected_template_id = 604

# Retrieve your template, so you can modify the data and create passes from it
get_response = Template.get(selected_template_id)

the_fields_model = get_response["fieldsModel"]

# Now for each user in your DB, grab the user data, modify the template.fields_model, create a pass and download it:
for user_record in user_db:
    the_fields_model["fname"]["value"] = user_record["first_name"]
    the_fields_model["lname"]["value"] = user_record["last_name"]

    create_response = Pass.create(selected_template_id, the_fields_model)

    new_pass_id = create_response["id"]
    print "NEW PASS CREATED. ID: %s, First: %s, Last: %s" % (new_pass_id, user_record["first_name"], user_record["last_name"])
    Pass.download(new_pass_id, "/tmp/%s_%s.pkpass" % (user_record["first_name"], user_record["last_name"]))

# Now distribute the passes to your users!
Ejemplo n.º 2
0
print 25*"#"
print "Creating new Pass from template ID %s" % the_template_id
create_response = Pass.create(the_template_id, the_template_fields_model)
print create_response
print 25*"#"

# There are a few ways to deliver passes to customers (and more to come), several of which involve you distributing
# the pass file itself...so you'll want to download passes you create.
# Let's do that, using the 'download' method of a pass.
# IMPORTANT: you'll want to be sure to give your passes the '.pkpass' extension, or they will not be properly recognized
# when your customer receives them.
print 25*"#"
new_pass_id = create_response['id']
print "Downloading an id-specified Pass from the service..."
Pass.download(new_pass_id, "/tmp/New_Pass.pkpass")
print 25*"#"

# Next, we'll update an existing pass, using--surprise!--the 'update' method. In this case, we use the fields
# from the existing pass, modify them, and call update. In typical usage, you might call 'get' above to retrieve a
# pass to use as input...we'll the pass we just created, so the script output will allow you to compare before/after update.
# Make a copy of the fields to operate on

pass_fields = copy.deepcopy(create_response["passFields"])
print 25*"#"
print "Start pass update test..."
# modify fields here...as an example:
if "primary1" in pass_fields:
    pass_fields["primary1"]["value"] = "Updated Primary!"
else:
    print "No primary1 to update"