Skip to content

Commit

Permalink
First and Last
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio committed Aug 12, 2015
0 parents commit a494802
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.rst
@@ -0,0 +1,34 @@
requests.py
===========

This single file should be used to help in debugging your project that uses 'requests' lib


Why Should I Use This?
----------------------

Because you are tired of setting breakpoints or 'print's into your code to figure out what requests for what urls your project is making.
I did this cause I'm on a project that makes a lot o requests for lots of diferente places.
With this I figured that I make about 300 requests every time I run a full project 'behave' test and that some views make up to 25 requests to get done. Now I'm refactoring my abstractions to make less requests and everybody is happy :)

When Should I NOT Use This?
---------------------------

On your production environment. This guy is working fine but you dont need to insert this lame-hacking failure point into you production code.


How To
------

Just put this file on your project root directory and ALL your 'requests' imports will import this guy

But remeber:
""""""""""""

Remove it before commit to production. I love this little hack but IT'S NOT SAFE FOR PRODUCTION


Features
--------

- Print out EVERY request you make using 'requests' lib
69 changes: 69 additions & 0 deletions requests.py
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
u"""Arquivo de debug da biblioteca 'requests'.
Substitui a biblioteca requests em todo o projeto adicionando um _log_it()
aos metodos get, post, put e update.
Por se tratar de um hack apenas para debug sugiro remover esse arquivo de
produção.
"""

from sys import path
import imp

SHORT = 0 # Se diferente de 0 irá truncar a saída do log em X caracteres

# Varre todo o sys.path em busca da biblioteca requests
for i in path:
try:
r = imp.load_package("requests", i + "/requests")
# Caso consiga achar a biblioteca salva uma cópia dos metodos
# post, get, put e update
_post = r.__getattribute__("post")
_get = r.__getattribute__("get")
_put = r.__getattribute__("put")
_update = r.__getattribute__("update")
# Pára a busca ao encontrar
break
except:
pass


def _log_it(method, args, kwargs):
u"""Printa de modo amigável todos os requests feitos."""
url = kwargs.get("url")
if not url:
url = args[0]
args = args[1:]
args = ", " + ", ".join([str(i) for i in args]) if args else None
kwargs = ", " + ", ".join(["%s=%s" % (str(i), str(j))
for i, j in kwargs.items()]) if args else None
linha = '%s: %s%s%s' % (
method.upper(), url, args or '', kwargs or '')
if SHORT:
linha = linha[:SHORT]
print u"\033[91m%s\033[0m" % linha


def get(*args, **kwargs):
u"""Printa os parametros de chama a cópia original do get."""
_log_it("get", args, kwargs)
return _get(*args, **kwargs)


def post(*args, **kwargs):
u"""Printa os parametros de chama a cópia original do post."""
_log_it("post", args, kwargs)
return _post(*args, **kwargs)


def update(*args, **kwargs):
u"""Printa os parametros de chama a cópia original do update."""
_log_it("update", args, kwargs)
return _update(*args, **kwargs)


def put(*args, **kwargs):
u"""Printa os parametros de chama a cópia original do put."""
_log_it("put", args, kwargs)
return _put(*args, **kwargs)

0 comments on commit a494802

Please sign in to comment.