Skip to content

Commit 8d9a674

Browse files
committed
create campfire bot
1 parent 8f2e4cd commit 8d9a674

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
fastapi[all]
22
pytest
33
pip-tools
4+
pytest-httpx

requirements.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ httpcore==0.16.1
3838
httptools==0.5.0
3939
# via uvicorn
4040
httpx==0.23.1
41-
# via fastapi
41+
# via
42+
# fastapi
43+
# pytest-httpx
4244
idna==3.4
4345
# via
4446
# anyio
@@ -69,6 +71,10 @@ pydantic==1.10.2
6971
pyparsing==3.0.9
7072
# via packaging
7173
pytest==7.2.0
74+
# via
75+
# -r requirements.in
76+
# pytest-httpx
77+
pytest-httpx==0.21.2
7278
# via -r requirements.in
7379
python-dotenv==0.21.0
7480
# via uvicorn

src/basecamp/campfire_bot.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import httpx
2+
from httpx._models import Response
3+
4+
from config import get_settings
5+
6+
settings = get_settings()
7+
8+
9+
class CampfireBot:
10+
def send_message(self, message: str) -> Response:
11+
""" https://github.com/basecamp/bc3-api/blob/master/sections/chatbots.md#create-a-line """
12+
13+
url = f'https://3.basecampapi.com/{settings.BASECAMP_ACCOUNT_ID}/integrations/{settings.BASECAMP_CHATBOT_KEY}/buckets/1/chats/2/lines.json'
14+
15+
response = httpx.post(url, data={'content': message})
16+
response.raise_for_status()
17+
18+
return response
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
from basecamp.campfire_bot import CampfireBot
4+
from httpx._exceptions import HTTPStatusError
5+
6+
7+
@pytest.fixture
8+
def success_response(httpx_mock):
9+
httpx_mock.add_response(
10+
method='POST',
11+
status_code=200,
12+
)
13+
14+
15+
@pytest.fixture
16+
def bad_response(httpx_mock):
17+
httpx_mock.add_response(
18+
method='POST',
19+
status_code=400,
20+
)
21+
22+
23+
@pytest.fixture
24+
def bot():
25+
return CampfireBot()
26+
27+
28+
@pytest.mark.usefixtures('success_response')
29+
def test_good_response(bot):
30+
result = bot.send_message('Hello World!')
31+
32+
assert result.status_code == 200
33+
34+
35+
@pytest.mark.usefixtures('bad_response')
36+
def test_bad_response(bot):
37+
with pytest.raises(HTTPStatusError):
38+
bot.send_message('Hello World!')

0 commit comments

Comments
 (0)