Skip to content

Commit

Permalink
wip_workingExample
Browse files Browse the repository at this point in the history
  • Loading branch information
tijsziere committed Sep 30, 2024
1 parent 17b4de0 commit 856f433
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 57 deletions.
41 changes: 34 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,40 @@
import io
import json
from dotenv import load_dotenv
import logging
from opentelemetry._logs import set_logger_provider
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter
from routes import routes121, routesEspo, routesGeneric, routesKobo


from utils.logging import setup_logging

# load environment variables
load_dotenv()
port = os.environ["PORT"]

# Setup logging
setup_logging()
# Set up logs export to Azure Application Insights
logger_provider = LoggerProvider()
set_logger_provider(logger_provider)
exporter = AzureMonitorLogExporter(
connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter))

# Attach LoggingHandler to root logger
handler = LoggingHandler()
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.NOTSET)
logger = logging.getLogger(__name__)

# Silence noisy loggers
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("azure").setLevel(logging.WARNING)
logging.getLogger("requests_oauthlib").setLevel(logging.WARNING)
logging.getLogger("asyncio").setLevel(logging.WARNING)
logging.getLogger("opentelemetry").setLevel(logging.ERROR)

# initialize FastAPI
app = FastAPI(
Expand All @@ -52,6 +77,8 @@
},
)



# initialize CosmosDB
client_ = cosmos_client.CosmosClient(
os.getenv("COSMOS_URL"),
Expand All @@ -69,10 +96,10 @@ async def docs_redirect():
return RedirectResponse(url="/docs")

# Include routes
app.include_router(121_routes.router)
app.include_router(espo_routes.router)
app.include_router(generic_routes.router)
app.include_router(kobo_routes.router)
app.include_router(routes121.router)
app.include_router(routesEspo.router)
app.include_router(routesGeneric.router)
app.include_router(routesKobo.router)



Expand Down
Empty file added routes/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions routes/121_routes.py → routes/routes121.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
from fastapi import APIRouter, Request, Depends, HTTPException
import requests
from fastapi.responses import JSONResponse
from utils.utilsKobo import clean_kobo_data, get_attachment_dict, required_headers_kobo
from utils.utils121 import login121, required_headers_121
import os

router = APIRouter()

@router.post("/kobo-to-121")
async def kobo_to_121(request: Request, dependencies=Depends(required_headers_121)):
"""Send a Kobo submission to 121."""
Expand Down
12 changes: 4 additions & 8 deletions routes/espo_routes.py → routes/routesEspo.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
from fastapi import APIRouter, Request, Depends, HTTPException
from fastapi.responses import JSONResponse
from utils.kobo_utils import (
from utils.utilsKobo import (
add_submission,
clean_kobo_data,
get_attachment_dict,
update_submission_status,
espo_request,
required_headers_espocrm,
required_headers_121,
login121,
update_submission_status
)
from utils.espo_utils import espo_request, required_headers_espocrm
from utils.utilsEspo import espo_request, required_headers_espocrm
import logging
import os

router = APIRouter()

@app.post("/kobo-to-espocrm")
@router.post("/kobo-to-espocrm")
async def kobo_to_espocrm(
request: Request, dependencies=Depends(required_headers_espocrm)
):
Expand Down
7 changes: 7 additions & 0 deletions routes/generic_routes.py → routes/routesGeneric.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
from fastapi import APIRouter, Request, Depends, HTTPException
from fastapi.responses import JSONResponse
from utils.utilsKobo import clean_kobo_data, get_attachment_dict
import requests

router = APIRouter()

@router.post("/kobo-to-generic")
async def kobo_to_generic(request: Request):
"""Send a Kobo submission to a generic API.
Expand Down
25 changes: 15 additions & 10 deletions routes/kobo_routes.py → routes/routesKobo.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from fastapi import APIRouter, Request, Depends, HTTPException
from fastapi.responses import JSONResponse
import requests
import base64
import csv
import io
import json
from enum import Enum
from utils.utils121 import login121
from utils.utilsKobo import required_headers_121_kobo

router = APIRouter()

@router.post("/update-kobo-csv")
async def prepare_kobo_validation(request: Request, programId: int, kobousername: str, dependencies=Depends(required_headers_121_kobo)):
"""
Prepare Kobo validation by fetching data from 121 platform,
converting it to CSV, and uploading to Kobo.
"""
# get access token from cookie
body = {'username': request.headers['username121'], 'password': request.headers['password121']}
url = f"{request.headers['url121']}/api/users/login"
login = requests.post(url, data=body)
if login.status_code >= 400:
raise HTTPException(
status_code=login.status_code,
detail=login.content.decode("utf-8")
)
access_token = login.json()['access_token_general']

access_token = login121(request.headers["url121"], request.headers["username121"], request.headers["password121"])

# Fetch data from 121 platform
response = requests.get(
Expand Down
29 changes: 0 additions & 29 deletions utils/logging.py

This file was deleted.

6 changes: 6 additions & 0 deletions utils/121_utils.py → utils/utils121.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import requests
from fastapi import HTTPException, Header
from datetime import datetime, timedelta
from utils.utilsKobo import required_headers_kobo
import unicodedata

def clean_text(text):
# Normalize text to remove accents
normalized_text = unicodedata.normalize("NFD", text)
Expand Down
3 changes: 1 addition & 2 deletions utils/espo_utils.py → utils/utilsEspo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from clients.espo_api_client import EspoAPI
import requests
import time
import logger
from fastapi import HTTPException
from fastapi import HTTPException, Header
from datetime import datetime, timedelta
from azure.cosmos.exceptions import CosmosResourceExistsError

Expand Down
2 changes: 1 addition & 1 deletion utils/kobo_utils.py → utils/utilsKobo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time
import base64
import logging
from fastapi import HTTPException
from fastapi import HTTPException, Header
from datetime import datetime, timedelta
from azure.cosmos.exceptions import CosmosResourceExistsError

Expand Down

0 comments on commit 856f433

Please sign in to comment.