Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
shyal committed Dec 8, 2016
1 parent ec5873e commit a7c4e90
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.venv
.eggs
*.bak
*.pyc
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.11
0.1.12
2 changes: 1 addition & 1 deletion docs/source/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ basic

This is by far the simplest example on how to get started with HoverPy. Please run this example using:

``$ env PYTHONPATH=.:${PYTHONPATH} python examples/basic/basic.py``
``$ python examples/basic/basic.py``

You should see your IP address show up twice. Let's walk through the code to see what's happening.

Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
# built documents.
#
# The short X.Y version.
version = '0.1.11'
version = '0.1.12'
# The full version, including alpha/beta/rc tags.
release = '0.1.11'
release = '0.1.12'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
16 changes: 9 additions & 7 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ Installation
Cloning
-------

We're currently in development mode, so you're better off cloning the repository. Make sure to pull often!
We're currently in development mode, so you're better off cloning the repository.

.. code:: bash
$ virtualenv hoverpyenv
$ cd hoverpyenv
$ source bin/activate
$ git clone https://github.com/SpectoLabs/hoverpy.git
$ cd hoverpy
$ virtualenv .venv
$ source .venv/bin/activate
$ python setup.py install
This installs hoverpy and its requirements in your .venv folder; make sure to pull often, and run the ``python setup.py install`` when you do.

Testing
-------
Expand Down Expand Up @@ -52,7 +54,7 @@ Please note we'll cover the examples in the `usage`_ page. But for the truly imp

.. code:: bash
$ env PYTHONPATH=.:${PYTHONPATH} python examples/basic/basic.py
$ python examples/basic/basic.py
Installing from repo
--------------------
Expand All @@ -70,7 +72,7 @@ You can also install HoverPy from PIP, however once again you're better off play

.. code:: bash
$ pip install --user -i https://testpypi.python.org/pypi hoverpy
$ pip install -i https://testpypi.python.org/pypi hoverpy
HoverFly binary
---------------
Expand All @@ -79,5 +81,5 @@ Please note that when you install HoverPy, the HoverFly binaries get downloaded

.. code:: bash
${home}/hoverfly/bin/dist_vX.X.X/${OS}_${ARCH}/hoverfly
${home}/.hoverfly/bin/dist_vX.X.X/${OS}_${ARCH}/hoverfly
4 changes: 2 additions & 2 deletions docs/source/readthedocs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ readthedocs

This is a slightly more advanced example, where we query readthedocs.io for articles. In the first phase, we run the program in capture mode. This is done using the capture flag:

``env PYTHONPATH=.:${PYTHONPATH} python examples/readthedocs/readthedocs.py --capture``
``python examples/readthedocs/readthedocs.py --capture``

the program can then be run again in simulate mode, in a fraction of the time:

