Skip to content

Commit

Permalink
fix: Setup logging (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
egekocabas authored Jan 10, 2024
1 parent 6d5f157 commit 5f364b3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
24 changes: 22 additions & 2 deletions parma_mining/reddit/api/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
"""Main entrypoint for the API routes in of parma-analytics."""
import json
import logging
import os

from fastapi import FastAPI, HTTPException, status

from parma_mining.reddit.api.analytics_client import AnalyticsClient
from parma_mining.reddit.client import RedditClient
from parma_mining.reddit.model import CompaniesRequest, CompanyModel, DiscoveryModel

env = os.getenv("env", "local")

if env == "prod":
logging.basicConfig(level=logging.INFO)
elif env in ["staging", "local"]:
logging.basicConfig(level=logging.DEBUG)
else:
logging.warning(f"Unknown environment '{env}'. Defaulting to INFO level.")
logging.basicConfig(level=logging.INFO)

logger = logging.getLogger(__name__)

app = FastAPI()
reddit_client = RedditClient()
analytics_client = AnalyticsClient()
Expand All @@ -15,6 +29,7 @@
@app.get("/", status_code=200)
def root():
"""Root endpoint for the API."""
logger.debug("Root endpoint called")
return {"welcome": "at parma-mining-reddit"}


Expand Down Expand Up @@ -56,8 +71,13 @@ def get_company_info(companies: CompaniesRequest) -> list[CompanyModel]:
for company in all_comp_details:
try:
analytics_client.feed_raw_data(company)
except HTTPException:
raise HTTPException("Can't send crawling data to the Analytics.")
except HTTPException as e:
logger.error(
f"Can't send crawling data for {company} to the Analytics: {e}"
)
raise HTTPException(
f"Can't send crawling data for {company} to the Analytics: {e}"
)
return all_comp_details


Expand Down
3 changes: 3 additions & 0 deletions parma_mining/reddit/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Reddit client for fetching data from Reddit API."""
import logging
import os
from datetime import datetime

Expand All @@ -13,6 +14,8 @@
)
from parma_mining.reddit.normalization_map import RedditNormalizationMap

logger = logging.getLogger(__name__)


class RedditClient:
"""Client class for fetching data from Reddit API."""
Expand Down
3 changes: 3 additions & 0 deletions tests/test_company_info.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from unittest.mock import MagicMock

import pytest
Expand All @@ -10,6 +11,8 @@

client = TestClient(app)

logger = logging.getLogger(__name__)


@pytest.fixture
def mock_analytics_client(mocker) -> MagicMock:
Expand Down

0 comments on commit 5f364b3

Please sign in to comment.