Add gitea router
- added a gitea router - /gitea/latest endpoint gives the latest release version of a repo.
This commit is contained in:
parent
b891efe601
commit
88ffd71911
3 changed files with 48 additions and 0 deletions
5
api.py
5
api.py
|
@ -25,3 +25,8 @@ for m_path in Path("routers").glob("*"):
|
|||
@api.get("/")
|
||||
def quack():
|
||||
return api_data
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
api_data["version"] += "-dev"
|
||||
uvicorn.run(api)
|
6
log.conf
6
log.conf
|
@ -29,6 +29,12 @@ handlers=console,file
|
|||
qualname=security
|
||||
propagate=0
|
||||
|
||||
[logger_gitea]
|
||||
level=DEBUG
|
||||
handlers=console,file
|
||||
qualname=security
|
||||
propagate=0
|
||||
|
||||
[handler_console]
|
||||
class=StreamHandler
|
||||
level=DEBUG
|
||||
|
|
37
routers/gitea.py
Normal file
37
routers/gitea.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
import logging
|
||||
from typing import Optional
|
||||
import giteapy
|
||||
import json
|
||||
|
||||
from pathlib import Path
|
||||
from fastapi import APIRouter, Header, Query, HTTPException
|
||||
from gitea.gitea import Repository
|
||||
|
||||
ROOT_PATH = Path.home().joinpath(".radical_api/gitea")
|
||||
|
||||
logger = logging.getLogger("gitea")
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/gitea/latest")
|
||||
def get_latest_version(base_url: Optional[str] = Header(None), author: str = Query(None), repo: str = Query(None), pre_releases: bool = Query(False)):
|
||||
with ROOT_PATH.joinpath("keys.json").open("r") as fp:
|
||||
data: dict = json.load(fp)
|
||||
if base_url in data.keys():
|
||||
config = giteapy.Configuration()
|
||||
config.api_key['access_token'] = data[base_url]
|
||||
config.host = base_url + "/api/v1"
|
||||
api = giteapy.RepositoryApi(giteapy.ApiClient(config))
|
||||
response = api.repo_list_releases(author, repo)
|
||||
if len(response) == 0:
|
||||
raise HTTPException(404, "No releases for given repository.")
|
||||
|
||||
for release in response:
|
||||
release: giteapy.Release
|
||||
if pre_releases:
|
||||
return release.name
|
||||
elif not pre_releases and release.prerelease:
|
||||
continue
|
||||
elif not pre_releases and not release.prerelease:
|
||||
return release.name
|
||||
else:
|
||||
raise HTTPException(400, "API cannot access this Gitea instance.")
|
Loading…
Reference in a new issue