``env PYTHONPATH=.:${PYTHONPATH} python examples/readthedocs/readthedocs.py``
`python examples/readthedocs/readthedocs.py``

We'll now run through the code to see what it's doing.

Expand Down
4 changes: 2 additions & 2 deletions docs/source/soap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ soap

In this example we'll take a look a using hoverpy when working with SOAP. To run this example, simply execute:

``env PYTHONPATH=.:${PYTHONPATH} examples/soap/soap.py --capture``
``examples/soap/soap.py --capture``

which runs the program in capture mode, then:

``env PYTHONPATH=.:${PYTHONPATH} examples/soap/soap.py``
``examples/soap/soap.py``

Which simply runs our program in simulate mode.

Expand Down
2 changes: 1 addition & 1 deletion examples/basic/basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is by far the simplest example on how to get started with HoverPy.
# Please run this example using: <br><br>``$ env PYTHONPATH=.:${PYTHONPATH} python examples/basic/basic.py``<br><br>
# Please run this example using: <br><br>``$ python examples/basic/basic.py``<br><br>
# You should see your IP address show up twice. Let's walk through the
# code to see what's happening.
from hoverpy import HoverPy
Expand Down
4 changes: 2 additions & 2 deletions examples/readthedocs/readthedocs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This is a slightly more advanced example, where we query readthedocs.io for articles. In the first phase, we run
# the program in capture mode. This is done using the capture flag:<br><br>
# ``env PYTHONPATH=.:${PYTHONPATH} python examples/readthedocs/readthedocs.py --capture``<br><br>
# ``python examples/readthedocs/readthedocs.py --capture``<br><br>
# the program can then be run again in simulate mode, in a fraction of the time:<br><br>
# ``env PYTHONPATH=.:${PYTHONPATH} python examples/readthedocs/readthedocs.py``<br><br>
# ``python examples/readthedocs/readthedocs.py``<br><br>

# We'll now run through the code to see what it's doing.
from hoverpy import HoverPy
Expand Down
92 changes: 90 additions & 2 deletions hoverpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class HoverPy:

def __init__(self, host="localhost", capture=False,
proxyPort=8500, adminPort=8888, inMemory=False,
modify=False, middleware="", dbpath="", simulation=""):
modify=False, middleware="",
dbpath="requests.db", simulation=""):
self._proxyPort = proxyPort
self._adminPort = adminPort
self._host = host
Expand All @@ -59,21 +60,41 @@ def __enter__(self):
return self

def wipe(self):
"""
Wipe the bolt database.
Calling this after HoverPy has been instantiated is
potentially dangerous. This function is mostly used
internally for unit tests.
"""
try:
os.remove("./requests.db")
if os.isfile(self._dbpath):
os.remove(self._dbpath)
except OSError:
pass

def host(self):
"""
Returns the URL to the admin interface / APIs.
"""
return "http://%s:%i" % (self._host, self._adminPort)

def v1(self):
"""
Return the URL to the v1 API
"""
return self.host()+"/api"

def v2(self):
"""
Return the URL to the v2 API
"""
return self.host()+"/api/v2"

def enableProxy(self):
"""
Set the required environment variables to enable the use of hoverfly as a proxy.
"""
logging.debug("enabling proxy")
os.environ[
"HTTP_PROXY"] = "http://%s:%i" % (self._host, self._proxyPort)
Expand All @@ -85,10 +106,20 @@ def enableProxy(self):
"cert.pem")

def disableProxy(self):
"""
Clear the environment variables required to enable the use of hoverfly as a proxy.
"""
del os.environ['HTTP_PROXY']
del os.environ['HTTPS_PROXY']
del os.environ['REQUESTS_CA_BUNDLE']

def start(self):
"""
Start the hoverfly process.
This function waits until it can make contact
with the hoverfly API before returning.
"""
logging.debug("starting %i" % id(self))
FNULL = open(os.devnull, 'w')
flags = self.flags()
Expand Down Expand Up @@ -118,28 +149,55 @@ def start(self):
raise ValueError("Could not start hoverfly!")

def stop(self):
"""
Stop the hoverfly process.
"""
if logging:
logging.debug("stopping")
self._process.kill()
self._process = None
self.disableProxy()

def capture(self):
"""
Switches hoverfly to capture mode.
"""
return self.mode("capture")

def simulate(self):
"""
Switches hoverfly to simulate mode.
Please note simulate is the default mode.
"""
return self.mode("simulate")

def config(self):
"""
Returns the hoverfly configuration json.
"""
return session().get(self.v2()+"/hoverfly").json()

def simulation(self, data=None):
"""
Gets / Sets the simulation data.
If no data is passed in, then this method acts as a getter.
if data is passed in, then this method acts as a setter.
Keyword arguments:
data -- the simulation data you wish to set (default None)
"""
if data:
return session().put(self.v2()+"/simulation", data=data)
else:
return session().get(self.v2()+"/simulation").json()

def destination(self, name=""):
"""
Gets / Sets the destination data.
TBD.
"""
if name:
return session().put(
self.v2()+"/hoverfly/destination",
Expand All @@ -148,9 +206,21 @@ def destination(self, name=""):
return session().get(self.v2()+"/hoverfly/destination").json()

def middleware(self):
"""
Gets the middleware data.
TBD.
"""
return session().get(self.v2()+"/hoverfly/middleware").json()

def mode(self, mode=None):
"""
Gets / Sets the mode.
If no mode is provided, then this method acts as a getter.
Keyword arguments:
mode -- this should either be 'capture' or 'simulate' (default None)
"""
if mode:
logging.debug("SWITCHING TO %s" % mode)
url = self.v2()+"/hoverfly/mode"
Expand All @@ -161,34 +231,52 @@ def mode(self, mode=None):
return session().get(self.v2()+"/hoverfly/mode").json()["mode"]

def usage(self):
"""
Gets the usage data. TBD.
"""
return session().get(self.v2()+"/hoverfly/usage").json()

def metadata(self, delete=False):
"""
Gets the metadata. TBD.
"""
if delete:
return session().delete(self.v1()+"/metadata").json()
else:
return session().get(self.v1()+"/metadata").json()

def records(self, data=None):
"""
Gets / Sets records. TBD.
"""
if data:
return session().post(self.v1()+"/records", data=data).json()
else:
return session().get(self.v1()+"/records").json()

def delays(self, delays=[]):
"""
Gets / Sets the delays. TBD.
"""
if delays:
return session().put(
self.v1()+"/delays", data=json.dumps(delays)).json()
else:
return session().get(self.v1()+"/delays").json()

def addDelay(self, urlPattern="", delay=0, httpMethod=None):
"""
Adds delays. TBD.
"""
delay = {"urlPattern": urlPattern, "delay": delay}
if httpMethod:
delay["httpMethod"] = httpMethod
return self.delays(delays={"data": [delay]})

def flags(self):
"""
Internal method. Turns arguments into flags.
"""
flags = []
if self._dbpath:
flags += ["-db-path", self._dbpath]
Expand Down
2 changes: 1 addition & 1 deletion hoverpy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def getOS():

dirName = dirname(abspath(__file__))

version = '0.1.11'
version = '0.1.12'
dist_version = '0.9.2'

hoverflyDirectory = join(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
except ImportError:
from distutils.core import setup

version = '0.1.11'
version = '0.1.12'

setup(
name='hoverpy',
Expand Down

0 comments on commit a7c4e90

Please sign in to comment.