Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fabio
committed
Aug 12, 2015
0 parents
commit a494802
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |