-
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.
Create simple public version of API. (WIP)
- Loading branch information
1 parent
2c84df6
commit 3bcd223
Showing
5 changed files
with
68 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# ! WARNING: When changing DATABASE_URL, also change it in alembic.ini | ||
DATABASE_URL=postgresql://root:root@localhost:5432/rhinventory | ||
SECRET_KEY=abcabc | ||
FLASK_DEBUG=True | ||
GITHUB_CLIENT_ID= | ||
GITHUB_CLIENT_SECRET= | ||
GITHUB_REDIRECT_URI= | ||
|
||
FILE_STORE_LOCAL="files" | ||
MULTIPROCESSING_ENABLED=True |
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,34 +1,36 @@ | ||
from fastapi import APIRouter, Depends | ||
from sqlalchemy.orm import Session | ||
|
||
from rhinventory.api.asset.schemas import AssetSchema | ||
from rhinventory.api.asset.schemas import AssetSchema, AssetListOutputSchema | ||
from rhinventory.api.database import get_db | ||
from rhinventory.models.asset import Asset | ||
from rhinventory.service.asset.services import AssetService | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get(path="/{asset_id}") | ||
@router.get("/get/{asset_id}") | ||
def get_asset(asset_id: int, db: Session = Depends(get_db)): | ||
asset = db.query(Asset).get(asset_id) | ||
if not asset: | ||
return {"error": "Asset not found"} | ||
|
||
return { | ||
'id': asset.id, | ||
'category': asset.category.name, | ||
'name': asset.name | ||
} | ||
|
||
@router.get("/list") | ||
def list_assets(): | ||
return [ | ||
AssetSchema( | ||
id=1, | ||
name="Sample asset 1" | ||
), | ||
AssetSchema( | ||
id=2, | ||
name="Sample asset 2" | ||
) | ||
] | ||
# TODO: Hide private | ||
|
||
return AssetSchema.model_validate(asset, from_attributes=True) | ||
|
||
|
||
@router.get("/list-public", response_model=AssetListOutputSchema) | ||
def list_assets(limit: int = 20, offset: int = 0, db: Session = Depends(get_db)): | ||
""" | ||
List all public items. | ||
Has two parameters: | ||
* limit: How many items to return. | ||
* offset: Offset in database query. | ||
""" | ||
assets = AssetService.list_all(db, limit, offset, private=False) | ||
|
||
return AssetListOutputSchema( | ||
assets=[AssetSchema.model_validate(asset, from_attributes=True) for asset in assets], | ||
) |
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,6 +1,15 @@ | ||
from typing import List | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class AssetSchema(BaseModel): | ||
id: int | ||
name: str | ||
model: str | None | ||
note: str | None | ||
serial: str | None | ||
|
||
|
||
class AssetListOutputSchema(BaseModel): | ||
assets: List[AssetSchema] |
Empty file.
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,27 @@ | ||
from sqlalchemy import select | ||
from sqlalchemy.orm import Session | ||
|
||
from rhinventory.api.asset.schemas import AssetSchema | ||
from rhinventory.models.asset import Asset | ||
from rhinventory.models.enums import HIDDEN_PRIVACIES, PUBLIC_PRIVACIES | ||
|
||
|
||
class AssetService: | ||
@staticmethod | ||
def list_all(db_session: Session, limit: int = 100, offset: int = 0, private=False): | ||
""" | ||
List all Assets. | ||
:param limit: How much assets to return (defaut 100). | ||
:param offset: With what offset (defaults to 0). | ||
:param private: If True, shows even private items (defaults to False). | ||
:return: List of Asset objects. | ||
""" | ||
|
||
assets = db_session.execute( | ||
select(Asset) | ||
.where(Asset.privacy.in_(PUBLIC_PRIVACIES if not private else PUBLIC_PRIVACIES + HIDDEN_PRIVACIES)) | ||
.limit(limit).offset(offset) | ||
) | ||
|
||
return [asset[0] for asset in assets.fetchall()] |