I needed to do this for a REST client using media tokens. I wrapped the Requests session object in my own interface so that I can always send the auth header and (more relevant) make HTTP requests to the same site, just using the URL.
class requests_wrapper(): client = requests.session(headers={'Authorization':'myauthtoken'}) base_path = "http://www.example.com" def _make_path_request(self, http_method, path, **kwargs): """ Use the http_method string to find the requests.Session instance's method. """ method_to_call = getattr(self.client, http_method.lower()) return method_to_call(self.base_path + path, **kwargs) def path_get(self, path, **kwargs): """ Sends a GET request to base_path + path. """ return self._make_path_request('get', path, **kwargs) def path_post(self, path, **kwargs): """ Sends a POST request to base_path + path. """ return self._make_path_request('post', path, **kwargs) def path_put(self, path, **kwargs): """ Sends a PUT request to base_path + path. """ return self._make_path_request('put', path, **kwargs) def path_delete(self, path, **kwargs): """ Sends a DELETE request to base_path + path. """ return self._make_path_request('delete', path, **kwargs)
Then I could just make a request based on the path:
# Initialize myclient = requests_wrapper()
source share