Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
adding capability to specify http_method; default is post
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-strangecode committed May 18, 2014
1 parent fdb58e4 commit a4ea8a0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Expose a directory of bash scripts as an API.

# Recent Breaking Changes!

Pyjojo now supports the use of alternative HTTP methods (defined in your script). To support this, we changed the previous GET calls to OPTIONS calls.

## Tutorial

Create a directory to store the bash scripts, by default pyJoJo will be pointed at /srv/pyjojo.
Expand Down Expand Up @@ -77,6 +81,7 @@ Example block:
# param: secret2 - more sensitive stuff
# filtered_params: secret1, secret2
# tags: test, staging
# http_method: get|post|put|delete
# lock: false
# -- jojo --

Expand All @@ -90,20 +95,22 @@ Fields:
- format: filtered_params: item1 [,item2]
- **tags**: specifies a list of tags that you want displayed when querying pyjojo about scripts.
- format: tags: item1 [,item2]
- **http_method**: specifies the http method the script should respond to. Default is POST
- format: http_method: get|post|put|delete
- **lock**: if true, only one instance of the script will be allowed to run
- format: lock: True|False

### Script List

Returns information about all the scripts.

GET /scripts
OPTIONS /scripts

### Get Information about a Script

Returns information about the specified script.

GET /scripts/{script_name}
OPTIONS /scripts/{script_name}

### Run a Script

Expand Down
56 changes: 52 additions & 4 deletions pyjojo/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,60 @@ def get(self):
@route(r"/scripts/([\w\-]+)/?")
class ScriptDetailsHandler(BaseHandler):

def get(self, script_name):
def options(self, script_name):
""" get the requirements for this script """

script = self.get_script(script_name)
script = self.get_script(script_name, 'options')
self.finish({'script': script.metadata()})

@asynchronous
@gen.engine
def get(self, script_name):
""" run the script """

script = self.get_script(script_name, 'get')
retcode, stdout, stderr = yield gen.Task(script.execute, self.params)

self.finish({
"stdout": stdout,
"stderr": stderr,
"retcode": retcode
})

@asynchronous
@gen.engine
def delete(self, script_name):
""" run the script """

script = self.get_script(script_name, 'delete')
retcode, stdout, stderr = yield gen.Task(script.execute, self.params)

self.finish({
"stdout": stdout,
"stderr": stderr,
"retcode": retcode
})

@asynchronous
@gen.engine
def put(self, script_name):
""" run the script """

script = self.get_script(script_name, 'put')
retcode, stdout, stderr = yield gen.Task(script.execute, self.params)

self.finish({
"stdout": stdout,
"stderr": stderr,
"retcode": retcode
})

@asynchronous
@gen.engine
def post(self, script_name):
""" run the script """

script = self.get_script(script_name)
script = self.get_script(script_name, 'post')
retcode, stdout, stderr = yield gen.Task(script.execute, self.params)

self.finish({
Expand All @@ -136,12 +178,18 @@ def post(self, script_name):
"retcode": retcode
})

def get_script(self, script_name):
def get_script(self, script_name, http_method):
script = self.settings['scripts'].get(script_name, None)

if script is None:
raise HTTPError(404, "Script with name '{0}' not found".format(script_name))

if http_method == 'options':
return script

if script.http_method != http_method:
raise HTTPError(405, "Wrong HTTP method for script '{0}'. Use '{1}'".format(script_name, script.http_method.upper()))

return script


Expand Down
12 changes: 10 additions & 2 deletions pyjojo/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ def metadata(self):
class Script(object):
""" a single script in the directory """

def __init__(self, filename, name, description, params, filtered_params, tags, needs_lock):
def __init__(self, filename, name, description, params, filtered_params, tags, http_method, needs_lock):
self.lock = toro.Lock()
self.filename = filename
self.name = name
self.description = description
self.params = params
self.filtered_params = filtered_params
self.tags = tags
self.http_method = http_method
self.needs_lock = needs_lock

def filter_params(self, params):
Expand Down Expand Up @@ -95,6 +96,7 @@ def create_env(self, input):
def metadata(self):
return {
"filename": self.filename,
"http_method": self.http_method,
"name": self.name,
"description": self.description,
"params": self.params,
Expand Down Expand Up @@ -141,6 +143,7 @@ def create_script(script_name, filename):
params = []
filtered_params = []
tags = []
http_method = 'post'
lock = False

# warn the user if we can't execute this file
Expand Down Expand Up @@ -186,6 +189,11 @@ def create_script(script_name, filename):
description = value
continue

# http_method
if in_block and key == "http_method":
http_method = value.lower()
continue

# param
if in_block and key == "param":
# handle the optional description
Expand Down Expand Up @@ -231,4 +239,4 @@ def create_script(script_name, filename):
log.error("file with filename {0} is missing an end block, Ignoring".format(filename))
return None

return Script(filename, script_name, description, params, filtered_params, tags, lock)
return Script(filename, script_name, description, params, filtered_params, tags, http_method, lock)

0 comments on commit a4ea8a0

Please sign in to comment.