mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-03-25 08:55:50 +00:00
Some rewrites to requests handling
Conflicts: tests/storage/test_http.py vdirsyncer/storage/dav.py
This commit is contained in:
parent
91c3114b78
commit
633a7de28c
5 changed files with 36 additions and 34 deletions
|
|
@ -1,26 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
tests.conftest
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
:copyright: (c) 2014 Markus Unterwaditzer
|
||||
:license: MIT, see LICENSE for more details.
|
||||
'''
|
||||
|
||||
import pytest
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def requests_mock(monkeypatch):
|
||||
'''It is easier than setting up the logging module!'''
|
||||
import requests.sessions
|
||||
old_func = requests.sessions.Session.request
|
||||
def mock_request(self, method, url, data=None, headers=None, **kw):
|
||||
print(method)
|
||||
print(url)
|
||||
print(data)
|
||||
print(headers)
|
||||
r = old_func(self, method, url, data=data, headers=headers, **kw)
|
||||
print(r.status_code)
|
||||
print(r.text)
|
||||
return r
|
||||
monkeypatch.setattr('requests.sessions.Session.request', mock_request)
|
||||
|
|
@ -44,14 +44,16 @@ class TestHttpStorage(object):
|
|||
'\n'.join([b'BEGIN:VCALENDAR'] + items + [b'END:VCALENDAR'])
|
||||
] * 2
|
||||
|
||||
def get(*a, **kw):
|
||||
def get(method, url, *a, **kw):
|
||||
assert method == 'GET'
|
||||
assert url == collection_url
|
||||
r = Response()
|
||||
r.status_code = 200
|
||||
assert responses
|
||||
r._content = responses.pop()
|
||||
return r
|
||||
|
||||
monkeypatch.setattr('requests.get', get)
|
||||
monkeypatch.setattr('requests.request', get)
|
||||
|
||||
s = HttpStorage(url=collection_url)
|
||||
|
||||
|
|
|
|||
|
|
@ -9,8 +9,9 @@
|
|||
|
||||
from .base import Item
|
||||
from .http import HttpStorageBase
|
||||
import vdirsyncer.exceptions as exceptions
|
||||
import vdirsyncer.log as log
|
||||
from .. import exceptions
|
||||
from .. import log
|
||||
from ..utils import request
|
||||
import requests
|
||||
import datetime
|
||||
import urlparse
|
||||
|
|
@ -93,8 +94,8 @@ class DavStorage(HttpStorageBase):
|
|||
if self._session is None:
|
||||
self._session = requests.session()
|
||||
url = self.parsed_url.scheme + '://' + self.parsed_url.netloc + path
|
||||
return self._session.request(method, url, data=data, headers=headers,
|
||||
**self._settings)
|
||||
return request(method, url, data=data, headers=headers,
|
||||
session=self._session, **self._settings)
|
||||
|
||||
@staticmethod
|
||||
def _check_response(response):
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import requests
|
|||
import urlparse
|
||||
import hashlib
|
||||
from .base import Storage, Item
|
||||
from vdirsyncer.utils import expand_path, get_password
|
||||
from vdirsyncer.utils import expand_path, get_password, request
|
||||
|
||||
|
||||
def split_collection(text):
|
||||
|
|
@ -113,7 +113,7 @@ class HttpStorage(HttpStorageBase):
|
|||
self._items = {}
|
||||
|
||||
def list(self):
|
||||
r = requests.get(self.url, **self._settings)
|
||||
r = request('GET', self.url, **self._settings)
|
||||
r.raise_for_status()
|
||||
self._items.clear()
|
||||
for i, item in enumerate(split_collection(r.text)):
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ except ImportError:
|
|||
keyring = None
|
||||
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
password_key_prefix = 'vdirsyncer:'
|
||||
|
||||
|
||||
|
|
@ -151,3 +154,25 @@ def get_password(username, resource):
|
|||
username, password)
|
||||
|
||||
return password
|
||||
|
||||
|
||||
def request(method, url, data=None, headers=None, auth=None, verify=None,
|
||||
session=None):
|
||||
'''wrapper method for requests, to ease logging and mocking'''
|
||||
|
||||
logger = vdirsyncer.log.get('utils')
|
||||
|
||||
if session is None:
|
||||
func = requests.request
|
||||
else:
|
||||
func = session.request
|
||||
|
||||
logger.debug(u'{} {}'.format(method, url))
|
||||
logger.debug(headers)
|
||||
logger.debug(data)
|
||||
logger.debug('Sending request...')
|
||||
r = func(method, url, data=data, headers=headers, auth=auth, verify=verify)
|
||||
logger.debug(r.status_code)
|
||||
logger.debug(r.headers)
|
||||
logger.debug(r.content)
|
||||
return r
|
||||
|
|
|
|||
Loading…
Reference in a new issue