Skip to content

Commit

Permalink
Don't clear endpoint cache on terminal method
Browse files Browse the repository at this point in the history
  • Loading branch information
gshand committed Jul 26, 2023
1 parent 37bcffd commit 2d287db
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = fluent-discourse
version = 1.0.0
version = 1.0.1
author = Grayden Shand
author_email = graydenshand@gmail.com
description = A fluent interface to the Discourse API
Expand All @@ -22,4 +22,4 @@ install_requires =
requests

[options.packages.find]
where = src
where = src
5 changes: 0 additions & 5 deletions src/fluent_discourse/discourse.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def _request(self, method, url, data=None, params=None):
return self._handle_error(r, method, url, data, params)

def _handle_error(self, response, method, url, data, params):
self._cache = []
if response.status_code == 404:
raise PageNotFoundError(
f"The requested page was not found, or you do not have permission to access it: {response.url}"
Expand Down Expand Up @@ -94,25 +93,21 @@ def _wait_for_rate_limit(self, response, method, url, data, params):
def get(self, data=None):
# Make a get request
url = self._make_url()
self._cache = []
return self._request("GET", url, params=data)

def post(self, data=None):
# Make a post request
url = self._make_url()
self._cache = []
return self._request("POST", url, data=data)

def put(self, data=None):
# Make a put request
url = self._make_url()
self._cache = []
return self._request("PUT", url, data=data)

def delete(self, data=None):
# Make a delete request
url = self._make_url()
self._cache = []
return self._request("DELETE", url, data=data)

def _make_url(self):
Expand Down
16 changes: 16 additions & 0 deletions tests/0_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from fluent_discourse import *
import pytest
import json
from unittest.mock import patch


def test_accumulate_strings(client):
Expand Down Expand Up @@ -72,3 +73,18 @@ def test_wait_for_rate_limit():
MockResponse.status_code = 429
client = Discourse.from_env(raise_for_rate_limit=False)
client._wait_for_rate_limit(MockResponse, "GET", None, None, None)


def test_reuse_endpoint(client):
with patch("fluent_discourse.Discourse._request"):
endpoint = client.test.a.path
assert endpoint._cache == ["test", "a", "path"]
endpoint.get({"foo": "bar"})
assert endpoint._cache == ["test", "a", "path"]


def test_reuse_client(client):
with patch("fluent_discourse.Discourse._request"):
client.test.a.path.get()
endpoint = client.foo.bar
assert endpoint._cache == ["foo", "bar"]

0 comments on commit 2d287db

Please sign in to comment.