Skip to content

Commit

Permalink
Create simple public version of API. (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomascapek committed Aug 11, 2024
1 parent 2c84df6 commit 3bcd223
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
10 changes: 10 additions & 0 deletions .envrc
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
42 changes: 22 additions & 20 deletions rhinventory/api/asset/endpoints.py
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],
)
9 changes: 9 additions & 0 deletions rhinventory/api/asset/schemas.py
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.
27 changes: 27 additions & 0 deletions rhinventory/service/asset/services.py
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()]

0 comments on commit 3bcd223

Please sign in to comment.