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

Commit 6af37e4

Browse files
authored
Add client (#2)
1 parent 9c7a5cb commit 6af37e4

File tree

7 files changed

+177
-5
lines changed

7 files changed

+177
-5
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ pip install wayscript
2121
2. Run your WayScript programs from your Python code:
2222

2323
```python
24-
import wayscript
24+
from wayscript import WayScript
2525

2626
api_key = 'YOUR_API_KEY'
2727

28-
wayscript = wayscript.authorize( api_key )
28+
wayscript = WayScript( api_key )
2929

3030
# Run a program by id
3131
program_id = 1234
32-
wayscript.run( program_id )
32+
wayscript.run_program( program_id )
3333

3434
# Pass variables to a program
3535
variables = [ 'one', 'two', 'three' ]
36-
wayscript.run( program_id, variables )
36+
wayscript.run_program( program_id, variables = variables )
37+
38+
# Run a program asynchronously
39+
wayscript.run_program( program_id, run_async = True )
40+
wayscript.run_program( program_id, variables = variables, run_async = True )
41+
42+
# Get the response from the server
43+
response = wayscript.run_program( program_id )
3744
```

setup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright (c) 2019 WayScript, Inc. All rights reserved.
4+
# Licensed under the MIT License.
5+
16
import setuptools
27

38
with open("README.md", "r") as fh:
@@ -11,8 +16,9 @@
1116
description="WayScript gives you flexible building blocks to seamlessly integrate, automate and host tools in the cloud.",
1217
long_description=long_description,
1318
long_description_content_type="text/markdown",
19+
install_requires=['requests>=2.21.0'],
1420
url="https://github.com/wayscript/wayscript-python",
15-
packages=setuptools.find_packages(),
21+
packages=['wayscript'],
1622
license='MIT',
1723
classifiers=[
1824
"Development Status :: 4 - Beta",

tests/__init__.py

Whitespace-only changes.

tests/test.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright (c) 2019 WayScript, Inc. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from unittest import TestCase
5+
from unittest.mock import patch
6+
7+
from wayscript import WayScript
8+
from wayscript.exceptions import InvalidApiKeyException
9+
10+
11+
class TestWayScript( TestCase ):
12+
dummy_api_key = "_DUMMY_API_KEY_DUMMY_API_KEY_DUMMY_API_KEY_"
13+
program_id = 1234
14+
variables = [ 'one', 'two', 'three' ]
15+
api_url = 'https://wayscript.com/api'
16+
17+
def test_api_key( self ):
18+
with self.assertRaises( InvalidApiKeyException ):
19+
WayScript( '' )
20+
21+
with self.assertRaises( InvalidApiKeyException ):
22+
WayScript( 'foobar' )
23+
24+
self.assertIsNotNone( WayScript( self.dummy_api_key ) )
25+
26+
def test_run_program( self ):
27+
wayscript = WayScript( self.dummy_api_key )
28+
29+
with patch( 'requests.post' ) as post_request:
30+
wayscript.run_program( self.program_id )
31+
post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key,
32+
'program_id': self.program_id,
33+
'run_async': False } )
34+
35+
def test_run_program_with_variables( self ):
36+
wayscript = WayScript( self.dummy_api_key )
37+
38+
with patch( 'requests.post' ) as post_request:
39+
wayscript.run_program( self.program_id, variables = self.variables )
40+
post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key,
41+
'program_id': self.program_id,
42+
'run_async': False,
43+
'variables': self.variables } )
44+
45+
def test_run_program_async( self ):
46+
wayscript = WayScript( self.dummy_api_key )
47+
48+
with patch( 'requests.post' ) as post_request:
49+
wayscript.run_program( self.program_id, variables = self.variables, run_async = True )
50+
post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key,
51+
'program_id': self.program_id,
52+
'run_async': True,
53+
'variables': self.variables } )
54+
55+
def test_empty_variables( self ):
56+
wayscript = WayScript( self.dummy_api_key )
57+
58+
with patch( 'requests.post' ) as post_request:
59+
wayscript.run_program( self.program_id, variables = [ ], run_async = True )
60+
post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key,
61+
'program_id': self.program_id,
62+
'run_async': True } )
63+
64+
def test_returns_response( self ):
65+
wayscript = WayScript( self.dummy_api_key )
66+
67+
with patch( 'requests.post', return_value = 'ok' ) as post_request:
68+
response = wayscript.run_program( self.program_id, run_async = True )
69+
post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key,
70+
'program_id': self.program_id,
71+
'run_async': True } )
72+
self.assertEqual( response, 'ok' )

wayscript/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
WayScript Python SDK
5+
~~~~~~~~~~~~
6+
This module implements the WayScript Client.
7+
:copyright: (c) 2019 WayScript, Inc
8+
:license: MIT, see LICENSE.txt for more details.
9+
"""
10+
11+
from wayscript.client import Client
12+
13+
class WayScript( Client ):
14+
"""A user-created :class:`WayScript <WayScript>` client object.
15+
Used to send requests to the WayScript server.
16+
:param api_key: The API Key for a WayScript account.
17+
Usage::
18+
>>> from wayscript import WayScript
19+
>>> api_key = 'YOUR_API_KEY'
20+
>>> wayscript = WayScript( api_key )
21+
"""
22+
pass
23+

wayscript/client.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
wayscript.client
5+
~~~~~~~~~~~~
6+
This module handles WayScript API calls.
7+
:copyright: (c) 2019 WayScript, Inc
8+
:license: MIT, see LICENSE.txt for more details.
9+
"""
10+
11+
import requests
12+
from wayscript.exceptions import InvalidApiKeyException
13+
14+
15+
class Client:
16+
def __init__( self, api_key):
17+
if not api_key or len( api_key ) != 43:
18+
raise InvalidApiKeyException()
19+
20+
self._api_key = api_key
21+
self._api_url = 'https://wayscript.com/api'
22+
23+
def run_program( self, program_id, variables = None, run_async = False ):
24+
"""Runs a WayScript program.
25+
:param program_id: The id of the program you want to run.
26+
:param variables: (optional) An array of arguments to pass to your program.
27+
:param run_async: (optional) Run this program asyncronously.
28+
If False, this command will block until your program has finished running.
29+
:return: Response object
30+
:rtype: requests.Response
31+
Usage::
32+
>>> from wayscript import WayScript
33+
>>> api_key = 'YOUR_API_KEY'
34+
>>> wayscript = WayScript( api_key )
35+
>>> program_id = 1234
36+
>>> response = wayscript.run_program( program_id, variables = variables, run_async = True )
37+
<Response [200]>
38+
"""
39+
40+
params = { 'api_key': self._api_key,
41+
'program_id': program_id,
42+
'run_async': run_async }
43+
44+
if variables and len( variables ):
45+
params[ 'variables' ] = variables
46+
47+
return self._post( params )
48+
49+
def _post( self, params ):
50+
return requests.post( self._api_url, params = params )

wayscript/exceptions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
wayscript.exceptions
5+
~~~~~~~~~~~~
6+
This module contains the WayScript exception classes.
7+
:copyright: (c) 2019 WayScript, Inc
8+
:license: MIT, see LICENSE.txt for more details.
9+
"""
10+
11+
12+
class InvalidApiKeyException( Exception ):
13+
def __init__( self ):
14+
super().__init__( 'The API Key provided is not valid.' )

0 commit comments

Comments
 (0)