Skip to content

Instantly share code, notes, and snippets.

@ltddev
Created March 27, 2014 20:23
Show Gist options
  • Save ltddev/9817694 to your computer and use it in GitHub Desktop.
Save ltddev/9817694 to your computer and use it in GitHub Desktop.
RestTimeClient
'''
RestTimeClient.py
This script provides s simple one-function REST client to a "Date and Time
Service" running as as REST service on a remote server. It uses the Requests
api and the various alert dialogs in the Pythonista console module to both
get input and recieive output from thid 'service'. in addition it uses the
clickable link method of the same console module to invoke the same service
whose output is simply written to the console. Thus it provides two simple
frameworks to build REST clients for ios.
'''
import console
# Clears all content from the console.
console.clear()
console.show_activity()
# Display a clickable web link which is opened in default browser or a specific one for atypical protocols.
port = ':9080'
currIP = '192.168.0.11'
#currIP = 'swosnick-pc'
url = 'http://'+ currIP + port + '/HelloWorldWeb/HelloWorldServlet'
console.clear()
print 'Click on the blue link below to get the current date and time in your locale\n'
link = url
linkTitle = 'Current Date and Time Service'
console.write_link(linkTitle,link)
'''
# Display a typical modal information/error dialog with message.
message = 'The script completed successfully.'
console.hud_alert(message, 'success') # default duration used, 1.8 seconds.
console.hud_alert(message, 'success', 3.0) # non-default duration used, 3 seconds.
message = 'The script encountered an unknown error'
console.hud_alert(message, 'error', 2.4) # 2.4 seconds.
# Displaying dialogs that get a single input value and returns that value to the caller. They vary
# in whether default is supplied and/or OK button text is overridden.
# simple ok-cancel style dialogs with console.alert(). That is, 2 button modal dialog.
# this dialog returns only the ok button value or nothing (if cancel pressed). Note that the
# integer value assigned to any button is based on the relative ordering of parameters.
# Dialog that gets a single input value and supplies a default value (optionally)
title='Favourite Ice Cream'
message='Please enter the name of your favourite ice cream'
defaultInput= 'Vanilla'
okButtonText='My OK!'
# Dislay a title and message and empty textfield for input entry with fixed Cancel and OK buttons.
# NO default value is provided. Return value is not which button but value of textfield.
iceCreamName = console.input_alert(title,message)
print('Ice cream name: ' + iceCreamName)
# Same as previous but supplies a defaul value
iceCreamName = console.input_alert(title,message,defaultInput)
print('Ice cream name: ' + iceCreamName)
# Same as previous but overides default OK button text with your value
iceCreamName = console.input_alert(title,message,defaultInput,okButtonText)
print('Ice cream name: ' + iceCreamName)
# Setting font type, font size and font colour in the console. This does not change fonts in dialogs
# only in the console
console.set_color(0.0,0.0,1.0) #(r,g,b)
console.set_font('Arial', 22)
print('Arial, 22')
console.set_font('Arial', 9)
print('Arial, 9')
console.set_color(1.0,0.0,0.0) #(r,g,b)
console.set_font('Courier', 7)
print('Courier, 7')
console.set_font('Arial', 12)
print('Arial, 12')
console.set_color(0.0,1.0,0.0) #(r,g,b)
console.set_font('Courier', 24)
print('Courier, 24')
# Set back to default, call with no args, set default colour
console.set_font()
console.set_color()
print('Default font and size and default colour reset')
# Show image in the console
imagePath = '/var/mobile/testimage.png'
console.show_image(imagePath)
console.hide_activity()
# Getting secure input (input is echoed back as dots as is typical native behaviour)
promptMessage = 'Enter your password and then press return'
secureInput = console.secure_input(promptMessage)
print('Secure input entered: ' + secureInput)
# A dialog with two choices - either cancel or custom action which could also just be OK.
title = 'Continue Running Script?'
message = 'To continue running this script click OK (or Show Me); to abort the script now click CANCEL'
okButtonText = 'OK'
customActionButton1 = 'Show Me'
returnVal = console.alert(title, message, okButtonText)
print('return value is:'+ str(returnVal))
returnVal = console.alert(title, message, customActionButton1)
print('return value is:'+ str(returnVal))
# Dialog with two buttons (plus the default cancel, so four button choice)
customActionButton2 = 'Custom Action'
returnVal = console.alert(title, message, customActionButton1, customActionButton2)
print('return value is:'+ str(returnVal))
# Dialog with the maximum three buttons (plus the default cancel, so four button choice)
returnVal = console.alert(title,message,customActionButton1, customActionButton2, okButtonText)
print('return value is:'+ str(returnVal))
#if returnVal==1:
# print ('OK button was selected')
console.hide_output()
# A dialog that only offers a cancel button. No other choice is possible. Since this causes
# termination of the script there is no point to getting the return value. For that reason
# it is last call in this script because it ignores anything coming after it. What follows
# is what could be a usecase for having only a cancel button.
warnMessage = 'This script is unlicensed and you must press cancel and abort it'
console.alert(warnMessage)
'''
'''
port = ':9080'
currIP = '192.168.0.11'
#currIP = 'swosnick-pc'
url = 'http://'+ currIP + port + '/HelloWorldWeb/HelloWorldServlet'
console.clear()
print 'Trying to open url ' + url
response = requests.request('GET',url)
print (response.text)
'''
@ltddev
Copy link
Author

ltddev commented Mar 27, 2014

A simple Pythonista script that provides a REST client to a date-time service running as a servlet in a WAS Liberty profile Java EE appserver. The initial backup here contain various snippets of other scripts that will be used in final product.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment