generated from ks6088ts/template-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
IOT_HUB_DEVICE_CONNECTION_STRING="CHANGE_ME" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from logging import getLogger | ||
|
||
from tests.utilities import client | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
def test_iot(): | ||
path_format = "/iot_hub/{0}" | ||
response = client.get( | ||
url=path_format.format("device_twin"), | ||
) | ||
assert response.status_code == 200 | ||
logger.info(f"response: {response.json()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
from fastapi import FastAPI | ||
|
||
from workshop_azure_iot.routers.core import router as core_router | ||
from workshop_azure_iot.routers.iot_hub import router as iot_hub_router | ||
|
||
app = FastAPI( | ||
docs_url="/", | ||
) | ||
|
||
for router in [ | ||
core_router, | ||
iot_hub_router, | ||
# Add routers here | ||
]: | ||
app.include_router(router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from logging import getLogger | ||
|
||
from azure.iot.device.aio import IoTHubDeviceClient | ||
|
||
from workshop_azure_iot.settings.iot_hub import Settings | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
class Client: | ||
def __init__(self, settings: Settings) -> None: | ||
self.settings = settings | ||
|
||
async def get_device_twin(self) -> dict: | ||
client = IoTHubDeviceClient.create_from_connection_string(self.settings.iot_hub_device_connection_string) | ||
# FIXME: to make it faster, connection should be established once and reused | ||
await client.connect() | ||
twin = await client.get_twin() | ||
await client.shutdown() | ||
return twin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from logging import getLogger | ||
|
||
from fastapi import APIRouter, status | ||
from fastapi.responses import JSONResponse | ||
|
||
from workshop_azure_iot.internals.iot_hub import Client | ||
from workshop_azure_iot.settings.iot_hub import Settings | ||
|
||
logger = getLogger(__name__) | ||
|
||
client = Client( | ||
settings=Settings(), | ||
) | ||
|
||
router = APIRouter( | ||
prefix="/iot_hub", | ||
tags=["iot_hub"], | ||
) | ||
|
||
|
||
@router.get("/device_twin") | ||
async def get_device_twin(): | ||
return JSONResponse( | ||
status_code=status.HTTP_200_OK, | ||
content=await client.get_device_twin(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
|
||
class Settings(BaseSettings): | ||
iot_hub_device_connection_string: str | ||
model_config = SettingsConfigDict(env_file="iot_hub.env") |