Permalink
Browse files

Initial version of restsh

  • Loading branch information...
0 parents commit 23026dd7ea03205e6a2e87ea2e4aa3f4d3083645 @jespino committed Apr 4, 2012
Showing with 172 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +5 −0 README.rst
  3. +80 −0 restsh
  4. +1 −0 restshlib/__init__.py
  5. +50 −0 restshlib/restshlib.py
  6. +35 −0 setup.py
@@ -0,0 +1 @@
+*.pyc
@@ -0,0 +1,5 @@
+RestSH
+======
+
+RestSH is a shell for easy test and use REST services.
+
80 restsh
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import readline
+import simplejson
+from restshlib.restshlib import RestSH
+
+HELP_TEXT = '''Usage: command [<command-option>...]
+
+Change cliente status:
+ setheader <header-key> <header-value>
+ unsetheader <header-key>
+ headers
+ baseurl <host>
+
+Rest actions:
+ get <url>
+ post <url> <json-format-data>
+ put <url> <json-format-data>
+ delete <url>
+'''
+
+def print_response(response):
+ print "Response body:"
+ print response.read()
+ print "Response headers:"
+ for header in response.getheaders():
+ print " %s: %s" % (header[0], header[1])
+ print "Status Code: %s %s" % (unicode(response.status), unicode(response.reason))
+
+if __name__=='__main__':
+ restsh = RestSH()
+
+ while True:
+ try:
+ try:
+ command = raw_input('restsh> ')
+ except EOFError:
+ command = "quit"
+ command_split = command.split()
+ if len(command_split) == 0:
+ pass
+ elif command_split[0] == 'quit':
+ break
+ elif command_split[0] == 'help':
+ print HELP_TEXT
+ elif command_split[0] == 'headers':
+ for header in restsh.headers.iteritems():
+ print "%s: %s" % (header[0], header[1])
+ elif command_split[0] == 'setheader':
+ restsh.set_header(command_split[1],' '.join(command_split[2:]))
+ elif command_split[0] == 'unsetheader':
+ restsh.unset_header(command_split[1])
+ elif command_split[0] == 'baseurl':
+ restsh.set_base_url(' '.join(command_split[1:]))
+ elif command_split[0] == 'get':
+ response = restsh.get(' '.join(command_split[1:]))
+ print_response(response)
+ elif command_split[0] == 'post':
+ response = restsh.post(command_split[1], simplejson.loads(' '.join(command_split[2:])))
+ print_response(response)
+ elif command_split[0] == 'put':
+ response = restsh.put(command_split[1], simplejson.loads(' '.join(command_split[2:])))
+ print_response(response)
+ elif command_split[0] == 'delete':
+ response = restsh.delete(' '.join(command_split[1:]))
+ print_response(response)
+ else:
+ print "ERROR: Command not found"
+ print HELP_TEXT
+ pass
+ except IndexError:
+ print "ERROR: Invalid parameters for %s command" % command_split[0]
+ print HELP_TEXT
+ except simplejson.decoder.JSONDecodeError as e:
+ print "JSON ERROR: %s" % (str(e))
+ print "Your PUT and POST data parameter must be in JSON format."
+ except Exception as e:
+ print "ERROR: %s" % (str(e))
+
@@ -0,0 +1 @@
+__version__ = (0, 1, 0, "final", 0)
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import httplib
+import urlparse
+import urllib
+
+class RestSH():
+ base_url = ""
+ headers = {}
+
+ def __init__(self, base_url=""):
+ self.base_url = base_url
+
+ def post(self, url, data):
+ urlparts = urlparse.urlparse(self.base_url+url)
+ conn = httplib.HTTPConnection(urlparts[1])
+ rel_url = "%s?%s" % (urlparts[2], urlparts[4])
+ conn.request("POST", rel_url, urllib.urlencode(data), self.headers)
+ return conn.getresponse()
+
+ def put(self, url, data):
+ urlparts = urlparse.urlparse(self.base_url+url)
+ conn = httplib.HTTPConnection(urlparts[1])
+ rel_url = "%s?%s" % (urlparts[2], urlparts[4])
+ conn.request("PUT", rel_url, urllib.urlencode(data), self.headers)
+ return conn.getresponse()
+
+ def get(self, url):
+ urlparts = urlparse.urlparse(self.base_url+url)
+ conn = httplib.HTTPConnection(urlparts[1])
+ rel_url = "%s?%s" % (urlparts[2], urlparts[4])
+ conn.request("GET", rel_url, None, self.headers)
+ return conn.getresponse()
+
+ def delete(self, url):
+ urlparts = urlparse.urlparse(self.base_url+url)
+ conn = httplib.HTTPConnection(urlparts[1])
+ rel_url = "%s?%s" % (urlparts[2], urlparts[4])
+ conn.request("DELETE", rel_url, None, self.headers)
+ return conn.getresponse()
+
+ def set_header(self, key, value):
+ self.headers[key] = value
+
+ def unset_header(self, key):
+ self.headers.pop(key)
+
+ def set_base_url(self, base_url):
+ self.base_url = base_url
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from setuptools import setup, find_packages
+import restshlib
+
+setup(
+ name = 'restsh',
+ version = ":versiontools:restshlib:",
+ description = "A simple rest shell client",
+ long_description = "",
+ keywords = 'rest shell',
+ author = 'Jesús Espino García',
+ author_email = 'jespinog@gmail.com',
+ url = 'https://github.com/jespino/restsh',
+ license = 'BSD',
+ include_package_data = True,
+ packages = ['restshlib', ],
+ scripts = ['restsh', ],
+ install_requires=[
+ 'distribute',
+ ],
+ setup_requires = [
+ 'versiontools >= 1.8',
+ ],
+ classifiers = [
+ "Programming Language :: Python",
+ 'Development Status :: 4 - Beta',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: BSD License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Topic :: Internet :: WWW/HTTP',
+ ]
+)

0 comments on commit 23026dd

Please sign in to comment.