Package passtools :: Module pt_pass
[hide private]
[frames] | no frames]

Source Code for Module passtools.pt_pass

  1  ########################################## 
  2  # pt_pass.py 
  3  # 
  4  # Models PassTools Pass 
  5  # 
  6  # Copyright 2013, Urban Airship, Inc. 
  7  ########################################## 
  8   
  9  """ 
 10  Define and provide methods for manipulating PassTools Pass objects. 
 11   
 12  """ 
 13   
 14  try: 
 15      import simplejson as json 
 16  except ImportError: 
 17      import json 
 18   
 19  import pt_client 
 20  from passtools import PassTools 
21 22 23 -class Pass(object):
24 - def __init__(self, template_id=None, template_fields_model_dict=None):
25 """ 26 Init, optionally populate, new pt_pass.Pass instance 27 If template_id and template_fields_model are supplied, will create new complete instance, 28 else just create empty instance. 29 30 API call used is v1/pass/<template_id> (POST) 31 32 @type template_id: int 33 @param template_id: ID of the template used to create new pass [Optional] 34 @type template_fields_model_dict: dict 35 @param template_fields_model_dict: template_fields_model dict of the template used to create new pass [Optional] 36 @return: None 37 """ 38 self.pass_dict = None 39 if template_id and template_fields_model_dict is not None: 40 new_pass = self.create(template_id, template_fields_model_dict) 41 if new_pass: 42 self.pass_dict = new_pass.pass_dict
43 44 @property
45 - def id(self):
46 the_id = None 47 if self.pass_dict and "id" in self.pass_dict: 48 the_id = int(self.pass_dict["id"]) 49 return the_id
50 51 @property
52 - def template_id(self):
53 the_id = None 54 if self.pass_dict and "template_id" in self.pass_dict: 55 the_id = self.pass_dict["template_id"] 56 return the_id
57
58 - def __str__(self):
59 return json.dumps(self.pass_dict, sort_keys=True, indent=2, encoding="ISO-8859-1")
60 61 @classmethod
62 - def create(cls, template_id, template_fields_model_dict):
63 """ 64 Create new Pass from specified template. 65 66 API call used is v1/pass/<template_id> (POST) 67 68 @type template_id: int 69 @param template_id: ID of the template used to create new pass 70 @type template_fields_model_dict: dict 71 @param template_fields_model_dict: template_fields_model dict of the template used to create new pass 72 @return: json form of template full-form description 73 """ 74 75 request_url = "/pass/%d" % int(template_id) 76 request_dict = {"json": json.dumps(template_fields_model_dict, encoding="ISO-8859-1")} 77 return pt_client.pt_post(request_url, request_dict)
78 79 @classmethod
80 - def update(cls, pass_id, update_fields):
81 """ 82 Update existing pass 83 84 API call used is v1/pass/<pass_id> (PUT) 85 86 @type pass_id: int 87 @param pass_id: ID of desired Pass 88 @type update_fields: dict 89 @param update_fields: Pass.pass_dict dict 90 @return: json form of template full-form description 91 """ 92 93 request_url = "/pass/%d" % int(pass_id) 94 request_dict = {"json": json.dumps(update_fields, encoding="ISO-8859-1")} 95 return pt_client.pt_put(request_url, request_dict)
96 97 @classmethod
98 - def push_update(cls, pass_id):
99 """ 100 Update installed passes using push method 101 102 API call used is v1/pass/<pass_id>/push (PUT) 103 104 @type pass_id: int 105 @param pass_id: ID of desired Pass 106 @return: Response data 107 """ 108 109 request_url = "/pass/%d/push" % int(pass_id) 110 return pt_client.pt_put(request_url)
111 112 @classmethod
113 - def get(cls, pass_id):
114 """ 115 Retrieve existing pass with specified ID 116 117 API call used is v1/pass/<pass_id> (GET) 118 119 @type pass_id: int 120 @param pass_id: ID of desired Pass 121 @return: json form of template full-form description 122 """ 123 124 request_url = "/pass/%d" % int(pass_id) 125 return pt_client.pt_get(request_url, {})
126 127 @classmethod
128 - def list(cls, **kwargs):
129 """ 130 Retrieve list of existing passes created by owner of API-key 131 If template_id is specified, retrieve only passes associated with that template 132 Other parameters are translated into query-modifiers 133 134 Note that list() returns abbreviated form of passes. Use get() to retrieve full pass. 135 136 API call used is v1/pass (GET) 137 138 @type templateId: int 139 @param templateId: ID of the template used to create new pass 140 @type pageSize: int 141 @param pageSize: Maximum length of list to return [Optional; Default = 10] 142 @type page: int 143 @param page: 1-based index of page into list, based on page_size [Optional; Default = 1] 144 @type order: string 145 @param order: Name of field on which to sort list [Optional; From (ID, Name, Created, Updated)] 146 @type direction: string 147 @param direction: Direction which to sort list [Optional; From (ASC, DESC); Default = DESC] 148 @return: json form of list of pass header descriptions 149 """ 150 151 request_dict = kwargs 152 request_url = "/pass" 153 return pt_client.pt_get(request_url, request_dict)
154 155 @classmethod
156 - def download(cls, pass_id, destination_path):
157 """ 158 Download pkpass file corresponding to existing pass with specified ID 159 160 API call used is v1/pass/<pass_id>/download (GET) 161 162 @type pass_id: int 163 @param pass_id: ID of desired Pass 164 @type destination_path: str 165 @param destination_path: path to receive pass file. Path must exist, and filename must end with ".pkpass" 166 @return: Writes pass to filesystem 167 """ 168 169 request_url = "/pass/%d/download" % int(pass_id) 170 resp = pt_client.pt_get(request_url) 171 if PassTools.test_mode: 172 return resp 173 elif resp is not None: 174 fh = open(destination_path, "wb") 175 fh.write(resp.content) 176 fh.close()
177 178 @classmethod
179 - def delete(cls, pass_id):
180 """ 181 delete existing pass 182 183 API call used is v1/pass/<pass_id> (DELETE) 184 185 @type pass_id: int 186 @param pass_id: ID of Pass to delete 187 @return: json form of response data 188 """ 189 190 request_url = "/pass/%d" % int(pass_id) 191 return pt_client.pt_delete(request_url, {})
192 193 @classmethod
194 - def add_locations(cls, pass_id, location_list):
195 """ 196 add locations to an existing pass 197 198 API call used is v1/pass/<pass_id>/locations (POST) 199 200 @type pass_id: int 201 @param pass_id: ID of the pass to add locations to 202 @type location_list: list 203 @param location_list: list of locations to add 204 @return: json form of response data 205 """ 206 207 request_url = "/pass/%d/locations" % int(pass_id) 208 request_dict = {"json": json.dumps(location_list, encoding="ISO-8859-1")} 209 return pt_client.pt_post(request_url, request_dict)
210 211 @classmethod
212 - def delete_location(cls, pass_id, location_id):
213 """ 214 delete existing location from pass 215 216 API call used is v1/pass/<pass_id>/location/<location_id> (DELETE) 217 218 @type pass_id: int 219 @param pass_id: ID of the pass to delete from 220 @type location_id: int 221 @param location_id: ID of the location to delete 222 @return: json form of response data 223 """ 224 225 request_url = "/pass/%d/location/%d" % (int(pass_id), int(location_id)) 226 return pt_client.pt_delete(request_url, {})
227