# STEP 1: Retrieve your API key from your Account view on the PassTools website
my_api_key = "your-key-goes-in-here"

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

# Pick the pass to work on
# This example script is more interesting if you installed that pass on multiple phones.
test_pass_id = your-pass-id-goes-here

# Retrieve the full form of that pass
print 25*"#"
print "Retrieving Pass #%s" % test_pass_id
get_response = Pass.get(test_pass_id)

print "Retrieved Pass..."
print get_response
print 25*"#"

# Now update the pass...
# Make a copy of the fields to operate on
working_copy = copy.deepcopy(get_response["passFields"])

# modify fields here:
working_copy["primary1"]["value"] = "Push Happens!"
working_copy["secondary1"]["value"] = "Push Happens Again!"

# Call 'update', passing the modifications as input.
update_response = Pass.update(test_pass_id, working_copy)
# 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!
# You'll always configure the api, providing your api key.
# This is required!
PassTools.configure(api_key = my_api_key)

# Let's create a new pass from a template.
# Start by retrieving a template using the same method used in Ex1_Templates.py
# The first two steps are a bit contrived, since you would know the ID of the template
# you wanted to use, but for this example we'll get the whole list and use the latest
list_response = Template.list()
template_header_list = list_response["templateHeaders"]
the_template_id = template_header_list[0]['id']

get_response = Template.get(the_template_id)

# Now create a new pass from the template.
test_pass = Pass.create(the_template_id, get_response['fieldsModel'])

print 25*"#"
print "New Pass at start"
print test_pass
print 25*"#"

# Add a location to that pass. Locations are passed as a list of dicts, length 1 or more
# A pass can have a maximum of 10 locations
print 25*"#"
print "Adding locations to new pass..."
location_list_1=[{"latitude":37.4471107, "longitude":-122.16206219999998,
                    "streetAddress1":"408 Florence St", "streetAddress2":"",
                    "city":"Palo Alto", "region":"CA", "regionCode":"94301",
                    "country":"US", "relevantText":"Palo Alto Office!"}]
add_response = Pass.add_locations(test_pass['id'], location_list_1)
Example #4
0
# STEP 2:
# You'll always configure the api, providing your api key.
# This is required!
PassTools.configure(api_key = my_api_key)

# First, we'll use 'list' to retrieve a list of pass headers we own (in abbreviated format)
# Note that, as with templates, default sort order of the list is most-recent-first
# and default page-size = 10
print 25*"#"
print "Retrieve default list of existing Passes owned by this user"
print "Note that the list is accompanied by some meta-info about total pass count, page-size, etc."
print "And that the list of pass descriptions proper is under the 'Passes' key\n"
print "Note also that you can retrieve passes associated with a specific template"

list_response = Pass.list()
print "Total count of passes for this user:"******"Page size", list_response['PageSize']
print "Page number", list_response['Page']
print "Order by", list_response['OrderField']
print "Order direction", list_response['OrderDirection']
for item in list_response['Passes']:
    print item
print 25*"#", "\n"

# Next, we'll retrieve the full form of a pass
# You might retrieve a pass, for example, in preparation for updating it.
# Normally, you'd know the ID of the pass you wanted--we'll just grab an id from the list above
print 25*"#"
the_pass_id = int(list_response['Passes'][0]['id'])

#######################
# PASSES:
#######################
# Now a few examples with passes. Same methods hold.

# First, we'll use 'list' to retrieve a default list of passes we own (in abbreviated format)
# Note that, as with templates, default sort order of the list is most-recent-first.
print 25*"#"
print "Retrieve default list of existing Passes owned by this user"
print "Note that the list is accompanied by some meta-info about total pass count, page-size, etc."
print "And that the list of pass descriptions proper is under the 'Passes' key\n"
print "Note also that you can retrieve passes associated with a specific template"

list_response = Pass.list()
report_passes(list_response)
print 25*"#", "\n"


print 25*"#"
print "Retrieve second page of existing Passes, 15 per page, owned by this user, ascending sort by creation date"
list_response = Pass.list(pageSize=15, page=2, direction="desc", order="created")
report_passes(list_response)
print 25*"#", "\n"

# Let's get passes associated with a specific template
# You would normally know which template ID you were interested in, but we'll just grab a known-good one
the_template_id = int(list_response["Passes"][-1]["templateId"])
print 25*"#"
print "Retrieve 2nd page of 3-per-page passes associated with template %s" % the_template_id
from passtools import PassTools
from passtools.pt_pass import Pass

# API User:
# STEP 1: Retrieve your API key from your Account view on the PassTools website
my_api_key = "your-key-goes-in-here"

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

# Retrieve the current form of the pass, using the pass ID.
current_pass_id = 2039
starting_pass = Pass.get(current_pass_id)

# Let's a copy of the fields to operate on
pass_fields = copy.deepcopy(starting_pass["passFields"])

print "Starting:"
print "Offer:", pass_fields["offer"]["value"]
print "Exp_date:", pass_fields["exp_date"]["value"]

# Now set the new data. We're going to imagine that our initial offer just expired, and we're setting a '15% off'
# offer to extend 'til the end of the year:

# NOTE: date values must be passed in iso-8601 format
pass_fields["exp_date"]["value"] = "2013-01-01T12:01Z"
pass_fields["offer"]["value"] = "15% Off!!!"
# You'll always configure the api, providing your api key.
# This is required!
PassTools.configure(api_key = my_api_key)

# Let's create a new pass from a template.
# Start by retrieving a template using the same method used in Ex1_Templates.py
# This is a bit contrived, since you would probably know the ID of the template
# you wanted to use, but for this example we'll get the whole list and use the latest
list_response = Template.list()
template_header_list = list_response["templateHeaders"]
the_template_id = template_header_list[0]['id']

get_response = Template.get(the_template_id)

# Now create a new pass from the template.
test_pass = Pass.create(the_template_id, get_response['fieldsModel'])

print 25*"#"
print "New Pass at start"
print test_pass
print 25*"#"

# And let's update the "relevantDate" field in that pass
# Note that as of this writing, the RelevantDate argument is not format-validated
# so you _must_ ensure that the date you pass in confirms to ISO-8601
pass_fields = copy.deepcopy(test_pass["passFields"])
print 25*"#"
print "Start pass update..."
if "relevantDate" in pass_fields:
    pass_fields["relevantDate"]["value"] = "2012-01-01T12:00-08:00"