Skip to content
This repository was archived by the owner on Aug 19, 2022. It is now read-only.

Commit bab8388

Browse files
authored
Add the ability to specify which function to run (#5)
* Add the ability to specify which function to run * typo * Bump package version
1 parent df2d003 commit bab8388

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,44 @@ pip install wayscript
2626
from wayscript import WayScript
2727

2828
api_key = 'YOUR_API_KEY'
29-
3029
wayscript = WayScript( api_key )
3130

3231
# Run a program by id
3332
program_id = 1234
3433
wayscript.run_program( program_id )
3534

36-
# Pass variables to a program
35+
# Pass variables to a program (optional)
3736
variables = [ 'one', 'two', 'three' ]
3837
wayscript.run_program( program_id, variables = variables )
3938

40-
# Run a program asynchronously
39+
# Run a specific function within your program (optional)
40+
function = 'My Function'
41+
wayscript.run_program( program_id, variables = variables, function = function )
42+
43+
# Run a program asynchronously (optional)
4144
wayscript.run_program( program_id, run_async = True )
42-
wayscript.run_program( program_id, variables = variables, run_async = True )
45+
wayscript.run_program( program_id, variables = variables, function = function, run_async = True )
4346

4447
# Get the response from the server
4548
response = wayscript.run_program( program_id )
4649
```
4750

51+
⭐ In order to run a program using the WayScript Python API, you must first add an active [Webhook Trigger](https://wayscript.com/documentation/trigger/webhook_trigger) to that program.
52+
53+
### Running a specific function
54+
55+
- The function you specify MUST have an active [Webhook Trigger](https://wayscript.com/documentation/trigger/webhook_trigger).
56+
- If you do not specify a function name in your request and your program has ***one*** function with a Webhook Trigger, the function with the Webhook Trigger will run.
57+
- If you do not specify a function name in your request and your program has ***multiple*** functions with Webhook Triggers, you will be asked to specify which function you would like to run.
58+
4859
## Run a WayScript program from command line
4960
```sh
5061
WS_API_KEY="YOUR_API_KEY"
5162
PROGRAM_ID=1234
5263
ARGUMENT="whatever"
64+
FUNCTION="My Function"
5365

54-
python -c "from wayscript import WayScript; WayScript('$WS_API_KEY').run_program($PROGRAM_ID, '$ARGUMENT')"
66+
python -c "from wayscript import WayScript; WayScript('$WS_API_KEY').run_program($PROGRAM_ID, '$ARGUMENT', '$FUNCTION')"
5567
```
68+
69+
If you don't want to use Python on the command line, you can use `curl`. (See the WayScript [REST API documentation](https://wayscript.com/documentation/apis/rest_api).)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setuptools.setup(
1212
name="wayscript",
13-
version="0.0.1",
13+
version="0.0.2",
1414
author="Team WayScript",
1515
author_email="[email protected]",
1616
description="WayScript gives you flexible building blocks to seamlessly integrate, automate and host tools in the cloud.",

tests/test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ def test_run_program( self ):
3434
'run_async': False },
3535
headers = self.headers )
3636

37+
def test_run_program_function( self ):
38+
wayscript = WayScript( self.dummy_api_key )
39+
40+
with patch( 'requests.post' ) as post_request:
41+
wayscript.run_program( self.program_id, function = 'My Function' )
42+
post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key,
43+
'program_id': self.program_id,
44+
'function': 'My Function',
45+
'run_async': False },
46+
headers = self.headers )
47+
3748
def test_run_program_with_variables( self ):
3849
wayscript = WayScript( self.dummy_api_key )
3950

@@ -45,6 +56,18 @@ def test_run_program_with_variables( self ):
4556
'variables': self.variables },
4657
headers = self.headers )
4758

59+
def test_run_program_function_with_variables( self ):
60+
wayscript = WayScript( self.dummy_api_key )
61+
62+
with patch( 'requests.post' ) as post_request:
63+
wayscript.run_program( self.program_id, variables = self.variables, function = 'My Function' )
64+
post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key,
65+
'program_id': self.program_id,
66+
'run_async': False,
67+
'variables': self.variables,
68+
'function': 'My Function'},
69+
headers = self.headers )
70+
4871
def test_run_program_async( self ):
4972
wayscript = WayScript( self.dummy_api_key )
5073

wayscript/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ def __init__( self, api_key):
2020
self._api_key = api_key
2121
self._api_url = 'https://wayscript.com/api'
2222

23-
def run_program( self, program_id, variables = None, run_async = False ):
23+
def run_program( self, program_id, variables = None, function = None, run_async = False ):
2424
"""Runs a WayScript program.
2525
:param program_id: The id of the program you want to run.
2626
:param variables: (optional) An array of arguments to pass to your program.
27+
:param function: (optional) The name of the function within your program that you would like to run.
2728
:param run_async: (optional) Run this program asyncronously.
2829
If False, this command will block until your program has finished running.
2930
:return: Response object
@@ -33,7 +34,9 @@ def run_program( self, program_id, variables = None, run_async = False ):
3334
>>> api_key = 'YOUR_API_KEY'
3435
>>> wayscript = WayScript( api_key )
3536
>>> program_id = 1234
36-
>>> response = wayscript.run_program( program_id, variables = variables, run_async = True )
37+
>>> variables = [ 'one', 'two', 'three' ]
38+
>>> function = 'My Function'
39+
>>> response = wayscript.run_program( program_id, variables = variables, function = function, run_async = True )
3740
<Response [200]>
3841
"""
3942

@@ -44,6 +47,9 @@ def run_program( self, program_id, variables = None, run_async = False ):
4447
if variables and len( variables ):
4548
params[ 'variables' ] = variables
4649

50+
if function and len( function ):
51+
params[ 'function' ] = function
52+
4753
return self._post( params )
4854

4955
def _post( self, params ):

0 commit comments

Comments
 (0)