@@ -9,40 +9,39 @@ def __init__(self, resource):
99 """
1010 Initializes an instance of Examples. You can try the different examples in this class.
1111 To do so, initialize the class and then call one of the functions.
12- :param resource: An instance of SpontitResource
12+ :param resource: an instance of SpontitResource
1313 """
1414 assert type (resource ) == SpontitResource
1515 self .__resource = resource
1616
1717 def simple_push_example (self ):
1818 """
1919 Sends a simple push notification.
20- :return: The result of the call to push
20+ :return: the result of the call to push
2121 """
2222 return self .__resource .push ("Hello!" )
2323
2424 def scheduled_push (self ):
2525 """
2626 Schedules a push notification for one hour from the current time.
27- :return: The result of the call to push
27+ :return: the result of the call to push
2828 """
29- # 3600 seconds in one hour
3029 return self .__resource .push ("Hello, in 1 hour" ,
3130 schedule_time_stamp = int (time .time ()) + 3600 )
3231
3332 def immediate_expiration_push (self ):
3433 """
3534 Sends a push notification and sets the expiration for the current time. Because our expiration function
3635 polls approximately every 15-30 minutes, the notification will delete within 30 minutes.
37- :return: The result of the call to push
36+ :return: the result of the call to push
3837 """
3938 return self .__resource .push ("Hello, expire ASAP (within 15 minutes)" ,
4039 expiration = SpontitResource .Expiration (0 , 0 , 0 ))
4140
4241 def expire_one_hour_after_schedule (self ):
4342 """
4443 Schedules a push notification for one hour from now, and then expires it one hour later.
45- :return: The result of the call to push
44+ :return: the result of the call to push
4645 """
4746 return self .__resource .push ("Hello, in 1 hour. Bye in 2." ,
4847 schedule_time_stamp = int (time .time ()) + 3600 ,
@@ -52,18 +51,18 @@ def rate_limit_example(self):
5251 """
5352 Spontit currently only supports sending one push notification per second.
5453 This function throttles that limit as an example of what you can expect if you exceed that limit.
55- :return: The final response of the 10 calls to push.
54+ :return: the final response of the 10 calls to push.
5655 """
57- response = None
56+ response_received = None
5857 for _ in range (10 ):
59- response = self .simple_push_example ()
60- print (response )
61- return response
58+ response_received = self .simple_push_example ()
59+ print (response_received )
60+ return response_received
6261
6362 def subtitle_body_example (self ):
6463 """
6564 Sends a push notification with a subtitle and a body.
66- :return: The result of the call to push
65+ :return: the result of the call to push
6766 """
6867 return self .__resource .push ("Hello!" ,
6968 subtitle = "An API Notice" ,
@@ -81,7 +80,7 @@ def post_a_link_ex_1(self):
8180 """
8281 Sends a push notification with a link to Jack Dorsey's first tweet. should_open_link_in_app is set to False
8382 so that the Twitter app opens (rather than opening twitter.com within the Spontit app).
84- :return: The result of the call to push
83+ :return: the result of the call to push
8584 """
8685 return self .__resource .push ("Jack's first tweet." ,
8786 link = "https://twitter.com/jack/status/20" ,
@@ -95,7 +94,7 @@ def post_a_link_ex_1(self):
9594 def post_a_link_ex_2 (self ):
9695 """
9796 Sends a push notification with a link to Amazon.com.
98- :return: The result of the call to push
97+ :return: the result of the call to push
9998 """
10099 return self .__resource .push ("Buy from Amazon." ,
101100 link = "https://amazon.com" )
@@ -104,17 +103,17 @@ def post_a_link_ex_3(self):
104103 """
105104 Sends a push notification linking the Spontit review compose window in the App Store. should_open_link_in_app is
106105 set to False so that the App Store opens.
107- :return: The result of the call to push
106+ :return: the result of the call to push
108107 """
109- return self .__resource .push ("Please give Spontit 5 stars!! !" ,
108+ return self .__resource .push ("Please rate Spontit in the App Store !" ,
110109 link = "https://itunes.apple.com/app/id1448318683?action=write-review" ,
111110 should_open_link_in_app = False )
112111
113112 def post_an_ios_deep_link_ex_1 (self ):
114113 """
115114 Sends a push notification deep linking to Stocks app.
116115 Only works for iOS Spontit App, >= v6.0.1
117- :return: The result of the call to push
116+ :return: the result of the call to push
118117 """
119118 return self .__resource .push ("Open the stocks app." ,
120119 ios_deep_link = "stocks://" )
@@ -123,106 +122,183 @@ def post_an_ios_deep_link_ex_2(self):
123122 """
124123 Sends a push notification deep linking to the gallery tab of the Shortcuts app.
125124 Only works for iOS Spontit App, >= v6.0.1
126- :return: The result of the call to push
125+ :return: the result of the call to push
127126 """
128127 return self .__resource .push ("Open the gallery tab of the Shortcuts app." ,
129128 ios_deep_link = "shortcuts://gallery" )
130129
131130 def post_an_ios_deep_link_ex_3 (self ):
132131 """
133132 Pushes a notification that deep links to the Spontit review creation window in the App Store.
134- :return: The result of the call to push
133+ :return: the result of the call to push
135134 """
136- return self .__resource .push ("Please give Spontit 5 stars!! !" ,
135+ return self .__resource .push ("Please rate Spontit in the App Store !" ,
137136 ios_deep_link = "itms-apps://itunes.apple.com/app/id1448318683?action=write-review" )
138137
139138 def create_new_channel (self ):
140139 """
141- Creates a new channel
142- :return: The new channel
140+ Creates a new channel.
141+ :return: the new channel
143142 """
144- return self .__resource .create_channel ("My First Custom Channel" )
143+ response = self .__resource .create_channel ("My First Custom Channel" )
144+ return Examples .__get_channel_from_response (response )
145145
146146 def create_new_channel_with_category (self ):
147147 """
148148 Creates a new channel with a category code of 0.
149- :return: The new channel
149+ :return: the new channel
150150 """
151- available_categories = self .__resource .get_channel_categories ()
151+ available_categories = self .__resource .get_categories ()
152152 category_code = 0
153153 if 'data' in available_categories and len (available_categories ['data' ]) > 0 :
154154 category_code = available_categories ['data' ][0 ].get ('categoryCode' , 0 )
155155
156156 new_display_name = "My New Channel"
157- return self .__resource .create_channel (new_display_name , category_code )
157+ response = self .__resource .create_channel (new_display_name , int (category_code ))
158+ return Examples .__get_channel_from_response (response )
159+
160+ def create_new_channel_with_profile_image_and_push_to_it (self ):
161+ """
162+ Creates a new channel, uploads a profile image, and sends a simple push notification to it.
163+ You should see the profile image in the push notification.
164+ :return: the result of the push notification request
165+ """
166+ new_channel_display_name = "Profile Image Channel"
167+ _ = self .__resource .create_channel (new_channel_display_name )
158168
159- def create_new_channel_and_push_to_it (self ):
169+ import os
170+ cwd = os .getcwd () # Get the current working directory (cwd)
171+ print (cwd )
172+ files = os .listdir (cwd ) # Get all the files in that directory
173+ print (files )
174+
175+ response = self .__resource .channel_profile_image_upload (
176+ image_path = "examples/orange.png" ,
177+ is_png = True ,
178+ channel_name = new_channel_display_name
179+ )
180+ self .__resource .push (
181+ "Hello new channel!" ,
182+ channel_name = new_channel_display_name
183+ )
184+ print ("After uploading a profile image, you receive a URL that you can use to confirm the upload succeeded." )
185+ print ("You can also view the result of the push. You should see the channel image in the push, provided you "
186+ "have good connection." )
187+ print (response )
188+
189+ def create_new_channel_and_update_its_settings (self ):
160190 """
161191 Creates a new channel and sends a simple push notification to it.
162- :return: The result of the push notification
192+ :return: the result of the push notification request
163193 """
164194 new_channel_display_name = "Push Push Channel"
165195 _ = self .__resource .create_channel (new_channel_display_name )
196+ self .__resource .update_channel (
197+ new_channel_display_name ,
198+ add_all_followers = True ,
199+ auto_add_future_followers = True ,
200+ category_code = 1
201+ )
166202
167- # The channel might not have been completely initialized just yet. It is generally recommended to create your
168- # channels prior to pushing. Sleeping is far less than ideal, and we will work on a better solution to
169- # create channels on the fly. If you have any requests, please create an issue in the repo.
170- time .sleep (1.0 )
171-
172- mapping = self .__resource .get_display_name_to_channel_id_mapping ()
173- if 'data' in mapping :
174- channel_id_to_push_to = mapping ['data' ].get (new_channel_display_name , None )
175- if channel_id_to_push_to is not None :
176- return self .__resource .push ("Hello!" ,
177- channel_id = channel_id_to_push_to )
178- return {
179- "errors" : [{
180- "message" : "Channel not found."
181- }]
182- }
203+ def create_and_then_delete_channel (self ):
204+ """
205+ Creates a channel and then deletes it.
206+ :return: the channel (before deletion)
207+ """
208+ new_channel_display_name = "Invite Me Channel"
209+ response = self .__resource .create_channel (new_channel_display_name )
210+ print ("Delete Response:" , self .__resource .delete_channel (new_channel_display_name ))
211+ return Examples .__get_channel_from_response (response )
183212
184213 def create_new_channel_and_get_invite_options (self ):
185214 """
186- Creates a new channel and gets the invite options.
187- :return: The ways to invite someone to the channel, if the channel exists
215+ Creates a new channel and print out the item. Embedded in the channel item are the invite options
216+ :return: the channel item
188217 """
189218 new_channel_display_name = "Invite Me Channel"
190- _ = self .__resource .create_channel (new_channel_display_name )
191-
192- # The channel might not have been completely initialized just yet. It is generally recommended to create your
193- # channels prior to pushing. Sleeping is far less than ideal, and we will work on a better solution to
194- # create channels on the fly. If you have any requests, please create an issue in the repo.
195- time .sleep (1.0 )
196-
197- mapping = self .__resource .get_display_name_to_channel_id_mapping ()
198- if 'data' in mapping :
199- channel_id_to_invite_to = mapping ['data' ].get (new_channel_display_name , None )
200- if channel_id_to_invite_to is not None :
201- return self .__resource .get_invite_options (channel_id_to_invite_to )
202- return {
203- "errors" : [{
204- "message" : "Channel not found."
205- }]
206- }
219+ response = self .__resource .create_channel (new_channel_display_name )
220+ return Examples .__get_channel_from_response (response )
207221
208222 def get_invite_options_for_my_main_account (self ):
209223 """
210- Gets the invite options for your main channel.
211- :return: The response
224+ Get the main channel. Embedded in the main channel are the invite options
225+ :return: the main channel (your account)
226+ """
227+ response = self .__resource .get_channel ()
228+ return Examples .__get_channel_from_response (response )
229+
230+ def list_my_channels (self ):
231+ """
232+ List all created channels
233+ :return: the channels
212234 """
213- return self .__resource .get_invite_options ()
235+ response = self .__resource .get_channels ()
236+ return Examples .__get_channel_from_response (response )
237+
238+ @staticmethod
239+ def __get_channel_from_response (response ):
240+ """
241+ Print the channel from the responses. This function assumes a response is passed that contains "data". If not,
242+ it catches the KeyError and prints the error
243+ :param response: the response received from the Channel endpoint
244+ :return: either the new channel item or None
245+ """
246+ try :
247+ new_channel = response ['data' ]
248+ print (new_channel )
249+ return new_channel
250+ except KeyError :
251+ print (response )
252+ return None
253+
254+ def do_everything (self ):
255+ """
256+ Runs every example. Are you ready?
257+ :return:
258+ """
259+ example_functions = [
260+ self .simple_push_example ,
261+ self .scheduled_push ,
262+ self .immediate_expiration_push ,
263+ self .expire_one_hour_after_schedule ,
264+ self .rate_limit_example ,
265+ self .subtitle_body_example ,
266+ self .post_a_link_ex_1 ,
267+ self .post_a_link_ex_2 ,
268+ self .post_an_ios_deep_link_ex_3 ,
269+ self .create_new_channel ,
270+ self .create_new_channel_with_category ,
271+ self .create_new_channel_with_profile_image_and_push_to_it ,
272+ self .create_new_channel_and_update_its_settings ,
273+ self .create_and_then_delete_channel ,
274+ self .create_new_channel_and_get_invite_options ,
275+ self .get_invite_options_for_my_main_account ,
276+ self .list_my_channels
277+ ]
278+
279+ for func in example_functions :
280+ # Added time.sleep(1) because of the rate limit. Only one push per second per channel.
281+ # (e.g. You can have two pushes in the same second if each goes to a different channel.)
282+ # See rate_limit_example
283+ print (func .__name__ )
284+ func ()
285+ time .sleep (1 )
286+ print ("" )
214287
215288
216289if __name__ == "__main__" :
217290 # Try an example...
218291 spontit_src = SpontitResource ("my_username" , "my_secret_key" )
219292 example_instance = Examples (spontit_src )
220- response = example_instance .simple_push_example ()
221- print ("Simple push example result: " + str (response ))
293+ push_response = example_instance .simple_push_example ()
294+ print ("Simple push example result: " + str (push_response ))
295+
296+ # ...or be bold...
297+ example_instance .do_everything ()
222298
223299 # ...or get right to pushing!
224- response = spontit_src .push ("Hello!!!" )
225- print ("Result: " + str (response ))
300+ push_response = spontit_src .push ("Hello!!!" )
301+ print ("Result: " + str (push_response ))
226302
227303 # To see documentation, run:
228304 help (SpontitResource )
0 commit comments