Skip to content

Commit

Permalink
Merge pull request #241 from FND/http-simplification
Browse files Browse the repository at this point in the history
Simplify HTTP setup for readability
  • Loading branch information
cdent committed Feb 14, 2018
2 parents dd8a407 + f13aa5e commit d71d944
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions gabbi/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Subclass of Http class for verbosity."""

from __future__ import print_function

Expand All @@ -29,10 +28,10 @@


class Http(urllib3.PoolManager):
"""A subclass of the urllib3.PoolManager to munge the data.
"""A subclass of the ``urllib3.PoolManager`` to munge the data.
This transforms the response to look more like what httplib2
provided when it was used as the httpclient.
provided when it was used as the HTTP client.
"""

def request(self, absolute_uri, method, body, headers, redirect):
Expand All @@ -55,21 +54,21 @@ def request(self, absolute_uri, method, body, headers, redirect):


class VerboseHttp(Http):
"""A subclass of Http that verbosely reports on activity.
"""A subclass of ``Http`` that verbosely reports on activity.
If the output is a tty or ``GABBI_FORCE_COLOR`` is set in the
environment, then output will be colorized according to ``COLORMAP``.
Output can include request and response headers, request and
response body content (if of a printable content-type), or both.
response body content (if of a printable content type), or both.
The color of the output has reasonable defaults. These may be overridden
by setting the following environment variables
* GABBI_CAPTION_COLOR
* GABBI_HEADER_COLOR
* GABBI_REQUEST_COLOR
* GABBI_STATUS_COLOR
* ``GABBI_CAPTION_COLOR``
* ``GABBI_HEADER_COLOR``
* ``GABBI_REQUEST_COLOR``
* ``GABBI_STATUS_COLOR``
to any of: BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE
"""
Expand Down Expand Up @@ -171,16 +170,11 @@ def _verbose_output(self, message, prefix='', color=None, stream=None):


def get_http(verbose=False, caption=''):
"""Return an Http class for making requests."""
if verbose:
body = True
headers = True
colorize = True
stream = sys.stdout
if verbose == 'body':
headers = False
if verbose == 'headers':
body = False
return VerboseHttp(body=body, headers=headers, colorize=colorize,
stream=stream, caption=caption, strict=True)
return Http(strict=True)
"""Return an ``Http`` class for making requests."""
if not verbose:
return Http(strict=True)

headers = False if verbose == 'body' else True
body = False if verbose == 'headers' else True
return VerboseHttp(headers=headers, body=body, stream=sys.stdout,
caption=caption, colorize=True, strict=True)

0 comments on commit d71d944

Please sign in to comment.