Implementieren Sie die Python -Abhängigkeitsinjektorbibliothek in AzurefunktionenPython

Python-Programme
Anonymous
 Implementieren Sie die Python -Abhängigkeitsinjektorbibliothek in Azurefunktionen

Post by Anonymous »

Implementieren Sie die Python -Abhängigkeitsinjektorbibliothek in Azure -Funktionen < /h1>
Beschreibung des Problems < /H2>
Ich versuche, den Abhängigkeitsinjektor für Python Azure -Funktionen zu implementieren. Abhängigkeitsinjektor
https://pythondependency-injector.ets-labs.org/
Ich werde jedoch unter dem Fehler untersuchen. 'dict' Objekt hat kein Attribut 'codieren' '< /p>
< /blockquote>
Dies ist der Code, den ich implementieren möchte. Könnten Sie bitte jemanden hier leiten lassen? < /P>

Code: Select all

function app file name: function_app.py

Code: Select all

import azure.functions as func
from fastapi import FastAPI, Depends, Request, Response
from dependency_injector.wiring import inject, Provide
from abstraction.di_container import DIContainer
import logging
import json

from src.config.app_settings import AppSettings

container = DIContainer()
container.wire(modules=[__name__])

fast_app = FastAPI()

@fast_app.exception_handler(Exception)
async def handle_exception(request: Request, exc: Exception):
return Response(
status_code=400,
content={"message": str(exc)},
)

@fast_app.get("/")
@inject
async def home(settings: AppSettings = Depends(Provide[DIContainer.app_config])):
cont_name =  settings.get("ContainerName", "No setting found")
return {
"info": f"Try to get values from local.settings using DI {cont_name}"
}

@fast_app.get("/v1/test/{test}")
async def get_test(self,
test: str):
return {
"test": test
}

app = func.AsgiFunctionApp(app=fast_app, http_auth_level=func.AuthLevel.ANONYMOUS)

< /code>
Dependency Injector file name: di_container.py

Code: Select all

from dependency_injector import containers, providers
from src.config.app_settings import AppSettings

class DIContainer(containers.DeclarativeContainer):
app_config = providers.Singleton(AppSettings)
< /code>
Application Setting to read local.settings.json file: app_settings.py

Code: Select all

import json
import os
from dependency_injector import containers, providers

class AppSettings:
def __init__(self, file_path="local.settings.json"):
self.config_data = {}
if os.path.exists(file_path):
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
self.config_data = data.get("Values", {})

def get(self, key: str, default=None):
return os.getenv(key,self.config_data.get(key, default))

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post