Example #1
0
    def test_post_data(self):
        # issue #2, raw payload
        self.s = CachedSession(CACHE_NAME,
                               CACHE_BACKEND,
                               allowable_methods=('GET', 'POST'))
        d1 = json.dumps({'param1': 'test1'})
        d2 = json.dumps({'param1': 'test1', 'param2': 'test2'})
        d3 = str('some unicode data')
        if is_py3:
            bin_data = bytes('some binary data', 'utf8')
        else:
            bin_data = bytes('some binary data')

        for d in (d1, d2, d3):
            self.assertEqual(self.post(d)['data'], d)
            r = self.s.post(httpbin('post'), data=d)
            self.assert_(hasattr(r, 'from_cache'))

        self.assertEqual(self.post(bin_data)['data'], bin_data.decode('utf8'))
        r = self.s.post(httpbin('post'), data=bin_data)
        self.assert_(hasattr(r, 'from_cache'))
Example #2
0
    def test_post_data(self):
        # issue #2, raw payload
        requests_cache.configure(CACHE_NAME, CACHE_BACKEND,
                                 allowable_methods=('GET', 'POST'))
        d1 = json.dumps({'param1': 'test1'})
        d2 = json.dumps({'param1': 'test1', 'param2': 'test2'})
        d3 = str('some unicode data')
        if is_py3:
            bin_data = bytes('some binary data', 'utf8')
        else:
            bin_data = bytes('some binary data')

        for d in (d1, d2, d3):
            self.assertEqual(self.post(d)['data'], d)
            r = requests.post(httpbin('post'), data=d)
            self.assert_(hasattr(r, 'from_cache'))

        self.assertEqual(self.post(bin_data)['data'],
                         bin_data.decode('utf8'))
        r = requests.post(httpbin('post'), data=bin_data)
        self.assert_(hasattr(r, 'from_cache'))
Example #3
0
def _request_send_hook(self, *args, **kwargs):
    if self.method not in _config["allowable_methods"]:
        return _original_request_send(self, *args, **kwargs)

    if self.method == "POST":
        data = self._encode_params(getattr(self, "data", {}))
        if isinstance(data, tuple):  # old requests versions
            data = data[1]
        cache_url = self.full_url + str(data)
    else:
        cache_url = self.full_url

    def send_request_and_cache_response():
        result = _original_request_send(self, *args, **kwargs)
        if result and self.response.status_code in _config["allowable_codes"]:
            _cache.save_response(cache_url, self.response)
        return result

    response, timestamp = _cache.get_response_and_time(cache_url)
    if response is None:
        return send_request_and_cache_response()

    if _config["expire_after"] is not None:
        difference = datetime.now() - timestamp
        if difference > timedelta(minutes=_config["expire_after"]):
            _cache.del_cached_url(cache_url)
            return send_request_and_cache_response()

    response.from_cache = True
    self.sent = True
    self.response = response
    # TODO: is it stable api?
    if dispatch_hook is not None:
        dispatch_hook("response", self.hooks, self.response)
        r = dispatch_hook("post_request", self.hooks, self)
        self.__dict__.update(r.__dict__)
    return True