Last active
August 29, 2015 13:56
-
-
Save proppy/8964896 to your computer and use it in GitHub Desktop.
request hack to add url prefix
This file contains hidden or 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
import requests | |
import json | |
class Client(object): | |
def __init__(self, base_url): | |
self.base_url = base_url | |
def build_url(self, path): | |
return self.base_url + path | |
def update_kwargs(self, json_data, kwargs): | |
if json_data: | |
kwargs.update({ | |
'headers': { | |
'content-type': 'application/json', | |
}, | |
'data': json.dumps(json_data) | |
}) | |
return kwargs | |
def get(self, path, json_data=None, **kwargs): | |
return requests.get(self.build_url(path), | |
**self.update_kwargs(json_data, kwargs)) | |
def post(self, path, json_data=None, **kwargs): | |
return requests.post(self.build_url(path), | |
**self.update_kwargs(json_data, kwargs)) | |
def put(self, path, json_data=None, **kwargs): | |
return requests.put(self.build_url(path), | |
**self.update_kwargs(json_data, kwargs)) | |
def delete(self, path, json_data=None, **kwargs): | |
return requests.delete(self.build_url(path), | |
**self.update_kwargs(json_data, kwargs)) | |
r = Client('http://localhost:8080') | |
foo_todo = r.post('/todos', json_data={'text':'foo'}).json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class better(object):